Sub topic 1
How a Flutter App Is a Tree of Widgets
Prerequisites
This chapter assumes basic Dart syntax, including classes, constructors, and named parameters. You should also have a working Flutter SDK and understand StatelessWidget and its build method.
The widget tree
Flutter describes an interface as a tree of widgets. A widget is an immutable description of part of the user interface. Immutable means that, after the widget is created, its configuration does not change. When something changes, Flutter creates or reuses widget configurations and rebuilds the affected part of the tree.
The tree starts at one root widget. Each widget can have child widgets, either through a child property or a collection such as children. The parent provides structure and configuration. The child describes a smaller part of the screen.
For a small Material app, the structure often looks like this:
MyApp
└── MaterialApp
└── HomePage
└── Scaffold
├── AppBar
│ └── Text
└── Center
└── TextMyApp is the Dart class that returns the root MaterialApp. MaterialApp supplies Material Design services and configuration. HomePage describes a screen. Scaffold gives that screen standard regions, such as an app bar and body. The remaining widgets describe visible content.
The tree is a description, not a direct list of pixels. Flutter compares the new widget tree with the previous one and updates the underlying element and render-object trees as needed. This separation lets Flutter rebuild declarative descriptions without recreating every low-level object on every change.
Building parent and child widgets
A widget's build method returns another widget. That returned widget becomes its child in the tree:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Widget Tree Demo',
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Widget Tree'),
),
body: const Center(
child: Text('The Text widget is inside Center.'),
),
);
}
}The nesting in the return statement is the important part. MaterialApp is the child returned by MyApp. Scaffold is the child returned by HomePage. The AppBar and Center are configuration values passed to Scaffold, and Text is passed to either AppBar or Center.
A widget can have several descendants without placing every detail in one class. Extracting a subtree into a separate widget improves readability and gives that subtree a clear responsibility:
class WelcomeMessage extends StatelessWidget {
const WelcomeMessage({super.key});
@override
Widget build(BuildContext context) {
return const Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Welcome'),
Text('Choose an action to continue.'),
],
);
}
}You can then use const WelcomeMessage() as the child of Center. The widget tree stays the same conceptually, but the code becomes easier to maintain.
Build context and tree position
BuildContext identifies a widget's location in the tree. Flutter passes it to build so widgets can find information supplied by ancestors, such as the current theme, localization, or navigator.
For example, Theme.of(context) reads theme data from an ancestor. Navigator.of(context) finds the nearest navigator. The result depends on where the widget sits. Calling these methods with a context above the needed ancestor can produce the wrong value or fail.
Do not treat BuildContext as a global application object. It is a handle to a particular position. A context may also become invalid after its widget is removed, so avoid using it across an asynchronous gap without checking that the widget is still mounted.
Reading the tree in DevTools
Flutter DevTools can display the widget tree while the app runs. Use it to inspect parent-child relationships, locate unexpected padding or constraints, and identify which widget owns a property. The widget tree is often more useful than guessing from the final pixels because it shows where each behavior comes from.
Exercise
Exercise
In the example, which widget is the parent of Text('The Text widget is inside Center.'), and which widget is its ancestor that controls the screen's main layout regions?
Show answer
Center is the direct parent. Scaffold is the ancestor that provides the app bar and body regions.