Why Flutter Container does not respects its width and height constraints when it is inside other Container
Asked Answered
A

4

75
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.

Apiarist answered 15/2, 2019 at 22:13 Comment(2)
Please make the "Update" section as an answer. Its a valuable information and I can interpret all the layout complexities using it. Thanks.Lavern
Thank you for the update! Very useful question and answersArleta
A
28

All views inside a layout must have four constraints:

  1. Width
  2. Height
  3. X position
  4. 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
                    ),
            ),
        )
)

enter image description here

The inner container in red, the outer container in green.

Apiarist answered 15/10, 2020 at 0:56 Comment(0)
A
91

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.

Asmara answered 15/2, 2019 at 22:24 Comment(3)
From where did you get that knowledge? I have been looking at docs but I have not found anything like that. Especially this: "When you specify a width/height on a Container, you're not constraining Container. You're constraining the child of Container"Apiarist
You can find it right here. Container combines its width/height properties with its constraints (if set) to constrain its child.Nathalia
This is not Container specific. It apply to all Flutter widgets. I don't think there 's any documentation about this, I saw it by experience and looking at the source codeEloyelreath
A
28

All views inside a layout must have four constraints:

  1. Width
  2. Height
  3. X position
  4. 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
                    ),
            ),
        )
)

enter image description here

The inner container in red, the outer container in green.

Apiarist answered 15/10, 2020 at 0:56 Comment(0)
N
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.

Nutlet answered 24/6, 2020 at 3:47 Comment(0)
M
0

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

Maimonides answered 18/4, 2022 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.