What method do you override to draw on the canvas?
The paint() method receives a Canvas and the Size of the paint area, where you do all drawing.
Draw custom shapes, charts, and graphics on a canvas
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
CustomPainter gives you direct access to a canvas where you can draw anything from shapes, lines, arcs, and paths to gradients and beyond. CustomPainter is commonly used for building charts, custom animations, and unique UI elements that standard widgets cannot achieve. Understanding the Canvas API opens up possibilities for creating entirely custom visual experiences in your Flutter applications.
1// ── Basic CustomPainter ──2class CircleProgressPainter extends CustomPainter {3final double progress; // 0.0 to 1.04final Color color;56CircleProgressPainter({required this.progress, required this.color});78@override9void paint(Canvas canvas, Size size) {10final center = Offset(size.width / 2, size.height / 2);11final radius = size.width / 2 - 10;1213// Background circle14final bgPaint = Paint()15..color = Colors.grey[300]!16..strokeWidth = 817..style = PaintingStyle.stroke;18canvas.drawCircle(center, radius, bgPaint);1920// Progress arc21final progressPaint = Paint()22..color = color23..strokeWidth = 824..style = PaintingStyle.stroke25..strokeCap = StrokeCap.round;26canvas.drawArc(27Rect.fromCircle(center: center, radius: radius),28-pi / 2, // start angle (top)292 * pi * progress, // sweep angle30false,31progressPaint,32);3334// Center text35final textPainter = TextPainter(36text: TextSpan(37text: '${(progress * 100).toInt()}%',38style: TextStyle(color: color, fontSize: 24, fontWeight: FontWeight.bold),39),40textDirection: TextDirection.ltr,41);42textPainter.layout();43textPainter.paint(canvas, center - Offset(textPainter.width / 2, textPainter.height / 2));44}4546@override47bool shouldRepaint(CircleProgressPainter old) => old.progress != progress;48}4950// Usage:51CustomPaint(52size: const Size(150, 150),53painter: CircleProgressPainter(progress: 0.75, color: Colors.blue),54)
Always implement shouldRepaint() correctly! Return true only when the painter's parameters change. This prevents unnecessary repaints and improves performance.
CustomPainter work runs frequently, so small inefficiencies become visible quickly:
Efficient painters unlock advanced visuals without sacrificing smoothness.
Draw custom shapes, charts, and graphics on a canvas. This lesson adds one more building block for creating stable and user-friendly Flutter apps.
Operate with production and interview-level clarity.
After reading, rewrite the concept in your own words and apply it in a tiny runnable sample.
Each lesson becomes useful when you connect concept, implementation, and real-user outcome together.
Connect this lesson to separation of concerns so your codebase stays maintainable as it grows.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
The paint() method receives a Canvas and the Size of the paint area, where you do all drawing.
shouldRepaint() tells Flutter if the painter needs to redraw. Return true when data changes.
PaintingStyle.stroke draws just the outline. PaintingStyle.fill fills the entire shape.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue