When would you create a custom RenderObject?
TL;DR: Create a custom RenderObject when you need layout or painting behavior that cannot be achieved by composing existing widgets — for example, custom multi-child layout algorithms or highly-optimized drawing.
Full Answer
Most custom drawing needs are satisfied by CustomPainter. But when you need custom *layout* logic — determining children's sizes and positions with custom rules — you need a custom RenderObject.
RenderObject vs CustomPainter
| Aspect | CustomPainter | Custom RenderObject |
|---|---|---|
| Use case | Custom drawing only | Custom layout + drawing |
| Complexity | Low — implement paint() | High — implement layout, paint, hitTest |
| Children | No layout of children | Full control of child layout |
| Performance | Good | Best possible |
To create a custom multi-child RenderObject: extend RenderBox (or RenderSliver), use ContainerRenderObjectMixin for child list management, override performLayout() to lay out children, and override paint() to draw.
Flutter's own Row, Column, and Stack are implemented as custom RenderObjects. Reading the Flutter source for RenderFlex is the best tutorial.
Code Examples
Centers a square child (side = shortest dimension) within the parent's full size
Common Mistakes
- ✗Forgetting to call child.layout() before accessing child.size in performLayout()
- ✗Not calling markNeedsPaint() or markNeedsLayout() when properties change
Interview Tip
Showing you've read the Flutter source for RenderFlex or RenderStack distinguishes you as a candidate who goes beyond tutorials.