Sub topic 3
Exploring the widget tree with the Widget Inspector
This chapter assumes you can build Flutter apps with StatelessWidget and StatefulWidget, run them on an emulator or device, and open DevTools as described in the earlier chapters. You should also know what build returns and why widgets nest.
The Widget Inspector shows the live widget tree of your running app, lets you click a widget on the device and see where it sits in that tree, and shows the properties Flutter holds for it. Most UI debugging starts here. You find the widget that draws the wrong thing, then check its parents, its properties, and the source line that created it.
What the inspector shows
Flutter keeps three related trees at runtime.
- Widget tree: the immutable configuration objects your
buildmethods return. Widgets are cheap and are recreated often. - Element tree: the long-lived objects that connect each widget to its place in the hierarchy. An element holds the state and decides whether an existing render object can be reused.
- Render tree: the render objects that perform layout and painting.
The inspector presents the widget tree, but it gets its data from the element and render objects behind it. That is why it can show you a widget's size and constraints, which exist only on the render object.
Opening the inspector
Run the app in debug mode, which is the default for flutter run. Then open DevTools in one of three ways:
- Press the DevTools link that
flutter runprints in the terminal. - Use the Flutter sidebar or the "Open DevTools" command in VS Code or Android Studio.
- Start
dart devtoolsand paste in the VM service URI.
Choose the Inspector tab. Two details matter here:
- The inspector depends on debug-only service extensions. In profile mode, the app is built without the widget creation tracking that supports many of its features, and in release mode no VM service is available at all. Use debug mode for this tool.
- The inspector reflects the running app, not your source files. After a hot reload the tree updates on its own. After a hot restart, or if the tree looks stale, press Refresh Tree.
A sample app to explore
The following app has a few nested custom widgets. Run it and follow along in the inspector.
import 'package:flutter/material.dart';
void main() => runApp(const InspectorDemo());
class InspectorDemo extends StatelessWidget {
const InspectorDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Inspector demo')),
body: const Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CounterBadge(label: 'Inbox', count: 3),
SizedBox(height: 12),
CounterBadge(label: 'Drafts', count: 0),
],
),
),
),
);
}
}
class CounterBadge extends StatelessWidget {
const CounterBadge({super.key, required this.label, required this.count});
final String label;
final int count;
// Adds our own fields to the Properties pane in the inspector.
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(StringProperty('label', label))
..add(IntProperty('count', count));
}
@override
Widget build(BuildContext context) {
return Row(
children: [
Text(label),
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: count == 0 ? Colors.grey : Colors.red,
borderRadius: BorderRadius.circular(12),
),
child: Text('$count', style: const TextStyle(color: Colors.white)),
),
],
);
}
}The main panes
The layout of the Inspector tab changes slightly between DevTools releases, but it has the same parts.
| Part | What it does |
|---|---|
| Toolbar | Holds **Select Widget Mode**, **Refresh Tree**, and the toggles for overlays and slow animations. |
| Widget tree (summary tree) | Lists the widgets created by your own code, plus the main framework ancestors. |
| Details tree | Shows the full subtree below the selected widget, including widgets that the framework creates internally. |
| Layout Explorer tab | Shows the selected widget's size, constraints, and flex settings. The next chapter covers it. |
| Properties tab | Lists the selected widget's diagnostic properties, such as `padding`, `color`, or your own `debugFillProperties` entries. |
The summary tree is the default because the full tree is mostly framework internals. A single Text expands into several framework widgets. When you need to see those, use the details tree or the option to show implementation widgets, if your DevTools version offers one.
Selecting a widget
There are two ways to select a widget.
- Click it in the tree. The widget is highlighted on the device, and the Properties tab updates.
- Turn on Select Widget Mode and tap it on the device. This is faster when you know what you are looking at but not which widget produced it. The inspector selects the deepest widget under your tap, and the tree scrolls to it.
In the sample app, tap the red "3" badge. The selected node is a Text. Its parents, going up, are Container, Row, your CounterBadge, Column, and so on up to MaterialApp. Reading that chain upward answers questions such as "which ancestor is adding this padding?" or "why does this text inherit that style?"
Select Widget Mode picks the innermost widget, which is often a framework widget like RichText. To get to your own widget, climb the tree until you reach a node that comes from your code.
Jumping to source
When you select a widget that your code created, the inspector can open the line that created it in your IDE. This depends on widget creation tracking, which flutter run enables in debug mode. Flutter stores the file and line where each widget was constructed, and the inspector uses that location to filter the summary tree.
If the tree shows only framework widgets, or the widgets you wrote seem to be missing, check that you did not pass --no-track-widget-creation.
# Debug mode with creation tracking (the default)
flutter run
# This disables creation tracking and weakens the inspector
flutter run --no-track-widget-creationReading the Properties tab
Select a Container in the sample app. The Properties tab lists padding, decoration, and color as the framework sees them at runtime. This is more reliable than reading the code when the values are computed, such as the count == 0 ? Colors.grey : Colors.red expression. The inspector shows the result for the running instance.
Select a CounterBadge. Because the class overrides debugFillProperties, the tab also shows label and count. Without that override, a custom widget's fields do not appear. Adding the override is a small cost, and it is worth it for widgets that take configuration you often need to confirm at runtime. The StringProperty and IntProperty classes are two of several; others handle doubles, enums, flags, and nested diagnosable objects.
Overlay toggles worth knowing
The toolbar has toggles that draw on the device. They cost nothing to try and each answers a specific question.
- Slow Animations: runs animations at a fraction of normal speed, so you can see what a transition does.
- Show Guidelines: draws lines around render boxes, padding, and alignment, which helps with spacing questions.
- Show Baselines: draws text baselines, which helps when a
Rowof mixed text sizes looks misaligned. - Highlight Repaints: flashes a colored border around layers when they repaint. Use it to find areas that repaint on every frame.
- Highlight Oversized Images: marks images that are decoded at a much larger size than they are displayed at.
Some of these are performance tools. Highlight Repaints, for example, is most useful once you are chasing jank, which a later chapter covers. Overlays change what is drawn, so turn them off before you take measurements.
Inspecting from code
The inspector shows a snapshot on demand, and sometimes you want a text copy of the tree, for a bug report or a test. Flutter can print it:
import 'package:flutter/widgets.dart';
void dumpTree() {
// Prints the widget tree to the console. Call it in debug mode.
debugDumpApp();
}debugDumpApp() prints the widget hierarchy, and debugDumpRenderTree() prints the render tree with constraints and sizes. Both write to the console, not to DevTools. They are useful in environments where you cannot open DevTools, such as a CI log or a remote device.
Common problems
The tree is empty or stale. Press Refresh Tree. If it is still empty, check that DevTools is connected to the right app. With several running sessions, it is easy to inspect the wrong one.
Select Widget Mode picks the wrong widget. The tap lands on the deepest widget at that point, which may be a decoration or a gesture detector the framework added. Select the closest ancestor you recognize instead.
A widget you expect is not in the tree. The widget may not have been built. For example, a ListView builds only the items near the viewport, and a Visibility widget with maintainState off removes its child's subtree. The tree shows what exists now, not what could exist.
Properties are missing. Custom widgets expose only what debugFillProperties reports, and some framework widgets expose less than you expect.
It works in debug but you cannot inspect your release build. This is by design. Reproduce the issue in debug mode, and use profile mode only for performance questions.
A short worked session
Suppose the "Drafts" badge looks red in your real app, but it should be grey.
- Turn on Select Widget Mode and tap the badge.
- Climb from the
Textto itsContainer. Open the Properties tab and read thedecorationcolor. - Climb to the owning
CounterBadge. Readcountin the properties added throughdebugFillProperties. - If
countis0but the color is red, the bug is in the decoration logic. Ifcountis not0, the bug is upstream, where the value is produced. Use the tree's parent chain to find the widget that passed it down.
Each step takes a few seconds in the inspector, and the answer comes from the running app instead of from guessing at the code.
Exercise
In the sample app, you select the red "3" badge with Select Widget Mode and the Properties tab shows a Text, but you need to know the label that was passed to the CounterBadge containing it. What do you do, and what must the CounterBadge class contain for the value to appear?
Show answer
Climb the tree from the selected Text to its parent CounterBadge (through Container and Row), then read the Properties tab. The label field appears only because CounterBadge overrides debugFillProperties and adds a StringProperty('label', label). Without that override, the tab would not list the field.
Exercise
You run flutter run --release and cannot get the Widget Inspector to show your widgets. Why, and which mode should you use instead for a layout bug?
Show answer
Release mode strips the debug service extensions and the VM service, so the inspector cannot read the tree. Use debug mode (the default for flutter run) for widget and layout bugs, which also gives you widget creation tracking. Use profile mode only for performance measurements.