Flutter Container() vs SizedBox() for dummy empty


Asked by Taufik N on August 29, 2019 (source).

I am curious about this. I have seen many example using Container() for dummy hidden widget, for example, when loading has completed, then we setState(() { _isLoaded = true; });. So we can use the state like this, right?

return _isLoaded ? Container() : LoaderWidget();

Or maybe using SizedBox() is actually better because it don't take much parameters and often used for padding anyway?

return _isLoaded ? SizedBox() : LoaderWidget();

Or am I wrong?


Question answered by Spatz (source).

In case of use as a placeholder:

Container if the widget has no child, no height, no width, no constraints, and no alignment, but the parent provides bounded constraints, then Container expands to fit the constraints provided by the parent.

SizedBox if the widget has no child, no height, no width, then width and height are zero.

Therefore, SizedBox() serves more as a syntactic placeholder.

Also, note that SizedBox() is a const constructor but Container() is not. The first one allows the compiler to create more efficient code.