I have a simple app that draws via a CustomPainter a red or green circle on a canvas, depending on which button is pressed in the AppBar:
The class ColorCircle extends CustomPainter and is responsible for drawing the colored circle:
class ColorCircle extends CustomPainter {
MaterialColor myColor;
ColorCircle({@required this.myColor});
@override
void paint(Canvas canvas, Size size) {
debugPrint('ColorCircle.paint, ${DateTime.now()}');
final paint = Paint()..color = myColor;
canvas.drawCircle(Offset(size.width / 2, size.height / 2), 100, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
The drawing of the different colors works fine, but when I click (only once!) or hover over one of the buttons, the paint method gets called several times:
Further implementation details:
I use a StatefulWidget for storing the actualColor. In the build method actualColor is passed to the ColorCircle constructor:
class _MyHomePageState extends State<MyHomePage> {
MaterialColor actualColor = Colors.red;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
OutlinedButton(
onPressed: () => setState(() => actualColor = Colors.red),
child: Text('RedCircle'),
),
OutlinedButton(
onPressed: () => setState(() => actualColor = Colors.green),
child: Text('GreenCircle'),
),
],
),
body: Center(
child: CustomPaint(
size: Size(300, 300),
painter: ColorCircle(myColor: actualColor),
),
),
);
}
}
The complete source code with a running example can be found here: CustonPainter Demo
So why is paint called several times instead of only once? (And how could you implement it so that paint is called only once?).


