TExcelTAD
Home › Flutter › Layout Constraints

Flutter Layout Constraints

Sooner or later every Flutter developer sees a yellow-and-black striped overflow warning or an "unbounded height" error. These almost always come from one rule. Once you understand it, Flutter layout stops being mysterious:

Constraints go down. Sizes go up. Parent sets position.

The layout protocol

Layout happens in one pass down and up the widget tree:

  1. A parent passes constraints to each child: a minimum and maximum width and height (BoxConstraints).
  2. The child picks its own size — any size within those constraints — and reports it back up.
  3. The parent decides where to place the child.

A widget therefore cannot choose any size it likes: it can only choose within what its parent allows. And it does not know its own position on screen.

Tight vs loose constraints

Kind Meaning Example
Tight min = max, the child has exactly one choice The screen gives the root widget its full size
Loose min = 0, the child can be anything up to max Center loosens constraints for its child
Unbounded max = infinity Children of a ListView in its scroll direction, children of Row/Column on the main axis

This explains a classic surprise: a Container with width: 100 at the root of the app still fills the whole screen, because the screen's constraints are tight.

flutter · try it yourself
void main() {
  runApp(
    Container(width: 100, height: 100, color: Colors.red), // fills the screen!
  );
}

Wrap it in Center and the constraints become loose, so the 100×100 size is respected:

flutter · try it yourself
void main() {
  runApp(
    Center(
      child: Container(width: 100, height: 100, color: Colors.red),
    ),
  );
}

Seeing constraints with LayoutBuilder

LayoutBuilder gives you the constraints your widget received, so you can print them or build differently depending on the available space.

flutter · try it yourself
Center(
  child: SizedBox(
    width: 300,
    child: LayoutBuilder(
      builder: (context, constraints) {
        debugPrint('$constraints');
        return Text('Max width: ${constraints.maxWidth}');
      },
    ),
  ),
)
BoxConstraints(w=300.0, 0.0<=h<=640.0)

On a 360×640 phone, Center passes loose constraints (0–360 wide, 0–640 high). SizedBox then tightens only the width to exactly 300, so the LayoutBuilder receives w=300 and a height anywhere from 0 to 640. Always look at what your widget actually receives — it depends entirely on the parents above it.

Common errors and their fixes

1. RenderFlex overflowed — a Row's children are wider than the screen. A Row gives its children unbounded width, so a long Text doesn't wrap.

flutter · try it yourself
Row(
  children: [
    const Icon(Icons.info),
    // Without Expanded the text would overflow the screen.
    Expanded(
      child: Text('This is a very long message that needs to wrap onto '
          'several lines instead of overflowing.'),
    ),
  ],
)

2. Vertical viewport was given unbounded height — a ListView inside a Column. Both want infinite height. Fix: wrap the list in Expanded, or give it a fixed height with SizedBox.

3. BoxConstraints forces an infinite width — e.g. a TextField inside a Row. The field wants to be as wide as possible, but the row offers infinity. Fix: wrap it in Expanded or Flexible.

Tools for shaping constraints

Widget Effect on its child
SizedBox(width:, height:) Tight size
ConstrainedBox(constraints:) Adds extra min/max limits
Center / Align Loosens constraints
Expanded / Flexible Gives a share of the leftover space in a Row/Column
UnconstrainedBox Removes constraints (may overflow)
FittedBox Scales the child down to fit
AspectRatio Picks a size with a given width/height ratio

Tip: In Flutter DevTools, the Layout Explorer shows the constraints and sizes of every widget in a Row or Column — the fastest way to debug overflows.

Test yourself

Complete the rule: constraints go down, sizes go up, and…

  1. children set their own position
  2. the parent sets position
  3. the theme sets position
  4. positions are random

Why does Container(width: 100) passed straight to runApp fill the whole screen?

  1. Containers ignore width
  2. The screen gives tight constraints, so the child must match them
  3. It's a bug in Flutter
  4. Because color is set

A TextField inside a Row throws an infinite-width error. What fixes it?

  1. Wrap the TextField in Expanded
  2. Wrap the Row in Center
  3. Set maxLines
  4. Use a Column