Sub topic 1
Layout starts with the widget tree
Flutter builds every screen from a widget tree. A widget is a Dart object that describes part of the interface, such as text, padding, a row, or an entire page. Each widget can have a parent and one or more children.
The tree structure matters because parent widgets control how their children are laid out. A Padding widget gives its child less available space. A Row places its children horizontally. A Scaffold provides the main structure for a screen.
Prerequisites
This chapter assumes that you can:
- Read basic Dart syntax, including classes, named parameters, and lists.
- Create a
StatelessWidgetand implement itsbuildmethod. - Run a Flutter application on a device, emulator, or desktop target.
Reading a widget tree
Consider this small application:
import 'package:flutter/material.dart';
void main() {
runApp(const LayoutDemo());
}
class LayoutDemo extends StatelessWidget {
const LayoutDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Profile'),
),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Ada Lovelace'),
const SizedBox(height: 8),
Row(
children: const [
Icon(Icons.email),
SizedBox(width: 8),
Text('[email protected]'),
],
),
],
),
),
),
);
}
}The LayoutDemo widget is the root of the application. Its build method returns a MaterialApp, which contains a Scaffold. The Scaffold contains an app bar and a body.
The body is a Padding widget. Its child is a Column, so the padding surrounds the whole column. The column has two children: a name and a Row. The row then contains an icon, a gap, and an email address.
The list passed to children creates multiple child relationships. The order in that list is important. A Column lays out its children in list order from top to bottom. A Row lays them out from left to right.
Why nesting affects layout
Flutter performs layout by walking through this tree. A parent passes layout information to each child. The child chooses a size within that information, and the parent uses that result to position the child.
This means the same widget can behave differently in different parents. A Text inside a narrow Row may need to wrap or truncate. The same Text inside a wide page may remain on one line. The surrounding tree determines the available space.
Use small, meaningful subtrees. If a build method becomes difficult to inspect, extract a section into its own widget. The resulting tree still describes the same screen, but each part becomes easier to reason about and test.
The widget tree is separate from the rendered pixels. Widgets describe what should exist. Flutter uses that description to create an element tree and render objects that handle state, layout, and painting. You usually work with widgets, but understanding their parent-child relationships helps explain the behavior of the lower-level objects.
Exercise
In the example, which widget controls the vertical order of the name and the email row?
Show answer
The Column controls their vertical order because both widgets are children of its children list.