CustomPainter in Flutter: solved exercise
CustomPainter in Flutter: solved exercise
When a standard widget is not enough, CustomPainter gives you direct access to the Canvas to draw shapes, paths, and text with full pixel control. It is used in charts, animated indicators, digital signatures, and custom visual effects.
Problem statement
Implement two painters:
RingProgressPainter: a circular progress ring (like health or loading indicators).BarChartPainter: a simple bar chart with value labels.
Connect both to a Slider so the value updates in real time.
Dependencies
Flutter SDK only — no external packages.
Solution: RingProgressPainter
Solution: BarChartPainter
Solution: full screen
Expected result
- The ring displays the percentage in the center and the green arc grows as you move the slider.
- The bar chart shows 6 columns with day labels.
- Both repaint fluidly thanks to
shouldRepaint.
How shouldRepaint works
Flutter calls paint only when shouldRepaint returns true. Comparing only the changed fields avoids unnecessary repaints and keeps rendering at 60/120 fps.
Common mistakes
- Not implementing
shouldRepaintproperly: always returningtruetriggers a repaint every frame even when nothing changed. - Creating
Paint()insidepaint():Paintobjects are lightweight but creating them on every repaint adds up. Move them tofinalfields if the painter is not stateful. - Assuming arc origin is center:
canvas.drawArcuses aRect, not a center point. Always computeOffset(size.width/2, size.height/2)explicitly.
When to use CustomPainter
| Situation | Best option |
|---|---|
| Custom chart | CustomPainter |
| Complex animation | CustomPainter + AnimationController |
| Full production chart | fl_chart / syncfusion_flutter_charts |
| Simple decorative shapes | Container + BoxDecoration |
Practical use
CustomPainter is used for onboarding progress indicators, analytics charts, on-screen signatures, heat maps, and any effect that standard widgets cannot achieve.
Recommended next exercise
- SliverAppBar collapsible in Flutter: solved exercise
- Implicit animations in Flutter: solved exercise
- Performance and rebuilds in Flutter: solved exercise
- All Flutter exercises
Guided practice and next step
- More Flutter exercises
- C exercises to strengthen fundamentals
- Programming in C in 100 Solved Exercises
- View the book on Amazon (included in Kindle Unlimited)
- Subscribe to the newsletter
FAQ
Does CustomPainter work with animations?
Yes. Pass an AnimationController as a parameter, add ..addListener(notifyListeners) in initState, and the painter will repaint on every animation frame.
How do I add hit testing to CustomPainter?
Override hitTest(Offset position) in the painter and return true if the position falls within an interactive area. Then wrap CustomPaint in a GestureDetector.
Can I use CustomPainter inside a ListView?
Yes, but give it an explicit size with SizedBox or AspectRatio. Without a bounded size the canvas will have infinite dimensions and throw a layout error.