Sub topic 7
Deciding When to Repaint with `shouldRepaint`
Assume you know Dart classes, Flutter widget trees, layout constraints, Size, and the basics of AnimationController. This chapter focuses on how Flutter decides whether an existing CustomPainter needs to paint again.
What `shouldRepaint` controls
Flutter calls shouldRepaint when a CustomPaint receives a new painter delegate with the same runtime type. The method compares the old delegate with the new one:
@override
bool shouldRepaint(covariant MyPainter oldDelegate) {
return oldDelegate.value != value;
}Return true when the new painter might produce different pixels. Return false when its output is unchanged.
The method does not control the first paint. Flutter must paint a new painter at least once. It also does not replace layout or hit testing. It only tells Flutter whether the painter's visual output needs to be repainted after the delegate changes.
Compare every visual input
A painter should usually store its inputs in immutable final fields. shouldRepaint must compare every field that can change the result of paint.
import 'dart:math' as math;
import 'package:flutter/material.dart';
class ProgressPainter extends CustomPainter {
const ProgressPainter({required this.progress});
final double progress;
@override
void paint(Canvas canvas, Size size) {
final center = size.center(Offset.zero);
final radius = size.shortestSide / 2 - 10;
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.stroke
..strokeWidth = 12
..strokeCap = StrokeCap.round;
canvas.drawArc(
Rect.fromCircle(center: center, radius: radius),
-math.pi / 2,
2 * math.pi * progress,
false,
paint,
);
}
@override
bool shouldRepaint(covariant ProgressPainter oldDelegate) {
return oldDelegate.progress != progress;
}
}
class ProgressExample extends StatelessWidget {
const ProgressExample({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: CustomPaint(
size: Size.square(160),
painter: ProgressPainter(progress: 0.65),
),
);
}
}
void main() {
runApp(const MaterialApp(home: Scaffold(body: ProgressExample())));
}If the painter also accepted a color, stroke width, or radius, each of those values would need comparison. Omitting one creates stale visuals: the widget rebuilds, but Flutter may reuse the old pixels.
| Painter input | Compare it? | Reason |
|---|---|---|
| `progress` | Yes | It changes the arc length |
| `color` | Yes | It changes the painted color |
| A cached object unused by `paint` | No | It cannot affect the output |
Common errors
Returning true unconditionally is correct but inefficient. It repaints whenever Flutter supplies a new delegate, even if the result is identical.
Returning false unconditionally is only correct for a painter whose output never changes. Otherwise, the screen can display old graphics.
A mutable field can also cause confusion. If code changes a field inside the same painter instance, Flutter may not receive a new delegate, so shouldRepaint may not run. Prefer creating a new painter with new values.
Equality needs care. For lists, maps, and custom objects, != may compare object identity rather than contents. Use immutable value objects or compare the relevant values explicitly.
When animation is involved
An animation can repaint a painter through the repaint listenable passed to super. In that design, the animation triggers paint directly without requiring a widget rebuild. shouldRepaint still matters when the painter delegate itself changes, but it is not called for every animation tick.
For static painters, return false when the delegate has no changing visual data. For data-driven painters, compare all visual inputs. This gives Flutter a precise answer and avoids unnecessary work.
Exercise
A painter draws a circle using value and color, but shouldRepaint compares only value. What bug can occur, and how should you fix it?
Show answer
Changing only color can leave the old color on screen. Compare both oldDelegate.value != value and oldDelegate.color != color, or use an equivalent value comparison.