Run animation on property change

Flutter tips Published on

Triggering Animations in Flutter

Ever wondered how to make an animation play in your Flutter app when a specific setting or value changes in your widget? It's a common task, especially when dealing with state changes.

The Key Function: didUpdateWidget

In Flutter, StatefulWidgets have a special function called didUpdateWidget. This function is automatically called every time the widget is rebuilt with new data. It's the perfect spot to check if a property you care about has changed.

How it Works

The didUpdateWidget method gives you access to the oldWidget. This means you can compare the properties of the current widget (widget) with the properties of the widget before it was updated (oldWidget).

Simple Property Check

Let's say your widget has a bool property called isUploading, and you want to start an animation when it changes from false to true.


void didUpdateWidget(covariant UploadedAvatarAnimation oldWidget) {
  super.didUpdateWidget(oldWidget);
  if (widget.isUploading && !oldWidget.isUploading) {
    // Start your animation here
    _controller.forward(from: 0);
  }
}

This works well for single properties. However, be careful if your widget has many properties. didUpdateWidget fires whenever any property changes, so your check needs to be specific.

A Sexier Dart 3 Pattern

For handling changes between different states or multiple properties, the new Dart 3 pattern using switch on a tuple can be very clean.


void didUpdateWidget(covariant UploadedAvatarAnimation oldWidget) {
  super.didUpdateWidget(oldWidget);
  final (wasUploading, isUploading) = (
    oldWidget.isUploading,
    widget.isUploading
  );
  switch ((wasUploading, isUploading)) {
    case (false, true):
      // Transition from not uploading to uploading
      _controller.forward(from: 0);
      break;
    case (true, false):
      // Transition from uploading to not uploading
      _controller.reverse(from: 1);
      break;
    default:
      // No change in upload status or other transitions
      break;
  }
}

This pattern allows you to define specific actions for specific state transitions (e.g., false to true, true to false).

Conclusion

Using didUpdateWidget is the standard way to react to property changes in a StatefulWidget and trigger actions like starting or reversing animations. Choose the simple if check for single properties or the Dart 3 switch pattern for more complex state transitions.

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 `didUpdateWidget` used for?

`didUpdateWidget` is a method in Flutter's StatefulWidget lifecycle that is called whenever the widget is rebuilt with new properties from its parent.

How can I check if a specific property changed?

Inside `didUpdateWidget`, you can compare the value of a property on the `widget` (the new properties) with the value of the same property on the `oldWidget` (the previous properties).

Why use the `switch` pattern with tuples?

The `switch` pattern with tuples is a clean way to handle different transition cases for properties, especially when you want to trigger different actions (like forward or reverse animation) based on how the property changed.

When is `didUpdateWidget` called?

`didUpdateWidget` is called after the `build` method when the widget's configuration changes and it needs to be rebuilt, but only if the `canUpdate` method returns true (which it usually does for the same widget type and key).

Read more
You may also be interested in
Get ⭐ reviews on stores  blog card image
Get ⭐ reviews on stores
Published on 2025-05-19T07:11:44.058Z
Listen keyboard visibility state  blog card image
Listen keyboard visibility state
Published on 2025-05-12T09:22:43.829Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved