Reputation: 2304
I'm new to Flutter and I'm really confused about size of a container. I don't understand from the docs and from all the reading and tutorial watching how the size of the container is set and what parametr wins and when. For example when a container is inside Expanded but it has width and height so what its size or if a container has a child has width and height so does it shrink to the child size of honor its own. So many combination Is there a some kind or rule for all cases?
Upvotes: 0
Views: 381
Reputation: 729
Below is a quote from the official flutter documentation:
"Summary: Container tries, in order: to honor alignment, to size itself to the child, to honor the width, height, and constraints, to expand to fit the parent, to be as small as possible." (More Specifically)[https://api.flutter.dev/flutter/widgets/Container-class.html]
For example, if you set a height and width for the Container
but it is placed inside of the Expanded
widget. It will first size itself according to the height and width. However, the Expanded
(as the parent) widget will provide a new constraint to the Container
, making it expand to fill the space (as specified by Expanded
). This is also applicable if BoxConstraints
is passed into the constraints
property of Container
. The official documentation says:
... the parent provides bounded constraints, then the Container tries to expand to fit the parent ...
which correlates to the part where it says 'expand to fit the parent' after 'honor the width, height, and constraints'
In the second example, the Container
will size itself to the height and width. It will first try to size itself to the child and then proceed to honor the width and height, making it size to the width and height specified.
In my experience, it will come naturally as you start using more containers. I was confused too when I first started.
Upvotes: 1