Sub topic 2
Composing Widgets into a Tree
Assume you know basic Dart classes, constructors, final fields, named parameters, and null safety. You also need a Flutter installation that can run a simple app.
A widget tree is a description of the screen
A Flutter screen is built from widgets. Each widget describes one part of the user interface, such as text, padding, a row, or a complete page.
Widgets contain other widgets. This creates a tree:
- A parent widget contains child widgets.
- A child widget can contain more children.
- The root widget is at the top.
- Leaf widgets, such as
TextorIcon, are near the bottom.
For example, a page might have this structure:
This structure is called composition. Instead of creating one large class that draws everything, you combine small widgets. Each widget has a focused responsibility.
Parent and child relationships
Many Flutter widgets accept a child parameter for one widget or a children parameter for several widgets.
| Parameter | Accepts | Common examples |
|---|---|---|
| `child` | One widget | `Padding`, `Center`, `Container` |
| `children` | A list of widgets | `Column`, `Row`, `ListView` |
A Column places its children vertically. A Row places them horizontally. Widgets such as Padding modify the space around a single child.
The following app composes a page from several layers:
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(
home: Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: const Padding(
padding: EdgeInsets.all(24),
child: ProfileCard(),
),
),
);
}
}
class ProfileCard extends StatelessWidget {
const ProfileCard({super.key});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
CircleAvatar(child: Icon(Icons.person)),
SizedBox(height: 12),
Text('Ada Lovelace'),
Text('[email protected]'),
],
);
}
}MyApp returns a MaterialApp. That widget contains a Scaffold, which contains an app bar and a body. The body contains padding, and the padding contains ProfileCard. ProfileCard then returns a Column with four children.
Build methods create branches
A build method returns the widget at the root of one branch. Flutter calls that method to obtain the branch's current description. The method does not draw pixels directly. It returns widgets that Flutter uses to update the screen.
BuildContext identifies where a widget is located in the tree. It lets a widget find information provided by ancestors, such as the theme or screen size. You will often pass context to methods such as Theme.of(context) or MediaQuery.sizeOf(context).
Keep build methods readable. If a branch becomes difficult to understand, extract it into a named widget. This improves reuse and makes later rebuild behavior easier to reason about.
Exercise
Exercise
In the example, which widget is the parent of ProfileCard, and which widget is its child?
Show answer
Padding is the parent of ProfileCard, and ProfileCard is its child.