3

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?

Carlos Daniel
  • 2,459
  • 25
  • 30

2 Answers2

0

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
MendelG
  • 14,885
  • 4
  • 25
  • 52
0

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.

Sauron
  • 551
  • 2
  • 11