Sub topic 13
Keeping Painters Fast with RepaintBoundary
This chapter assumes you understand CustomPaint, CustomPainter, Flutter layout constraints, and AnimationController. It focuses on reducing unnecessary painting when a screen contains custom graphics.
What RepaintBoundary does
RepaintBoundary is a widget that isolates part of the render tree into its own composited layer. A composited layer is a separately managed drawing result. When a widget inside that boundary repaints, Flutter can keep previously painted content outside the boundary.
Without a boundary, a change in one painter can cause nearby widgets to paint again. With a boundary, Flutter can reuse unchanged layers.
A boundary does not prevent layout or widget rebuilding. It mainly limits paint and compositing work. If the parent rebuilds, Flutter may still rebuild the child. If the child changes size, Flutter may still perform layout.
When to use it
Use a boundary when a region:
- Is expensive to paint.
- Changes independently from surrounding content.
- Contains animation or frequent updates.
- Is surrounded by mostly static content.
- Can be cached and reused between frames.
Typical examples include charts, maps, canvases, complex illustrations, and animated custom graphics.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: PerformanceExample()));
}
class PerformanceExample extends StatelessWidget {
const PerformanceExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Painter performance')),
body: Column(
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text('This text can remain unchanged while the painter updates.'),
),
Expanded(
child: RepaintBoundary(
child: CustomPaint(
painter: GridPainter(),
child: const SizedBox.expand(),
),
),
),
],
),
);
}
}
class GridPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.indigo
..strokeWidth = 1;
for (double x = 0; x < size.width; x += 24) {
canvas.drawLine(Offset(x, 0), Offset(x, size.height), paint);
}
for (double y = 0; y < size.height; y += 24) {
canvas.drawLine(Offset(0, y), Offset(size.width, y), paint);
}
}
@override
bool shouldRepaint(covariant GridPainter oldDelegate) => false;
}Make `paint()` inexpensive
paint() can run many times per second. Keep it predictable and avoid work that does not depend on the current frame.
Prefer these practices:
- Create reusable objects outside
paint()when their values do not change. - Avoid network calls, file access, and heavy computation.
- Do not rebuild widget trees from inside a painter.
- Cache complicated paths, images, or calculated data when possible.
- Draw only the visible content.
- Keep
shouldRepaintaccurate.
A painter with no changing data should return false from shouldRepaint. A painter with changing fields should compare those fields. Returning true unconditionally forces extra paint work.
| Situation | Recommended action |
|---|---|
| Static illustration | Use `shouldRepaint => false` and consider a boundary |
| Animated region | Use a boundary when surrounding content is stable |
| Cheap, frequently changing shape | Measure before adding a boundary |
| Large path or image | Cache calculations and isolate the region |
Check the result
Use Flutter DevTools to inspect frame times and repaint activity. Turn on the performance overlay or repaint rainbow to see which regions paint again. Compare the screen with and without the boundary under the same workload.
Exercise
Exercise
A custom chart animates every frame, while the rest of the page is static. Where should you place RepaintBoundary, and what should you verify afterward?
Show answer
Place it around the chart's CustomPaint region. Verify with DevTools that the chart repaints while static page content is reused, and confirm that the extra layer does not make memory or frame time worse.