Automaticly redraw your canvas

Flutter tips Published on

Make Your Canvas Update Itself

Creating custom drawings on a canvas in Flutter is cool, but how do you make it redraw automatically when your data changes?

The trick is to use a ChangeNotifier.

Using ChangeNotifier

First, create a class for your data that extends ChangeNotifier. Whenever your data changes, call notifyListeners() in this class. This sends a signal that something is different.

class WaveModel extends ChangeNotifier {
  // your data here
  ...

  void updateData() {
    // change your data
    notifyListeners(); // Tell everyone something changed!
  }
}

Connecting to CustomPainter

In your CustomPainter, make sure it has access to your ChangeModel. The key is to pass your ChangeModel instance to the super constructor using the repaint argument.

class WorldPainter extends CustomPainter {
  final WaveModel model;

  WorldPainter({required this.model}) : super(repaint: model); // Link the painter to the model!

  
  void paint(Canvas canvas, Size size) {
    // your drawing code using model data
  }

  #### shouldRepaint Method

  
  bool shouldRepaint(covariant WorldPainter oldDelegate) => false;
}

When you pass the model to repaint, the painter will automatically call its paint method whenever model.notifyListeners() is called.

This makes your canvas redraw automatically whenever the data it depends on changes, without needing to rebuild the entire widget that holds the CustomPaint.

Summary

Using a ChangeNotifier with the repaint argument in your CustomPainter is an efficient way to get your custom drawings to update automatically when their underlying data changes.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a ChangeNotifier?

A ChangeNotifier is a simple class in Flutter that helps you notify other parts of your app when something changes. You call `notifyListeners()` to send out this signal.

How does linking ChangeNotifier to CustomPainter help?

By passing an instance of your ChangeNotifier to the `repaint` argument of the CustomPainter's constructor (`super(repaint: model)`), you tell the painter to automatically redraw itself whenever that specific ChangeNotifier calls `notifyListeners()`.

Why return false in shouldRepaint?

When you use the `repaint` argument with a ChangeNotifier, the repainting is managed by the listener attached to the notifier. In this case, you often return `false` in `shouldRepaint` because you don't need the painter to repaint just because the widget containing it was rebuilt. The repainting trigger comes from the data model itself.

Read more
You may also be interested in
Create an image from widget  blog card image
Create an image from widget
Published on 2025-05-12T09:27:21.320Z
AnimatedSwitcher with Riverpod  blog card image
AnimatedSwitcher with Riverpod
Published on 2025-05-12T08:59:30.176Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved