Sub topic 18
Building a custom RenderObjectWidget
Prerequisites
This chapter assumes basic Dart, including classes, constructors, fields, and setters. You should also know how to build simple Flutter widgets and understand that layout constraints flow from parents to children.
Most Flutter UI can be built by composing existing widgets. Sometimes you need a drawing or layout behavior that those widgets do not provide. A custom RenderObjectWidget connects your declarative widget configuration to a custom render object.
The widget stores configuration such as color and size. The render object performs layout and painting.
flowchart TD
A[ColoredSquare widget] -->|createRenderObject| B[RenderColoredSquare]
A -->|updateRenderObject| B
B -->|performLayout| C[Choose size from constraints]
B -->|paint| D[Draw colored rectangle]
B -->|markNeedsLayout or markNeedsPaint| E[Schedule affected phase]The widget is immutable. The render object is long-lived and can be updated when the widget configuration changes.
A minimal custom render object
This example draws a square. LeafRenderObjectWidget is appropriate because the widget has no child widgets.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(const Directionality(
textDirection: TextDirection.ltr,
child: Center(child: ColoredSquare(color: Color(0xff1565c0))),
));
}
class ColoredSquare extends LeafRenderObjectWidget {
const ColoredSquare({
super.key,
required this.color,
this.side = 48,
});
final Color color;
final double side;
@override
RenderColoredSquare createRenderObject(BuildContext context) {
return 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,
);
}
}createRenderObject runs when the element first creates the render object. On later rebuilds, Flutter calls updateRenderObject instead of replacing it when the widget type and key still match.
The performLayout method must choose a size that satisfies the incoming constraints. constraints.constrain keeps the requested square within the permitted minimum and maximum dimensions.
The paint method receives a canvas context and an offset. The expression offset & size creates the rectangle occupied by this render object.
Invalidating only the necessary phase
A property that affects drawing calls markNeedsPaint. A property that affects size or position calls markNeedsLayout. Layout may lead to painting later, but a color change does not need to repeat layout.
Widget and render-object responsibilities
| Responsibility | Widget | Render object |
|---|---|---|
| Store immutable configuration | Yes | No |
| Reconcile new configuration | `updateRenderObject` | Property setters |
| Choose size | No | `performLayout` |
| Draw pixels | No | `paint` |
| Participate in hit testing | Usually no | Can override hit testing |
A RenderObjectWidget is a bridge. It does not draw by itself. Its render object participates in the frame pipeline and remains attached to the element tree until Flutter removes it.
Exercise
If only the square's color changes, which invalidation method should the color setter call, and why?
Show answer
It should call markNeedsPaint because the square's size and position are unchanged. Only its pixels need to be drawn again.