Sub topic 4
Fixing layout problems and overflow errors
This chapter assumes you can build simple Flutter apps, read a build method, and run an app on a device or emulator. You should also have DevTools connected to a running app, as covered earlier in the tutorial, and know how to open the Widget Inspector.
Why layout errors happen
Most Flutter layout errors come from one rule: constraints go down, sizes go up, and the parent sets the position. A parent hands each child a minimum and maximum width and height, which are the constraints. The child picks a size inside those limits and reports it back. The parent then places the child.
Errors appear when a child cannot satisfy its constraints, or when it is given constraints it cannot work with. The most common cases are:
- A
RoworColumnhas children that are larger than the space available along its main axis. Flutter paints a yellow and black striped bar and logs aRenderFlex overflowederror. - A widget that wants to fill all available space sits inside a parent that gives it unbounded space. Flutter reports
unbounded heightorunbounded width. - An
ExpandedorFlexiblewidget is placed somewhere other than directly inside aRow,Column, orFlex. Flutter reports an incorrect use ofParentDataWidget.
These messages name the offending widget and often include the constraints it received. Read them before reaching for a tool.
Reading constraints
A constraint has four numbers: minimum width, maximum width, minimum height, and maximum height. Three shapes matter in practice.
| Constraint type | Meaning | Typical source |
|---|---|---|
| Tight | Minimum equals maximum, so the child has exactly one allowed size | `SizedBox`, or the root of the app |
| Loose | Minimum is zero, so the child may be anything up to the maximum | `Center`, `Align`, and most single-child parents |
| Unbounded | Maximum is infinity along one axis | The main axis of a `Row`, `Column`, or scrollable list |
A Row gives its children unbounded width. Each child can measure itself at its natural width, and the Row then adds those widths. If the sum is larger than the Row's own maximum width, the Row overflows. A Text widget will happily ask for one very long line, because nothing told it to wrap.
Finding the problem with the Widget Inspector
When you see the striped overflow bar, switch to DevTools and open the Widget Inspector. The workflow is short:
- Turn on Select widget mode and tap the broken area in the running app. The tree view jumps to the matching widget.
- Look for the widget the error message named, usually a
Row,Column, orFlex. Widgets with problems are marked in the tree. - With the
RoworColumnselected, open the Layout Explorer tab. It draws the widget and its children, and shows the main axis, the cross axis, and each child's size. - Select a child inside the Layout Explorer to see the constraints it received and the size it chose.
The Layout Explorer is useful because it lets you change a flex layout live. You can switch mainAxisAlignment, crossAxisAlignment, and the flex factor of children, and the running app updates. The changes are temporary and apply only to the running session. After you find a setting that works, copy it into your source code.
Also check Show debug paint in the Inspector settings. It draws baselines and layout boundaries, which helps when padding or alignment looks wrong but nothing is overflowing.
Fixing a RenderFlex overflow
Consider this broken widget:
import 'package:flutter/material.dart';
class BrokenRow extends StatelessWidget {
const BrokenRow({super.key});
@override
Widget build(BuildContext context) {
return const Row(
children: [
Icon(Icons.info),
Text(
'A very long message that does not fit on one line of a narrow screen.',
),
],
);
}
}The Row offers unbounded width, so the Text measures itself as one long line. The line is wider than the screen, and the Row reports an overflow. The Layout Explorer shows the Text child with a width larger than the Row itself.
The usual fix is to give the long child a bounded width. Expanded does that: it makes the child fill the leftover space on the main axis, so the text wraps inside it.
import 'package:flutter/material.dart';
class OverflowDemo extends StatelessWidget {
const OverflowDemo({super.key});
@override
Widget build(BuildContext context) {
return const Row(
children: [
Icon(Icons.info),
// Without Expanded, this long text overflows the Row and Flutter reports a RenderFlex overflow.
Expanded(
child: Text(
'A very long message that does not fit on one line of a narrow screen.',
),
),
],
);
}
}After the change, select the Text again in the Layout Explorer. Its width is now bounded by the space left after the icon, and the striped bar is gone.
Choosing between Expanded, Flexible, and other fixes
Expanded is not always the right answer. Pick the fix that matches what you want the UI to do.
| Fix | Behavior | Use when |
|---|---|---|
| `Expanded` | Child is forced to fill the remaining main-axis space | Text or content should take all leftover room |
| `Flexible` | Child may be smaller than the remaining space, but never larger | Content should shrink only if needed |
| `Text` with `overflow: TextOverflow.ellipsis` and `maxLines` | Text is cut off with an ellipsis | Truncation is acceptable. Pair it with `Expanded` or `Flexible`, because it needs bounded width to know where to cut |
| `Wrap` | Children flow onto new lines | Chips, tags, and buttons that can stack |
| `SingleChildScrollView` | Content scrolls | The content is truly longer than the screen, such as a form on a small phone |
Do not wrap a Row in SingleChildScrollView just to silence the overflow bar. That hides the symptom, and an Expanded inside a horizontally scrolling Row will then fail, because the width is unbounded.
Unbounded height and width errors
The second common family of errors is Vertical viewport was given unbounded height, or its width equivalent. It happens when a scrollable widget such as ListView sits inside a Column. The Column gives its children unbounded height, and the ListView wants to be as tall as its content allows, which here is infinite.
import 'package:flutter/material.dart';
class ListInColumn extends StatelessWidget {
const ListInColumn({super.key});
@override
Widget build(BuildContext context) {
return Column(
children: [
const Text('Results'),
// Expanded gives the ListView the remaining height, which is finite.
Expanded(
child: ListView.builder(
itemCount: 50,
itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
),
),
],
);
}
}In the Inspector, select the ListView and check its constraints. A maximum height of infinity confirms the diagnosis. Wrapping it in Expanded replaces infinity with a real number.
You may see advice to set shrinkWrap: true instead. That makes the list size itself to its content, which also removes the error. The cost is that the list builds every item at once instead of lazily, so it can become slow for long lists. Use shrinkWrap for short lists only.
Other constraint problems to recognize
- A widget has zero size or disappears. A
Containerwith no child and no size fills its parent, but inside an unbounded parent it collapses to zero. Select it in the Inspector and read its size. - A widget will not become the size you set. If a parent passes tight constraints, the child's own
widthandheightare ignored. For example, aContainer(width: 50)directly inside aSizedBox.expandstill fills the parent. Wrap it inCenterorAlignto loosen the constraints. Expandedoutside a flex parent. The error namesParentDataWidget. Move theExpandedso it is a direct child of aRow,Column, orFlex. Wrapping it in aPaddingorContainerin between breaks the link.- Overflow only on some devices. The text scale factor and screen width change between devices. Test with a large system font size, and with a narrow window, before you decide a layout is done.
Checking your fix
A layout fix is not finished until you have confirmed it the same way you found the problem.
- Hot reload the change, then check that the striped bar and the console error are gone.
- Select the repaired widget in the Layout Explorer and confirm the sizes are what you intended.
- Resize the window, or switch to a smaller device, to confirm the layout still holds at the narrowest width you support.
- Raise the system font size and check again.
Exercise
A screen has a Column containing a Text header and a ListView of 200 items. The app throws "Vertical viewport was given unbounded height". Using the Widget Inspector, what would you see on the ListView, and what is the best fix?
Show answer
The ListView's constraints show a maximum height of infinity, because the Column passes unbounded height to its children. The best fix is to wrap the ListView in Expanded so it receives the finite remaining height and still builds items lazily. Using shrinkWrap: true also removes the error, but it builds all 200 items at once and is slower.
Exercise
A Row holds an Icon and a long Text. You wrap the Text in Expanded and the overflow disappears, but the text now wraps onto three lines and you want a single line. What do you add, and why must Expanded stay?
Show answer
Add maxLines: 1 and overflow: TextOverflow.ellipsis to the Text. Expanded must stay because it gives the Text a bounded width. Without a bounded width the Text has no point at which to cut the line, and the Row overflows again.