Sub topic 2
How Flutter Layout Works: Constraints Down, Sizes Up
This chapter assumes basic Dart syntax, StatelessWidget, the build method, and a working Flutter project. The focus is Flutter’s layout contract: constraints go down, sizes go up, and parents decide where children are placed.
The layout contract
A Flutter widget receives a set of box constraints from its parent. A constraint gives minimum and maximum values for width and height. The widget then chooses a size that fits those limits. Finally, the parent uses that size to position the widget.
The process has three parts:
- A parent passes constraints to a child.
- The child chooses its size within those constraints.
- The parent positions the child in its own layout.
A child cannot choose a size outside its constraints. For example, a screen might give a child a maximum width of 400 logical pixels. The child can be 100, 250, or 400 pixels wide, but not 500.
| Constraint type | Meaning |
|---|---|
| Tight | The minimum and maximum are equal, so the size is fixed |
| Loose | The child can choose any size between the limits |
| Unbounded | There is no maximum in that direction |
The phrase “constraints down, sizes up” describes the direction of this negotiation. Constraints travel from ancestors to descendants. Sizes travel back toward ancestors.
Reading constraints with `LayoutBuilder`
LayoutBuilder builds its child using the constraints available at that point in the widget tree. This is useful when a widget must adapt to the space provided by its parent.
The following app displays the maximum width and height received by a Container. ConstrainedBox supplies a maximum width of 300 pixels, even if the device screen is wider.
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(maxWidth: 300),
child: LayoutBuilder(
builder: (context, constraints) {
return Container(
padding: const EdgeInsets.all(16),
color: Colors.blue.shade100,
child: Text(
'Maximum width: ${constraints.maxWidth}\n'
'Maximum height: ${constraints.maxHeight}',
),
);
},
),
),
),
);
}
}Center gives its child loose constraints based on the available screen space. ConstrainedBox narrows the maximum width. The Container chooses a size that fits its text and padding, while Center positions it in the middle.
Why constraints cause errors
Many layout errors happen when a widget needs a finite limit but receives an unbounded constraint. A scrollable widget, such as ListView, needs to know how much space it can occupy in its scrolling direction. A parent that provides no maximum height can therefore cause an unbounded-height error.
Another common problem occurs when content needs more space than its maximum constraint allows. Flutter may report a RenderFlex overflow error and draw a yellow-and-black warning stripe. The fix is usually to change the constraints or make the content flexible, rather than adding arbitrary spacing.
Exercise
A child receives a maximum width of 200 pixels but requests a width of 260 pixels. What must happen?
Show answer
The child must choose a width of 200 pixels or less. If its content cannot fit, use a fitting strategy such as wrapping, truncation, or a scrollable layout.