Sparround

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 axis
  • mainAxisSize — 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).

SymptomCauseFix
A yellow-and-black "RenderFlex overflowed" stripeChildren want more space than the parent offersWrap in `Expanded`/`Flexible`, or make it scrollable with `SingleChildScrollView`
A ListView inside a Column throwsThe Column offers unbounded height and the ListView wants unbounded tooWrap it in `Expanded`, or set `shrinkWrap: true`
A widget is larger than expectedThe parent passed tight constraints, leaving the child no choiceChange 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