Sub topic 3
One frame: build, layout, paint, composite
This chapter assumes you know Dart classes, StatefulWidget, and basic layout widgets such as Row, Column, and Padding.
The four phases of a frame
Calling setState marks an element dirty and schedules a frame. Each frame runs four phases in order.
Build runs build on dirty elements and reconciles their children. Layout computes sizes for render objects that need it. Paint records drawing commands into layers. Composite sends the layers to the GPU.
Layout: constraints down, sizes up
A parent passes a BoxConstraints object down to each child. The child picks a size that satisfies those constraints and reports it back up. The parent then sets the child's position. A child never knows where it sits on screen.
Painting and the layer tree
paint draws into a PaintingContext. Some render objects, such as opacity or clipping, push their own layers. Compositing stitches these layers together, so a change that affects only one layer does not force the others to be redrawn.
InheritedWidget and dependency tracking
When a widget calls context.dependOnInheritedWidgetOfExactType<T>(), its element registers as a dependent. When the inherited widget changes and updateShouldNotify returns true, only the registered dependents rebuild.
Looking at the three trees
Call the debug dump functions after the first frame to inspect a running app.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(const Text('Hello', textDirection: TextDirection.ltr));
WidgetsBinding.instance.addPostFrameCallback((_) {
debugDumpApp(); // widget and element tree
debugDumpRenderTree(); // render object tree
});
}Relayout and repaint boundaries
A RepaintBoundary gives its subtree its own layer, so repainting stops at that edge. A render object becomes a relayout boundary when its size depends only on its constraints. Changes below such a boundary do not force layout to climb above it.
Practical consequences for performance
- Use
constconstructors so Flutter skips rebuilding unchanged widgets. - Move state into small widgets so
setStaterebuilds a small subtree. - Wrap frequently repainting content, such as animations, in a
RepaintBoundary. - Depend on narrow inherited widgets so rebuilds stay limited to the affected widgets.
Building a custom RenderObjectWidget
A LeafRenderObjectWidget creates a render object and updates it when its configuration changes. The render object handles layout and painting.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class ColoredSquare extends LeafRenderObjectWidget {
const ColoredSquare({super.key, required this.color, this.side = 48});
final Color color;
final double side;
@override
RenderObject createRenderObject(BuildContext context) =>
RenderColoredSquare(color, side);
@override
void updateRenderObject(BuildContext context, RenderColoredSquare renderObject) {
renderObject
..color = color
..side = side;
}
}
class RenderColoredSquare extends RenderBox {
RenderColoredSquare(this._color, this._side);
Color _color;
double _side;
set color(Color value) {
if (value == _color) return;
_color = value;
markNeedsPaint();
}
set side(double value) {
if (value == _side) return;
_side = value;
markNeedsLayout();
}
@override
void performLayout() {
size = constraints.constrain(Size.square(_side));
}
@override
void paint(PaintingContext context, Offset offset) {
context.canvas.drawRect(offset & size, Paint()..color = _color);
}
}Setting color calls markNeedsPaint, which reruns only paint. Setting side calls markNeedsLayout, which reruns layout as well.
Exercise
Exercise
In the ColoredSquare example, which setter should call markNeedsLayout instead of markNeedsPaint, and why?
Show answer
The side setter. A new side changes the size the render object reports, so the parent must run layout again. Changing only the color affects how pixels are drawn, so markNeedsPaint is enough.