When I start to think about those two componentes I find myself arguing about why should I one instead of the other. Some questions that come to my mind:
-
What are the differences between a Container and SizedBox?
-
I understand that Container can have other parameters like padding or decoration, but if I will not use those, why should I use a SizedBox instead of a Container?
-
There are performance differences between them?
Best Answer
Small Update: When used for whitespace, there is now even a linter warning to prefer
SizedBox
instead ofContainer
. The main advantage seems to be thatSizedBox
can beconst
and won't even create a new instance during runtime.Thanks to the magic of open source, you don't have to guess too much.
Container
is basically just a convenience widget which sometimes saves you to nest 4 other widgets. If you pass width/height into theContainer
:Which will result in:
And the ConstrainedBox in effect is pretty much the same as a
SizedBox
, just more flexible.A
SizedBox
will do:ie. It is effectively the same. If you only use
Container
for width/height there might be a very minor minor negligible performance overhead. but you most certainly will not be able to measure it.. But I would still recommendSizedBox
because it's way clearer. imho.