Layout and constraints
Flutter layout has a one-sentence rule, and the official docs put it this way: constraints go down, sizes go up, parent sets position.
In practice:
- The parent hands the child a
BoxConstraints: min/max width and height - The child picks its own size within that box and reports it back
- The parent positions the child inside itself
The key consequence: a child cannot know its position, and cannot ask its parent how big it is. That is why "make this widget 30% of the screen" does not work directly — you reach for Expanded, FractionallySizedBox or LayoutBuilder.
Row and Column are Flex widgets: they lay children out along one axis.
mainAxisAlignment— along the main axis (horizontal for Row, vertical for Column)crossAxisAlignment— along the perpendicular axismainAxisSize— whether the Flex takes all available space (max, the default) or only as much as its children need (min)
`Expanded` versus `Flexible` is a frequent interview question: both distribute the remaining space, but Expanded forces the child to fill it (fit: FlexFit.tight), while Flexible allows the child to take less (FlexFit.loose).
| Symptom | Cause | Fix |
|---|---|---|
| A yellow-and-black "RenderFlex overflowed" stripe | Children want more space than the parent offers | Wrap in `Expanded`/`Flexible`, or make it scrollable with `SingleChildScrollView` |
| A ListView inside a Column throws | The Column offers unbounded height and the ListView wants unbounded too | Wrap it in `Expanded`, or set `shrinkWrap: true` |
| A widget is larger than expected | The parent passed tight constraints, leaving the child no choice | Change the box with `Align`, `Center` or `SizedBox` |
Interview tip. Layout questions almost always arrive as "what do you do when you hit an overflow error?". Start with diagnosis: the Layout Explorer in DevTools' Widget Inspector shows which widget received which constraints. Then name the cause (unbounded versus tight constraints) and only then give the fix. Jumping straight to "I'd add an Expanded" sounds like guessing.
📚 Sources and documentation
- Layouts in Flutterofficialdocs.flutter.dev
- Understanding constraintsofficialdocs.flutter.dev
The "constraints go down, sizes go up" rule with 29 worked examples.
- Expanded classofficialapi.flutter.dev
- Flex classofficialapi.flutter.dev