Sub topic 12
One Frame: Build, Layout, Paint, Composite
Assume you know basic Dart and can compose Flutter widgets such as Row, Column, Padding, and Text. This chapter follows one state change through the main work Flutter performs before displaying a frame.
The frame pipeline
Flutter uses a simplified pipeline:
- Build updates the element tree from widget configuration.
- Layout calculates each render object's size and position.
- Paint records drawing commands and creates the layer tree.
- Composite combines layers and sends the result to the rasterizer, which turns the drawing into pixels.
A frame does not always run every phase for every object. Flutter tracks which elements, render objects, and layers need work.
| Phase | Main question | Typical trigger |
|---|---|---|
| Build | Which widgets and elements should exist? | `setState`, an inherited value change |
| Layout | How large and where is each render object? | Size or constraint change |
| Paint | What should each object draw? | Color or visual content change |
| Composite | How should layers be combined? | Transform, opacity, or layer change |
From `setState` to pixels
Consider this counter:
import 'package:flutter/material.dart';
void main() => runApp(const CounterApp());
class CounterApp extends StatelessWidget {
const CounterApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: CounterPage());
}
}
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Count: $count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => count++),
child: const Icon(Icons.add),
),
);
}
}When the button is tapped, setState changes count and marks the page's element dirty. Flutter schedules a new frame. During build, Text('Count: $count') creates new widget configuration. Flutter compares it with the previous configuration and usually updates the existing elements rather than replacing the whole tree.
The updated text may change its measured size. Layout then passes constraints from parents to children. Each child chooses a size within those constraints, and the parent chooses the child's position. This is the rule: constraints go down, sizes go up, and parents set positions.
After layout, render objects paint. The text records new drawing commands. Flutter may reuse unchanged painting from other parts of the tree. Finally, the compositor combines layers, such as an opacity layer or a transformed layer, and the rasterizer produces the screen image.
Finding unnecessary work
Keep frequently changing state near the widgets that use it. Use const constructors for configuration that never changes. Split large build methods into smaller widgets so Flutter can identify stable subtrees. A repaint boundary can isolate a frequently repainted region, but it also adds a layer and memory cost, so use it after measuring.
Exercise
If a widget's text changes but its size and position remain the same, which phase must at least run again?
Show answer
Paint must run again so the new text can be drawn. Layout may also run if the new text has different metrics, but it is not always required.