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:
- A parent passes constraints to each child: a minimum and maximum width and height (
BoxConstraints). - The child picks its own size — any size within those constraints — and reports it back up.
- 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.
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:
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.
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.
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
RoworColumn— the fastest way to debug overflows.
Test yourself
Complete the rule: constraints go down, sizes go up, and…
- children set their own position
- the parent sets position
- the theme sets position
- positions are random
Why does Container(width: 100) passed straight to runApp fill the whole screen?
- Containers ignore width
- The screen gives tight constraints, so the child must match them
- It's a bug in Flutter
- Because color is set
A TextField inside a Row throws an infinite-width error. What fixes it?
- Wrap the TextField in Expanded
- Wrap the Row in Center
- Set maxLines
- Use a Column