What is a Hero Animation?
A Hero animation is a shared element transition where a widget appears to 'fly' from one screen to another. It's often used for images, avatars, or any prominent element that the user taps to see more details. Flutter's built‑in Hero widget makes it easy to create these smooth, delightful transitions with minimal code.
How Hero Works
The Hero widget requires a unique tag (usually a string) that identifies the widget. When a route push/pop occurs, Flutter looks for a Hero with the same tag on both the source and destination screens. It then creates an overlay that animates the widget from the source position and size to the destination position and size, while fading the original widgets in/out.
Basic Hero Animation
To create a hero transition, wrap the widget you want to animate with a Hero widget on both screens, using the same tag. Usually, the source screen's hero is inside an InkWell or GestureDetector to handle tap navigation.
When you navigate from the source screen to the destination screen, the avatar will smoothly animate from the small size to the large size, creating a seamless effect.
Hero Tags
The tag can be any object, but it's typically a string. For lists, you might use a unique identifier like an index or an item's ID. If you use the same tag on multiple heroes in the same route, Flutter will throw an error. Keep tags unique per route.
Customizing the Hero Transition
You can customize the transition using the flightShuttleBuilder, placeholderBuilder, and createRectTween properties.
Flight Shuttle Builder
flightShuttleBuilder lets you replace the flying widget with a custom widget during the transition. It gives you access to the context, the hero's size and position, and the animation progress.
Placeholder Builder
placeholderBuilder allows you to show a placeholder while the hero is flying. This is useful when you want to keep the original widget visible (or fade it) during the transition.
Custom Rect Tween
createRectTween lets you control how the hero's bounds animate (e.g., using a curved motion). You can supply your own RectTween subclass.
Heroes with Different Widget Types
Heroes can animate between different widget types (e.g., a CircleAvatar to a ClipRRect image). As long as the Hero widget has the same tag, Flutter will interpolate the size, position, and other visual properties. You may need to provide a flightShuttleBuilder if the default transition doesn't look good.
Common Mistakes
- Duplicate tags in the same route: Each Hero tag must be unique per route.
- Forgetting to wrap both source and destination: Both screens must have a
Herowith the same tag.
- Forgetting to wrap both source and destination: Both screens must have a
- Using a non‑unique tag in lists: Use unique identifiers (e.g., item ID) to avoid collisions.
- Not disposing controllers: If you create custom animations, remember to dispose them.
- Over‑complicating with custom builders: Start simple; only add customisation if needed.
Best Practices
- Use unique tags: For lists, combine a prefix with the item's ID (e.g.,
'image_${item.id}').
- Use unique tags: For lists, combine a prefix with the item's ID (e.g.,
- Test with different screen sizes: The animation should look good on all devices.
- Keep the hero widget focused: The hero should be the only widget animating; use
childfor the content.
- Keep the hero widget focused: The hero should be the only widget animating; use
- Use
flightShuttleBuildersparingly: Only when you need special effects; the default transition is often sufficient.
- Use
- Consider performance: Heroes are efficient, but avoid heavy widgets inside the hero if possible.
Key Takeaways
- Hero animations create seamless shared element transitions.
- Use the same
tagon both the source and destinationHerowidgets.
- Use the same
- Tags must be unique per route.
- Customize transitions with
flightShuttleBuilder,placeholderBuilder, andcreateRectTween.
- Customize transitions with
- Heroes work between different widget types (e.g.,
CircleAvatartoImage).
- Heroes work between different widget types (e.g.,
- Always test with real data to ensure smooth animations.