AnimatedSwitcher with Riverpod

Flutter tips Published on

Looking to make your text changes in Flutter look smooth and dynamic? Combining AnimatedSwitcher with a state management solution like Riverpod is a great way to do it.

Animate Text Changes Easily

When your app's state updates and causes a Text widget to display different content, you might want that change to animate instead of just popping into place. AnimatedSwitcher is perfect for this.

How AnimatedSwitcher Works

AnimatedSwitcher is a built-in Flutter widget that helps animate the transition between its old child widget and its new child widget when the child changes. By default, it uses a fade animation, but you can customize it using the transitionBuilder.

Using Riverpod for State

Riverpod is used here to watch the application state. When the state changes, the widget rebuilds with the new data. This rebuild is what AnimatedSwitcher needs to potentially trigger an animation.

The Magic is in the Key

The most important part for making AnimatedSwitcher work when the same type of widget (like a Text widget) updates its content is using a ValueKey. You create a ValueKey based on the changing value from your state.

Like this:

ValueKey(state.value.myText)

When the value derived from your state changes, the ValueKey changes. AnimatedSwitcher sees the child widget now has a different key than before, thinks it's a brand new widget, and runs the animation.

Example Animation

The code example shows using a ScaleTransition within AnimatedSwitcher's transitionBuilder to make the text scale in and out when it changes over a duration (e.g., 250 milliseconds) with a specific curve (e.g., Curves.easeInOut).

Not Just for Text

While this example focuses on animating text, AnimatedSwitcher can be used to animate the transition between any two widgets.

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 AnimatedSwitcher?

It's a Flutter widget designed to animate the transition between its current child and a new child when the child widget changes. It's great for making UI updates look smooth.

Why use Riverpod with AnimatedSwitcher?

Riverpod helps manage and expose your application's state. By watching the state with Riverpod, your widget automatically rebuilds whenever the relevant state data changes, which is the trigger AnimatedSwitcher needs to potentially start an animation.

Why is the 'key' property important for AnimatedSwitcher?

AnimatedSwitcher uses the widget's key to determine if the child has truly 'changed'. If the key remains the same but the widget's properties change, AnimatedSwitcher might not animate. By assigning a unique key (like a ValueKey) based on the data that changes, you signal to AnimatedSwitcher that it should treat the new widget as different from the old one and run the animation.

Can AnimatedSwitcher animate things other than text?

Yes! Although this example shows animating text, AnimatedSwitcher can animate the transition between any type of widget. You can put icons, containers, or custom widgets inside it and it will animate when the child (or its key) changes.

Read more
You may also be interested in
Flutter tips: how to create a responsive layout  blog card image
Flutter tips: how to create a responsive layout
Published on 2025-05-03
How to open the system app settings page  blog card image
How to open the system app settings page
Published on 2025-05-02
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved