Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 73 ยท Layout

Constraints Go Down, Sizes Go Up

Understand Flutter's one layout rule: parents pass constraints down, children pick a size and report it up, parents set the position. Fix unbounded errors.

Almost every Flutter layout surprise has the same cause: you expected a widget to take the size you gave it, and it did not. A Container with width: 100 fills the whole screen. A ListView inside a Column crashes with a long red error. A Text refuses to wrap. All of these follow from one short rule, and once you know it, layout stops being guesswork.

The rule is: constraints go down, sizes go up, parents set the position. This lesson explains each part, shows the difference between tight and loose constraints, explains the dreaded "unbounded" errors, and gives you a small JavaScript layout engine to experiment with. It builds on the render objects from Widget, Element and RenderObject trees.

Real vs stand-in. BoxConstraints, RenderBox and the widgets named here are real Flutter. This site cannot run Flutter, so Dart code is read-only and the live examples are JavaScript stand-ins that apply the same clamping rule to a tiny widget tree.

One rule, three steps

Constraints down The parent says how big you may be.
Size up The child picks a size inside that range.
Parent positions The child never chooses its own spot.
1 / 2

The layout rule step by step

Every box in Flutter is laid out in a single conversation with its parent:

  1. The parent gives the child a BoxConstraints: a minimum and maximum width, and a minimum and maximum height.
  2. The child decides its own size, but it must stay inside those constraints. It may ask its own children first, passing them constraints of its own.
  3. The child reports its size back up. The parent now knows it and decides the child's position.
sequenceDiagram
  participant P as Parent
  participant C as Child
  P->>C: constraints (min and max width and height)
  C->>C: pick a size within the constraints
  C-->>P: size (width and height)
  P->>P: choose the child's position

Constraints travel down the tree, sizes travel back up, and the parent places each child.

This explains three facts that confuse beginners. A widget does not know where it is on screen, because the parent positions it. A widget cannot choose any size it likes, because the parent limits it. And a widget's size often depends on the whole tree above it, because constraints are handed down from the root.

Tight and loose constraints

A constraint box is written like this:

DART
const tight = BoxConstraints.tightFor(width: 200, height: 100);
// minWidth = maxWidth = 200, minHeight = maxHeight = 100

const loose = BoxConstraints(maxWidth: 200, maxHeight: 100);
// minWidth = 0, maxWidth = 200, minHeight = 0, maxHeight = 100

With tight constraints the child has no choice: the only legal size is the min/max value. With loose constraints the child may be anywhere from zero up to the maximum, so it usually picks the size that fits its content (a Text takes the size of its words, for example) or the largest size allowed (a Container with no child tries to fill).

The root of every app receives tight constraints equal to the screen size. That single fact explains the most famous layout puzzle:

DART
// Looks like a 100 x 100 box. It is not.
Container(width: 100, height: 100, color: Colors.teal)

If this container is the direct child of something that passes tight constraints, such as the screen root, it must be exactly screen sized. Its request for 100 is silently overruled, because a child may only pick a size inside the constraints it was given. The fix is to put a widget between them that loosens the constraints:

DART
Center(
  child: Container(width: 100, height: 100, color: Colors.teal),
)

Center (and Align) fills the space it is given, but passes loose constraints to its child, so the child may now be 100 by 100. Center then positions it in the middle. SizedBox, ConstrainedBox and UnconstrainedBox manipulate constraints in other ways; you will meet them in Padding, SizedBox, Container and ConstrainedBox.

Computing a layout yourself

The next stand-in implements the rule. A widget is a function layout(constraints) that returns a size. Try changing the numbers and predicting the output before you run it.

JS Clamp sizes into constraints

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Line 1 prints 360x640: the 100 by 100 request is clamped into the tight screen constraints. Line 2 shows that Center fixes it. Line 3 shows that even inside a Center a child cannot be wider than the parent's maximum: it is capped at 360. Line 4 shows a Text whose natural width of 140 is clamped to 100; in real Flutter a Text would wrap onto a second line instead, because text layout can trade width for height.

Why a child "ignores" its size

When a size you set seems ignored, ask two questions: what constraints is my parent giving me? and are they tight? Common cases:

  • A Container with a width in a Column is fine (a Column passes loose width constraints on the cross axis, up to its own width). The same container in a Scaffold body is tight, so the width is ignored.
  • A child of Expanded is forced to the exact size the parent computed (a tight constraint along the main axis), so its own width is ignored. See Row, Column, Expanded and Flexible.
  • A Container with no child and no size expands to the biggest size allowed; with a child it wraps the child.

Use Flutter DevTools' Widget Inspector with the layout explorer to see the actual constraints of any widget. You will use it again in Performance and DevTools.

Unbounded constraints and the red errors

A maximum can also be infinite (double.infinity). That is called unbounded, and it is legal, but widgets that want to "fill all the space" cannot do that with an infinite maximum. Scrollable widgets create unbounded constraints in their scroll direction, because content may be taller than the screen. If you put a widget that needs a finite size into that space, you see errors like:

  • Vertical viewport was given unbounded height. (a ListView inside a Column)
  • RenderFlex children have non-zero flex but incoming height constraints are unbounded. (an Expanded inside a scrollable Column)
  • BoxConstraints forces an infinite width.
DART
// Error: Column gives its children unbounded height,
// and ListView wants to be as tall as it can.
Column(
  children: [
    const Text('Messages'),
    ListView(children: [/* ... */]),
  ],
)

// Fix: let Expanded hand the ListView a finite, tight height.
Column(
  children: [
    const Text('Messages'),
    Expanded(child: ListView(children: [/* ... */])),
  ],
)

The fix is always the same idea: give the greedy widget a bounded size with Expanded, SizedBox, ConstrainedBox, or shrinkWrap: true for short lists. Next stand-in shows the problem as numbers.

JS Why unbounded height breaks a fill-it widget

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Tips for debugging layout

  • Read the error. Flutter prints the widget chain and the constraints it received.
  • Wrap suspects in ColoredBox or DecoratedBox temporarily to see their real size.
  • Remember who passes what: Center/Align loosen, SizedBox/Expanded tighten, Column/Row pass unbounded constraints along their main axis, scrollables pass unbounded in the scroll direction.
  • Prefer flexible layouts (Expanded, Flexible, LayoutBuilder) over hard-coded numbers, so the same code works from a small phone to a tablet. See Responsive and adaptive layouts.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a child do with the constraints it receives from its parent?

    The child must choose a size within the constraints. The parent, not the child, decides the position.
  2. 2. A Container(width: 100) is the direct child of the screen root and fills the whole screen. Why?

    Tight constraints leave a single legal size. Wrapping the container in Center passes loose constraints so 100 is allowed.
  3. 3. Which widget turns tight constraints into loose constraints for its child?

    Center and Align fill their parent but let the child be any size up to the maximum.
  4. 4. You put a ListView directly inside a Column and get 'Vertical viewport was given unbounded height'. What is the usual fix?

    Column gives unbounded height. Expanded passes a finite, tight height so the ListView knows how tall to be.

Summary

  • Layout rule: constraints go down, sizes go up, the parent sets the position.
  • Tight constraints allow one size; loose constraints allow a range starting at zero.
  • The root is tight, so use Center/Align to let a sized child be its real size.
  • Unbounded constraints (from scrollables and from Row/Column along their main axis) break widgets that try to fill; give them a finite size.
  • When a size is ignored, ask what constraints the parent passes.

Next: Row, Column, Expanded and Flexible