Small Update: When used for whitespace, there is now even a linter warning to prefer SizedBox
instead of Container
. The main advantage seems to be that SizedBox
can be const
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 the Container
:
constraints =
(width != null || height != null)
? constraints?.tighten(width: width, height: height)
?? BoxConstraints.tightFor(width: width, height: height)
: constraints,
Which will result in:
if (constraints != null)
current = ConstrainedBox(constraints: constraints, child: current);
And the ConstrainedBox in effect is pretty much the same as a SizedBox
, just more flexible.
A SizedBox
will do:
@override
RenderConstrainedBox createRenderObject(BuildContext context) {
return RenderConstrainedBox(
additionalConstraints: _additionalConstraints,
);
}
BoxConstraints get _additionalConstraints {
return BoxConstraints.tightFor(width: width, height: height);
}
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 recommend SizedBox
because it's way clearer. imho.