Flutter · One codebase, native feel

When and how would you use CustomPainter?

Hardcustom paintercanvasgraphicsperformanceUpdated Oct 2026
Quiz this track

Short answer

Use CustomPainter for shapes, charts or effects that are impractical with widget composition. Implement shouldRepaint correctly and keep the paint method allocation-light.

CustomPainter exposes a Canvas where you draw paths, text, images and gradients. The painter runs during paint, not build. Widgets are still better for interactive elements, semantics and layout. Custom painting is appropriate when the visual is inherently geometric or when many primitives would be wasteful as widgets.

dart
class SparklinePainter extends CustomPainter {
  SparklinePainter(this.values);

  final List<double> values;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = const Color(0xFF0F766E)
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;

    if (values.length < 2) return;

    final path = Path();
    for (var i = 0; i < values.length; i++) {
      final x = size.width * i / (values.length - 1);
      final y = size.height * (1 - values[i]);
      if (i == 0) {
        path.moveTo(x, y);
      } else {
        path.lineTo(x, y);
      }
    }
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant SparklinePainter oldDelegate) {
    return !listEquals(oldDelegate.values, values);
  }
}
  • Return true from shouldRepaint only when the drawing inputs changed
  • Wrap the painter in RepaintBoundary when it animates independently
  • Avoid saveLayer and large blurred shadows unless measured
  • Reuse Paint and Path objects where possible, but not across different sizes incorrectly
  • Provide semantics separately because canvas drawing is invisible to screen readers

Interaction and testing

Hit testing must be implemented manually through hitTest or by wrapping the painter in a gesture detector over known regions. Golden tests are effective for painter output. Keep the painter deterministic and avoid reading ambient state inside paint.

A custom painter that repaints every frame can dominate raster time. If shouldRepaint always returns true, check whether that is actually necessary.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What does shouldRepaint control?
  2. 2When does RepaintBoundary help a custom painter?
  3. 3How would you make a painted chart accessible?
  4. 4Why is saveLayer expensive?

Say it in the interview

I use CustomPainter when the visual is geometric, like a chart or a complex shape, and composing widgets would be slower or awkward. The paint method runs during the paint phase, so I keep it allocation-light and implement shouldRepaint accurately. I wrap independent paintings in RepaintBoundary, add semantics separately, and use golden tests for deterministic output.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Flutter mock interview and get scored feedback on depth, clarity and confidence.