D
Widget TreeExpert80 XP6 min read

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

AspectCustomPainterCustom RenderObject
Use caseCustom drawing onlyCustom layout + drawing
ComplexityLow — implement paint()High — implement layout, paint, hitTest
ChildrenNo layout of childrenFull control of child layout
PerformanceGoodBest 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

dartMinimal single-child RenderBox
Output
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.

#RenderObject#CustomPainter#layout#paint#low-level