Sub topic 12
Rebuilds Are Not Repaints: When RepaintBoundary Helps
Assume you know basic Dart, StatelessWidget, StatefulWidget, setState, and the build method. This chapter focuses on a different part of Flutter’s rendering pipeline: painting.
Build, layout, and paint are different
A widget rebuild means Flutter calls its build method again. The resulting element tree may then need layout and painting. These stages have different jobs:
- Build creates or updates widget configuration.
- Layout calculates sizes and positions.
- Paint draws pixels into a canvas.
- Rasterization turns those drawing commands into pixels for the screen.
A rebuild does not always cause expensive painting. Flutter can reuse work when the render tree has not changed. Similarly, preventing a repaint does not prevent a widget from rebuilding or running layout.
RepaintBoundary is a widget that places its child in a separate composited layer. Flutter can repaint that layer independently from surrounding content. It does not create a boundary around build calls.
When a boundary helps
A boundary is useful when one part of the screen repaints often and nearby content is expensive but mostly unchanged. Common examples include:
- An animated chart beside static labels.
- A custom painting surface receiving frequent pointer input.
- A video or animation surrounded by complex UI.
- A scrolling item with expensive painting, when the boundary matches the item’s behavior.
import 'package:flutter/material.dart';
class ChartCard extends StatelessWidget {
const ChartCard({super.key});
@override
Widget build(BuildContext context) {
return Card(
child: RepaintBoundary(
child: CustomPaint(
size: const Size(300, 180),
painter: ChartPainter(),
),
),
);
}
}
class ChartPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..strokeWidth = 4
..style = PaintingStyle.stroke;
final path = Path()
..moveTo(0, size.height * .7)
..quadraticBezierTo(
size.width * .5,
size.height * .1,
size.width,
size.height * .5,
);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(covariant ChartPainter oldDelegate) => false;
}This boundary can prevent a repaint of the card’s other contents when the chart’s layer changes. It does not help this example much because shouldRepaint already says that the painter never changes. The point is to place the boundary around a genuinely changing, costly painter.
When a boundary hurts
Each boundary can add a composited layer. Layers use memory and may require extra compositing work. A boundary around a cheap Text widget usually adds complexity without saving meaningful time.
A boundary can also increase memory use when Flutter must retain a large layer, such as a full-screen image. Too many boundaries can make scrolling or compositing slower rather than faster.
| Situation | Likely choice |
|---|---|
| Small, cheap widget that rarely changes | Do not add a boundary |
| Expensive animation beside stable content | Try a boundary and measure |
| Large layer that changes every frame | Measure carefully; it may hurt |
Rebuilds and repaints must be measured separately
Use Flutter DevTools in profile mode to inspect frame timing and the layer tree. Enable paint-related debugging options only in a debug build, and compare the result before and after the change. A boundary is successful when the expensive paint work no longer spreads to unrelated content and frame time improves.
A practical decision process
- Reproduce the slow interaction in profile mode.
- Check whether build, layout, paint, or rasterization consumes the frame budget.
- Find the frequently changing region.
- Estimate whether its neighboring paint work is expensive.
- Add one
RepaintBoundaryaround the changing region. - Measure the same interaction again.
- Keep the boundary only if the frame metrics or visual behavior improve.
Exercise
Exercise
An animated icon sits next to a large, complex static illustration. The icon changes every frame, but the illustration does not. What should you try, and what must you measure?
Show answer
Try placing RepaintBoundary around the animated icon or its small animated container. Then measure paint and rasterization time in profile mode. Keep it only if the boundary reduces unrelated repaint work without adding enough layer or compositing cost to slow the frame.