Flutter's Box Constraint System: Parents Rule
In Flutter, parents tell children their size limits, not their size. A widget must obey its parent's constraints (min/max width/height), but it chooses its own size within those bounds. The footgun is trying to size a child widget against its parent's rules.
WHY IT EXISTS: Flutter needs a fast, predictable way to arrange widgets on the screen. A single-pass layout system, where each widget is visited only once, is highly performant. The Box Constraint system provides this by enforcing a strict, one-way flow of layout information from parent to child.
THE MENTAL MODEL: Think of a parent telling a child, "You can play, but you must stay in the yard." The parent defines the boundary (the constraints). The child decides where to play within that yard (the size). Then, the parent decides where the yard itself is located relative to the house (the position). The rule is: constraints go down, sizes go up, parent sets position.
HOW IT WORKS: The layout process is a walk down and up the widget tree. A parent widget passes BoxConstraints (min/max width and height) down to its child. The child then looks at these constraints and its own internal preferences. It chooses a size that satisfies the parent's constraints. This chosen size is passed back up to the parent. Finally, the parent positions the child on the screen.
WHEN TO USE IT: You are always using it in Flutter. Every widget that participates in layout is subject to this system. Understanding it is crucial for debugging common layout issues like "RenderFlex overflowed" or widgets not appearing at the size you expect. It's not optional, it's fundamental.
WHEN NOT TO USE IT: You cannot turn the system off. The question is not when to use it, but how to work with it effectively. For example, if a parent gives tight constraints (like BoxConstraints.tight, which forces an exact size), the child has no freedom. If a parent gives loose constraints, the child can choose its own size. Your job is to pick parent widgets that provide the constraints your child widgets need.
ONE CANONICAL EXAMPLE: A Container with width: 500 is placed inside a Center widget, which is the full screen size (say, 360 wide). The Center tells the Container it can be any size up to 360 pixels wide. The Container wants to be 500 wide, but this violates the constraint. It must obey its parent, so it becomes 360 pixels wide. The parent's constraint always wins. This is a common source of confusion for beginners.
Read the original → docs.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.