Back to Articles

Intermediate

Using AnimatedSwitcher and AnimatedCrossFade in Flutter

· 6 min read

Using AnimatedSwitcher and AnimatedCrossFade in Flutter

The Problem: No Animation When the Child Changes

When you call setState and return a different widget from build, Flutter removes the old one and inserts the new one instantly by default. Going from a loading icon to a checkmark, or from one piece of text to another, feels like an abrupt jump. Smoothing that out doesn't require setting up a custom AnimationController everywhere; AnimatedSwitcher and AnimatedCrossFade do this job out of the box.

A scale transition between changing icons on the left, a cross-fade from skeleton to real content on the right
AnimatedSwitcher (left) and AnimatedCrossFade (right) side by side.

How AnimatedSwitcher Works

Whenever AnimatedSwitcher's child parameter changes, it animates the old widget out and the new widget in. The default transition is a FadeTransition: the old widget fades away while the new one fades in. All you have to do is change the child normally; AnimatedSwitcher manages the animation itself.

AnimatedSwitcher(
  duration: const Duration(milliseconds: 250),
  child: isSaving
      ? const SizedBox(
          key: ValueKey('saving'),
          width: 20,
          height: 20,
          child: CircularProgressIndicator(strokeWidth: 2),
        )
      : const Icon(Icons.check, key: ValueKey('done'), color: Colors.green),
)

The Role of the Key

AnimatedSwitcher decides whether the old and new child are 'the same widget' by looking at the widget's type and its key. If two Text widgets only change their text and carry no key, Flutter treats that as the same widget being updated, and no transition animation fires at all. Giving a different ValueKey every time the value changes is how you tell AnimatedSwitcher 'this is now a different widget, run the transition'.

// Animation does NOT fire: it's the same Text widget, only its content changes
AnimatedSwitcher(
  duration: const Duration(milliseconds: 300),
  child: Text('$counter'),
)

// Animation fires: each value carries a different ValueKey
AnimatedSwitcher(
  duration: const Duration(milliseconds: 300),
  child: Text('$counter', key: ValueKey(counter)),
)

Customizing with transitionBuilder

If the default fade isn't enough, transitionBuilder lets you fully customize the transition. switchInCurve and switchOutCurve let you define different easing curves for the incoming and outgoing widgets — for example, the incoming widget could grow in with a slight bounce while the outgoing one simply fades away.

AnimatedSwitcher(
  duration: const Duration(milliseconds: 300),
  switchInCurve: Curves.easeOutBack,
  switchOutCurve: Curves.easeIn,
  transitionBuilder: (child, animation) {
    return FadeTransition(
      opacity: animation,
      child: ScaleTransition(scale: animation, child: child),
    );
  },
  child: Text('$counter', key: ValueKey(counter)),
)

AnimatedCrossFade: Transitioning Between Two Fixed Widgets

While AnimatedSwitcher works with any number of children that come and go by key, AnimatedCrossFade is designed specifically to transition between exactly two fixed widgets (firstChild, secondChild). Which one is shown is determined by crossFadeState (CrossFadeState.showFirst / showSecond), and both children are built continuously — which also lets sizeCurve animate the height transition smoothly if the two widgets have different sizes.

AnimatedCrossFade(
  duration: const Duration(milliseconds: 300),
  crossFadeState: isExpanded
      ? CrossFadeState.showSecond
      : CrossFadeState.showFirst,
  firstChild: const Text('Short summary text...'),
  secondChild: const _FullArticleBody(),
  sizeCurve: Curves.easeInOut,
)

Which One, and When?

• If the number of states is variable or open-ended (loading / error / data, for example), or it's fine for only one child to be built at a time: AnimatedSwitcher. • If you're switching between exactly two known widgets and both need to exist in the background (for example, for a height animation): AnimatedCrossFade. • Keep in mind that AnimatedCrossFade continuously builds both children, so be mindful when using it with expensive widgets.

Conclusion

AnimatedSwitcher and AnimatedCrossFade are the fastest way to add transition animations without wiring up an AnimationController by hand. Don't forget the role of the key in AnimatedSwitcher — no key means no animation. Reach for AnimatedCrossFade when you're switching between exactly two known states and want their sizes animated together too.