Home > Software design >  Incoming constraints for Container in Flutter. Do i get it wrong?
Incoming constraints for Container in Flutter. Do i get it wrong?

Time:08-19

As enter image description here

CodePudding user response:

If you read the documentation a bit further, you'll see that constraint arguments on the Container itself will override this behavior:

Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and constraints arguments to the constructor override this.

Try wrapping the inner childless Container in an UnconstrainedBox, and you'll see it will shrink to zero width and height.

body: 
   Container(
    color: Colors.red,
    width: double.infinity,
    height:double.infinity,
    child: UnconstrainedBox(
      child: Container(
        color: Colors.green,
      ),
    ),
)

CodePudding user response:

Although red container has infinite size, You put it in a scaffold's body which has specific size. so your red container get specific size then your grin container with no child get size as much as its parents.

CodePudding user response:

The red container actually will has size as small as posibble depends its child.

the green container has no child, then as the documentation said it will try to be as big as possible unless the incoming constraints are unbounded,

if you change the child , like Text widget, or Column, or other widget, it will not get the maximum size.

  • Related