Carlos Daniel
Carlos Daniel

Reputation: 2729

Is it true that setting a constants file in Flutter with `SizedBox` width and height make the app more performant?

I came across this article in which the author says that it's more performant defining a file with all SizedBox width and height constants that is going to be imported each time a SizedBox is needed.

Is it true? Or how can we confirm what he says is true? Or, what happens if I use his approach, would it be counterproductive?

Upvotes: 3

Views: 119

Answers (2)

Sauron
Sauron

Reputation: 1353

const means that the following object can and will be instantiated before the program is run.
So I don't think it will be more performant, its same.
but I guess its just better approach, to write your const SizedBox in separate file compare to write at each time, less typing and clarification as mentioned in the article.

Upvotes: 0

MendelG
MendelG

Reputation: 20098

From the article:

In a separate file, we can define all the SizedBoxes we need as compile time constants, using multiples of 4 pixels:

so the thing is not that wer'e creating a separate file, but rather the point is that we're creating constants

The reason why SizedBox is better, since it can be const which is immutable and created in compile time so it's faster.

For example, this code works:

Scaffold(
      body: const SizedBox(),
    );

But this code will fail:

Scaffold(
  body: const Container(),
);

Also, take a look at the docs:

A Container is a heavier Widget than a SizedBox, and as bonus, SizedBox has a const constructor.

See also

Upvotes: 0

Related Questions