Sub topic 15
Inspecting layouts with Flutter DevTools
This chapter assumes you can build Row, Column, and Stack layouts, and that you know the rule "constraints go down, sizes go up." It also assumes you have seen a RenderFlex overflow error. Here you will learn to inspect a running layout with Flutter DevTools instead of guessing at the cause.
Why inspect instead of guess
Most layout bugs come from a mismatch between the constraints a parent passes down and the size a child wants. The source code shows what you asked for. It does not show the constraints the parent actually passed or the size the child actually got. DevTools shows both.
The tool you need is the Flutter Inspector. It shows the live widget tree of the running app. It also shows the render objects behind the widgets, along with their constraints and sizes.
Opening DevTools
You can reach the Inspector in three common ways:
- In VS Code, run the app in debug mode and open the Flutter Inspector from the Debug sidebar or the command palette (Flutter: Open DevTools).
- In Android Studio or IntelliJ, open the Flutter Inspector tool window.
- From the terminal, run
flutter run, then open the DevTools URL that the command prints.
DevTools works in debug mode and in profile mode. Use debug mode for layout work, because the debug-only overflow stripes and the inspector's layout tools depend on it.
A layout with a deliberate bug
Use this app to follow along. The title text is too wide for the Row, so the Row overflows.
import 'package:flutter/material.dart';
void main() => runApp(const InspectDemo());
class InspectDemo extends StatelessWidget {
const InspectDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Inspect me')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
const Text(
'A very long message that would otherwise overflow the Row',
),
],
),
const SizedBox(height: 16),
SizedBox(
width: 200,
height: 200,
child: Stack(
alignment: Alignment.center,
children: [
Container(width: 120, height: 120, color: Colors.blue),
const Text('Hi'),
],
),
),
],
),
),
),
);
}
}Run it on a narrow device or window. You will see yellow and black stripes on the right edge of the Row, and the debug console prints a "RenderFlex overflowed" message with a pixel count.
The parts of the Inspector
The Inspector has a few areas that matter for layout work.
| Area | What it shows | Use it to |
|---|---|---|
| Widget tree | The hierarchy of widgets in your app | Find the widget you care about and see its parents |
| Details tree | Properties and render object details for the selected widget | Read constraints, size, and flex values |
| Layout Explorer | A diagram of a selected Row, Column, or Flex child | Change alignment and flex values live |
| Select widget mode | A toggle that lets you tap a widget in the running app | Jump to a widget in the tree from the screen |
| Debug paint | A toggle that draws layout outlines on the running app | See bounds, padding, and alignment |
Select widget mode
Turn on Select Widget Mode, then tap the overflowing text in the running app. The Inspector selects the matching Text widget in the tree. Its parent chain shows the layout problem: Text sits directly inside Row, and the Row sits inside Column.
By default the tree shows only widgets created in your own code, which hides the many framework widgets underneath. A setting lets you show all widgets when you need to see the framework internals, such as the RichText and RenderFlex objects behind a Text and a Row.
Reading constraints and sizes
Select the Row and open the Render Object details. You will see a line like this:
constraints: BoxConstraints(0.0<=w<=358.0, 0.0<=h<=Infinity)
size: Size(358.0, 20.0)Read it using the rule from earlier chapters:
0.0<=w<=358.0means the parent allows any width up to 358. The width is bounded.0.0<=h<=Infinitymeans the height is unbounded. A Column gives its children unbounded height along its main axis.sizeis what the Row chose within those constraints.
The Row's children are laid out without a width limit on their main axis, because a Row gives non-flexible children unbounded width. The Text asks for its natural width, which is larger than the 358 pixels the Row allows. The Row cannot shrink the child, so it reports an overflow.
If you see Infinity in a constraint where you expected a number, that is the signature of an unbounded-constraint error. For example, a ListView directly inside a Column gets 0.0<=h<=Infinity, and a scrollable has no height limit to size against.
Using the Layout Explorer
Select the Row and open the Layout Explorer tab. It draws the Row with its children and labels each child with its size and flex factor.
You can do three things there without editing code:
- Change MainAxisAlignment and CrossAxisAlignment from the dropdowns and watch the children move.
- Change MainAxisSize to see how the Row shrinks or fills its parent.
- Select a child and set its flex value or its fit (
tightfor Expanded,loosefor Flexible).
Setting the Text's flex to 1 with a tight fit gives it a bounded width. The overflow stripes disappear in the running app. This tells you what the fix is, but the change lives only in the running app. Nothing is written to your source file.
Apply the fix yourself by wrapping the Text in Expanded:
Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
Expanded(
child: Text(
'A very long message that would otherwise overflow the Row',
overflow: TextOverflow.ellipsis,
),
),
],
)Hot reload, then reselect the Text. Its constraints now read a bounded width, such as 358-24=334, which is the Row's width minus the icon and the gap. The Text truncates with an ellipsis instead of overflowing.
Debug paint and other visual aids
The Inspector has toggles that draw extra information on the running app. Turn these on while you work.
- Debug Paint draws the bounds of every render box. It also draws padding as shaded areas and shows the main axis and cross axis of flex widgets with arrows.
- Paint Baselines draws the text baselines. Use it when
CrossAxisAlignment.baselinedoes not line up as you expect. - Highlight Repaints flashes a colored border around areas that repaint. It matters for performance work more than for layout.
- Slow Animations slows animations down, which helps when a layout changes mid-animation.
You can also turn on Debug Paint from code:
import 'package:flutter/rendering.dart';
void main() {
debugPaintSizeEnabled = true;
runApp(const InspectDemo());
}This is useful in a quick experiment. Remove it before you commit, because it changes how every screen looks.
Diagnosing common problems
The table links each symptom to what to check in the Inspector.
| Symptom | What to check | Typical fix |
|---|---|---|
| Yellow and black stripes on a Row | Select the overflowing child. Is its width unbounded on the main axis? | Wrap it in `Expanded` or `Flexible`, or use `Wrap` |
| "Vertical viewport was given unbounded height" | Select the `ListView`. Does its parent pass `h<=Infinity`? | Wrap the `ListView` in `Expanded`, or give it a fixed height |
| Widget does not appear at all | Select it. Is its size `0.0 x 0.0`? | Give it a size, or check for a zero-sized parent |
| Stack child in the wrong place | Select it. Is its parent a `Positioned`, or is it using the Stack `alignment`? | Add `Positioned` or change `alignment` |
| Stack is smaller than expected | Select the Stack. Which child sets its size? | Give the Stack a `SizedBox` parent, or use `Positioned.fill` |
| Row takes the full width when you wanted it small | Check `MainAxisSize` in the details | Set `mainAxisSize: MainAxisSize.min` |
Inspecting a Stack
Select the Stack from the demo. The Layout Explorer does not draw Stacks, so rely on the Details tree and Debug Paint instead. In Debug Paint, the 200 by 200 SizedBox appears as a box. The blue Container is centered inside it because of alignment: Alignment.center. The Text is also centered, and it paints above the Container because later children are painted last.
To check the draw order, look at the Stack's children in the widget tree. The order from top to bottom is the order of painting, so the last child is the front-most.
A debugging workflow
Follow these steps when a layout looks wrong.
- Read the console message. It names the widget and the pixel overflow.
- Turn on Select Widget Mode and tap the widget that looks wrong.
- Walk up the parent chain to the nearest Row, Column, Stack, or scrollable.
- Read the constraints on the parent and on the child. Look for
Infinityand for tight constraints where the min equals the max. - Try a fix in the Layout Explorer if the parent is a flex widget.
- Make the change in code and hot reload.
- Reselect the widget and confirm that the constraints and sizes match what you intended.
A diagram of the workflow
Edge cases to know
- Debug mode only. Overflow stripes and the debug console messages do not appear in release builds. A layout that overflows silently in release can still clip content.
- Different screens, different constraints. A layout that fits on your phone can overflow on a small device or with a larger system font. Use the device preview or resize the window and recheck the constraints.
- Tight constraints. If a parent passes a tight constraint (min equals max), the child cannot choose its own size. A
Container(width: 50)inside such a parent still gets the parent's size. Look for equal min and max values in the details. - Widgets you did not write. Framework widgets such as
Scaffold,AppBar, andListTileadd their own constraints. Show all widgets in the tree when you need to see which one imposes a limit.
Exercise
In the Inspector you select a ListView inside a Column and see constraints: BoxConstraints(0.0<=w<=360.0, 0.0<=h<=Infinity). The app reports a "Vertical viewport was given unbounded height" error. What does the constraint tell you, and how do you fix it?
Show answer
The h<=Infinity part shows that the Column gives its child unbounded height on the main axis. A scrollable needs a finite height to know where to scroll. Wrap the ListView in Expanded so it gets the Column's remaining height, or put it in a SizedBox with a fixed height. After the fix, reselect the ListView and confirm that the maximum height is a number.
Exercise
You use the Layout Explorer to set a Row child's flex to 1 with a tight fit, and the overflow stripes disappear. After a hot restart the overflow returns. Why, and what do you do?
Show answer
The Layout Explorer changes the running widget only and does not edit your source. A hot restart rebuilds the app from the original code. Wrap the child in Expanded (or Flexible with fit: FlexFit.tight) in the source, then hot reload.
Exercise
A blue Container and a Text('Hi') are children of a Stack with alignment: Alignment.center, but the text is hidden. Using the Inspector, what do you check first?
Show answer
Check the order of the Stack's children in the widget tree. Later children paint on top of earlier ones, so if the Text comes before the Container, the Container covers it. Move the Text after the Container.