Reputation: 2729
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
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
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 const
ants
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.
Upvotes: 0