Sub topic 10
Render objects: layout, painting, and hit testing
Assume you know basic Dart and can compose Row, Column, Padding, and Text. This chapter focuses on the render object tree, which is the part of Flutter that turns configuration into geometry, pixels, and pointer behavior.
What a render object does
A render object is a long-lived object that participates in one or more low-level UI tasks:
- Layout chooses the object's size and position relative to its parent.
- Painting draws the object and its children on a canvas.
- Hit testing determines which object is under a pointer event.
Widgets usually do not perform these tasks themselves. A RenderObjectWidget creates or updates a render object. Composition widgets such as Padding create a specialized render object, while widgets such as Directionality mainly provide information to descendants.
| Widget example | Render object role |
|---|---|
| `Padding` | Creates `RenderPadding`, which applies inset constraints |
| `Text` | Builds a paragraph render object that measures and paints text |
| `Directionality` | Provides inherited configuration and creates no render object of its own |
Layout and painting
Flutter layout follows the rule constraints go down, sizes go up, and the parent sets the position.
A parent gives a child a range of allowed widths and heights. The child chooses a size within that range. The parent then places the child. A render object implements this decision in performLayout.
After layout, Flutter calls paint. The render object receives a canvas and an offset. It should paint relative to that offset. If a property changes the geometry, call markNeedsLayout. If it changes only pixels, call markNeedsPaint.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class ColoredSquare extends LeafRenderObjectWidget {
const ColoredSquare({super.key, required this.color, this.side = 64});
final Color color;
final double side;
@override
RenderColoredSquare 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);
}
@override
bool hitTestSelf(Offset position) => true;
}
void main() {
runApp(const Directionality(
textDirection: TextDirection.ltr,
child: Center(child: ColoredSquare(color: Color(0xFF1565C0))),
));
}Hit testing and invalidation
Hit testing usually proceeds from the root toward children. A render object checks whether a pointer position falls inside its bounds, then offers the event to a child or reports itself as a target. hitTestSelf returning true makes the square receive hits within its bounds.
Painting does not automatically change when a field changes. The setter must invalidate the correct phase. Calling markNeedsLayout also causes later painting, but markNeedsPaint avoids unnecessary layout when only the color changes.
Exercise
If changing a render object's color does not change its size, which invalidation method should its color setter call?
Show answer
It should call markNeedsPaint(), because only the pixels change.