Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Container() vs SizedBox() for dummy empty

Tags:

flutter

dart

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?

like image 631
Taufik Nur Rahmanda Avatar asked Aug 29 '19 04:08

Taufik Nur Rahmanda


People also ask

What is the difference between SizedBox and container in flutter?

Difference Between SizedBox and Container Widgets Container can also be used to add space between widgets Or wrap the child and give it a specific height. But Container has some extra properties. So, We can use SizedBox instead where we can specify child , height and width .

What can I use instead of container in flutter?

Placeholder Widgets. Use SizedBox instead of Container as Placeholder widgets.

How do you make a blank page on flutter?

To start from a Blank Page, select + Create New under the Blank Page template. The UI Builder will then open and show a blank page.


1 Answers

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.

like image 199
Spatz Avatar answered Oct 02 '22 20:10

Spatz