'Why Flutter Container does not respects its width and height constraints when it is inside other Container
Container(
width: 200.0,
height: 200.0,
child: Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.red
),
),
)
I've been trying to find the answer in the Container class docs but I did not find it.
Update:
After a long time, I understood the problem.
All views inside a layout must have width, height, x position, and y position. (This applies to Android, IOS, Flutter, etc)
In my code, the inner container just has a width and height for that reason it doesn't know where to start painting.
For that reason, if the container is placed inside an Alignment widget the container gets the x position and y position and it works.
Solution 1:[1]
All views inside a layout must have four constraints:
- Width
- Height
- X position
- Y position
This applies to Android, IOS, Flutter, etc.
In the code, the inner container just has a width and height for that reason it doesn't know where to start painting and it gets all the available space.
But, if the inner container is placed inside another layout widget that aligns its child, for example, Center
. It will get its x and y positions.
Container(
decoration : BoxDecoration(color : Colors.green),
width: 200.0,
height: 200.0,
child: Center(
child: Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.red
),
),
)
)
The inner container in red, the outer container in green.
Solution 2:[2]
Constraints in Flutter works a bit different than usual. Widgets themselves do not have constraints.
When you specify a width
/height
on a Container
, you're not constraining Container
. You're constraining the child of Container
.
Container
will then size itself based on the size of its child.
As such, parent widgets always have the last word on how their descendants should be sized.
If you want to go around this, you have to use Align
widget:
Container(
width: 200.0,
height: 200.0,
child: Align(
alignment: Alignment.topLeft,
child: Container(
width: 50.0,
height: 50.0,
decoration:
BoxDecoration(shape: BoxShape.circle, color: Colors.red),
),
),
);
This may seem weird and limiting. But this single weirdness is the reason why Flutter's layout is so powerful and composable.
Solution 3:[3]
Keep in mind, setting width or height of a container, it's same as setting min-width and max-width or min-height and max-height at the same time.
Your case for the inner Container according to the documentation will be:
If the widget has no child and no alignment, but a height, width, or constraints are provided, then the Container tries to be as small as possible given the combination of those constraints and the parent's constraints.
Since your outer container gives minWidth=minHeight=200 constraints to the inner container, thus the size of your inner container cannot be less than Size(200, 200).
In support of Rémi Rousselet's answer, the Align widget, same as Center
widget, will expand to fit its parent's constraints, if its parent has constraints, and position its child in the specified alignment.
If the widget has an alignment, constraints, and the parent provides bounded constraints, then the Container tries to be as small as possible given the combination of those constraints and the parent’s constraints, and then positions the child within itself as per the alignment.
In this case, the outer Container has an Align widget as child (an alignment), constraints, and the parent provides bounded constraints, thus it tries to be as small as possible given the combination of its constraints and the parent’s constraints, which is Size(200, 200)
.
You might be wondering why does the inner Container has the size of 50*50
, instead of 200*200
, since its parent (outer Container) has specify the minWidth=minHeight = 200
. The reason is that as mentioned before the Align widget will expand to fit its parent constraints, so in this case the Align widget will expand to fit the outer Container
's constraints, i.e. it will have the size of Size(200, 200)
, and tell its child (the inner Container), that it can be any size it wants, but not bigger than my size, 200*200
. Therefore, the information of minWidth and minHeight of the outer Container that is supposed to be pass to the inner Container is lost.
Solution 4:[4]
Some additonal information for the "Update" section of the question:
As metioned above,
All views inside a layout must have width, height, x position, and y position. (This applies to Android, IOS, Flutter, etc)
In my code, the inner container just has a width and height for that reason it doesn't know where to start painting.
The reason why the inner container doesn't know where to start painting is that its parent, which is the outer Container Widget, does not have a corresponding RenderObject(it doesn't implement a createRenderObject
method), hence which can't performLayout
alone, and here comes the big deal:
Most widgets set the offset of their children(the x, and the y position of the children) by putting the Offset data in the parentData
attributes in them, and such process is done in the performLayout
method, so in the nested Container usage scanarios, the inner container can't get a reasonable offset from the outer widget, and therefore setting the width and height fails working in the inner container.
Furthermore, why Align
Widget can help solving this problem?
Take a glimpse of the Align
source code and everything becomes clear and plain
// Align -> createRenderObject -> RenderPositionedBox -> alignChild
@protected
void alignChild() {
// ....irrelevant code
final BoxParentData childParentData = child!.parentData! as BoxParentData;
childParentData.offset = _resolvedAlignment!.alongOffset(size - child!.size as Offset);
}
And I sure believe any other Widgets similar to Align
Widget that set the offset of their children can solve this problem as well
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | crrlos |
Solution 2 | Rémi Rousselet |
Solution 3 | Brady Peng |
Solution 4 | HonmaMeiko |