Valery  Kulikov
Valery Kulikov

Reputation: 319

Incoming constraints for Container in Flutter. Do i get it wrong?

As documentation says

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

Well when I make incoming constraints unbounded for inner Container i suppose that it should be as small as possible but it expands for entire screen.

  body: Container(
    color: Colors.red,
    constraints: const BoxConstraints(
      maxHeight: double.infinity,
      maxWidth: double.infinity,
    ),
    child: Container(
      color: Colors.green,
    ),
  ),

Why?enter image description here

Upvotes: 1

Views: 616

Answers (3)

eamirho3ein
eamirho3ein

Reputation: 17940

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 green container with no child get size as much as its parents.

Upvotes: 2

user18709971
user18709971

Reputation:

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.

Upvotes: 0

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,
      ),
    ),
)

Upvotes: 2

Related Questions