Back to Articles

Intermediate

Custom Drawing in Flutter with CustomPainter

· 8 min read

Custom Drawing in Flutter with CustomPainter

When Do You Need CustomPaint?

Push Container, DecoratedBox or ShapeBorder as far as you can; but once you need something that requires pixel-level control, such as a circular progress ring, a wave animation, a signature pad, or a simple chart, off-the-shelf widgets stop being enough. CustomPaint lets you draw lines, curves, shapes and text directly on a Canvas object.

CustomPaint is a widget on its own; the actual drawing is done by a CustomPainter subclass you hand to it. CustomPaint calls the painter's paint() method whenever a repaint is needed, giving you a Canvas and the available Size.

A progress ring continuously filling up with a percentage label in the middle
A progress ring animation drawn with CustomPainter using drawArc.

Basic Usage

class ProgressRingPainter extends CustomPainter {
  ProgressRingPainter({required this.progress, required this.color})
      : _trackPaint = Paint()
          ..color = color.withValues(alpha: 0.15)
          ..style = PaintingStyle.stroke
          ..strokeWidth = 10
          ..strokeCap = StrokeCap.round,
        _progressPaint = Paint()
          ..color = color
          ..style = PaintingStyle.stroke
          ..strokeWidth = 10
          ..strokeCap = StrokeCap.round;

  final double progress; // 0.0 - 1.0
  final Color color;
  final Paint _trackPaint;
  final Paint _progressPaint;

  @override
  void paint(Canvas canvas, Size size) {
    final center = size.center(Offset.zero);
    final radius = size.shortestSide / 2 - 8;
    final rect = Rect.fromCircle(center: center, radius: radius);

    canvas.drawCircle(center, radius, _trackPaint);
    canvas.drawArc(
      rect,
      -math.pi / 2,
      2 * math.pi * progress,
      false,
      _progressPaint,
    );
  }

  @override
  bool shouldRepaint(ProgressRingPainter oldDelegate) {
    return oldDelegate.progress != progress || oldDelegate.color != color;
  }
}

To use this painter, hand it to a CustomPaint. The size parameter sets the space the CustomPaint widget itself gets; the painter needs no extra size information from outside:

CustomPaint(
  size: const Size(120, 120),
  painter: ProgressRingPainter(progress: 0.65, color: Colors.indigo),
)

Getting shouldRepaint Right

Flutter doesn't automatically call paint() again every frame. shouldRepaint compares the old painter to the new one and decides whether a repaint is actually needed. Always returning true works, but forces a needless redraw every frame and hurts performance; always returning false means changes never show up. The right approach is to compare every field that affects the drawing, such as progress and color.

Using It With an Animation

To animate the progress ring, it's enough to tie the painter to an AnimationController's value and wrap the CustomPaint in an AnimatedBuilder. On every animation tick a new painter instance is created, shouldRepaint compares the old and new progress values, and a repaint only happens when something actually changed:

AnimatedBuilder(
  animation: _controller,
  builder: (context, _) {
    return CustomPaint(
      size: const Size(120, 120),
      painter: ProgressRingPainter(
        progress: _controller.value,
        color: Colors.indigo,
      ),
    );
  },
)

Things to Watch Out For

1. Don't Recreate Paint Objects on Every paint() Call

Creating Paint objects isn't free. Since paint() can run every frame, keeping Paint objects created once, in the constructor or as fields like in the example above, is far cheaper than calling Paint() again on every draw.

2. Isolate Complex Drawings With RepaintBoundary

If the CustomPaint widget is a small part of a screen but the widgets around it rebuild frequently, wrapping CustomPaint in a RepaintBoundary keeps the drawing on its own layer so it isn't affected by changes around it.

3. The isComplex and willChange Hints

CustomPainter's isComplex and willChange getters give Flutter's rendering engine optimization hints. If the drawing is genuinely complex, setting isComplex to true, and if it changes frequently, setting willChange to true, can improve Flutter's caching decisions.

Conclusion

CustomPainter gives you full control once off-the-shelf widgets run out of room. The Canvas and Paint APIs are rich, but the real key is performance: reuse Paint objects, get shouldRepaint right, and isolate with RepaintBoundary when needed. Apply these three rules and CustomPaint stays smooth even for animated graphics.