Sub topic 2
How Flutter Layout Works: Constraints Down, Sizes Up
Assume you know basic Dart, widget trees, Row, Column, Scaffold, and ListView. This chapter focuses on the layout contract between a parent widget and its children.
Flutter layout follows a simple direction:
- Constraints flow down from a parent to a child.
- The child chooses a size within those constraints.
- The child’s size flows back up to the parent.
- The parent positions the child.
A constraint is a set of minimum and maximum limits for width and height. A size is the actual width and height that a widget chooses.
The layout contract
A widget does not usually choose any size it wants. Its parent supplies a BoxConstraints object containing four values:
| Constraint | Meaning |
|---|---|
| `minWidth` and `maxWidth` | The allowed horizontal range |
| `minHeight` and `maxHeight` | The allowed vertical range |
For example, a parent might tell a child:
- Width must be between 0 and 360 pixels.
- Height must be between 0 and 640 pixels.
The child might choose 200 by 100 pixels. The parent then decides where to place that 200-by-100 area.
This explains why a child cannot make itself fill the screen simply by setting a large width. The parent’s maximum constraint still applies.
A working example
ConstrainedBox adds limits around its child. Center gives its child loose constraints inside the available area, while SizedBox requests a particular size.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ConstraintExample()));
}
class ConstraintExample extends StatelessWidget {
const ConstraintExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Constraints')),
body: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 160,
maxWidth: 280,
minHeight: 80,
maxHeight: 140,
),
child: Container(
color: Colors.blue,
alignment: Alignment.center,
child: const Text(
'I must fit inside the constraints',
style: TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
),
),
),
);
}
}The Container chooses a size that satisfies the limits. Its text may affect the preferred size, but the result cannot be narrower than 160 pixels, wider than 280 pixels, shorter than 80 pixels, or taller than 140 pixels.
Why errors happen
Many layout errors come from incompatible constraints.
A Column gives children a bounded width but may give them unbounded height when it is inside a scrollable widget. An unbounded constraint means there is no finite maximum. A child that tries to expand to all available height cannot do that when “all available height” has no limit.
Common examples include:
- A
ListViewinside aColumnwithout a bounded height. - An
Expandedwidget inside a vertically scrollingListView. - A
Rowwhose child needs more width than the row receives.
Use Expanded, Flexible, or an explicit height only when the surrounding parent provides a finite constraint. Use scrolling widgets when content can exceed the available space.
> callout warning > When Flutter reports an overflow or an unbounded constraint error, inspect the nearest Row, Column, or scrollable ancestor. Ask which widget should provide the missing maximum size.
Reading layout problems
When debugging, inspect the layout from the outside inward:
- Identify the widget that reports the error.
- Check the constraints its parent provides.
- Check whether a child is demanding more space than those constraints allow.
- Decide whether to shrink, flex, wrap, or scroll the content.
This model is more reliable than adding arbitrary SizedBox values. Fixed sizes can hide the problem at one window size and create another problem at a different size.
Exercise
A child receives a maximum width of 300 pixels but requests 500 pixels. What should you inspect first?
Show answer
Inspect the parent and the child’s layout behavior. The child must choose a width within the received constraints, or the design must provide wrapping, scrolling, or a flexible layout.