Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: SizedBox Vs Container, why use one instead of the other?

Tags:

flutter

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:

  1. What are the differences between a Container and SizedBox?

  2. 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?

  3. There are performance differences between them?

like image 716
Daniel Oliveira Avatar asked Apr 16 '19 20:04

Daniel Oliveira


People also ask

What is the difference between card and container in Flutter?

If you are familiar with HTML , think of containers like divs. They allow you to wrap other content. On the other hand, Card is an implementation of Material Design, A material design card.

Why are there containers in Flutter?

Container class in flutter is a convenience widget that combines common painting, positioning, and sizing of widgets. A Container class can be used to store one or more widgets and position them on the screen according to our convenience. Basically, a container is like a box to store contents.

What is SizedBox in Flutter?

SizedBox is a built-in widget in flutter SDK. It is a simple box with a specified size. It can be used to set size constraints to the child widget, put an empty SizedBox between the two widgets to get some space in between, or something else. It is somewhat similar to a Container widget with fewer properties.


1 Answers

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.

like image 187
Herbert Poul Avatar answered Sep 16 '22 13:09

Herbert Poul