Flutter Hero Animations: Guiding the User's Eye
A Hero animation makes a widget appear to fly from one screen to another, connecting two UIs. It's perfect for transitioning a thumbnail to a detail page, guiding the user's focus. The main footgun is using non-unique tags, which causes animations to fail.
WHY IT EXISTS To solve the problem of user disorientation during screen transitions. A standard page route can feel like a jarring cut, leaving the user to re-establish context. Hero animations create a continuous visual narrative, guiding the user's eye and reinforcing the connection between the two screens.
THE MENTAL MODEL Think of the widget as the "hero" of a story. When you change scenes (screens), the camera follows the hero. You wrap an element on screen A (like a thumbnail) and the same logical element on screen B (the full-size image) in a Hero widget. Flutter then animates the "hero" from A to B.
HOW IT WORKS You wrap the starting and ending widgets in a Hero widget. Both Hero widgets must share the same tag object, which must be unique within the current navigation stack. When Flutter's Navigator pushes or pops a route, it scans the widget trees for pairs of Hero widgets with matching tags. It then calculates the difference in position, size, and shape and creates a smooth tween animation between the two states. The animation happens in an overlay, appearing above both screens.
WHEN TO USE IT Use this for transitions where an element is visually represented on both the source and destination screens. It's perfect for list-to-detail views, such as an e-commerce app where a product image flows from a grid into the product detail page, or a contact list where an avatar expands into a profile screen.
WHEN NOT TO USE IT Avoid Hero animations for elements that don't have a clear visual correspondence between screens. Don't use it for animating very complex widgets with their own internal state, as it can lead to visual glitches. For simple fades or slides of an entire page, a standard PageRouteBuilder is more appropriate and efficient.
ONE CANONICAL EXAMPLE The most common example is a photo gallery. A grid of small thumbnails is displayed. When a user taps a thumbnail, a new screen opens showing the full-size image. The Hero animation makes the tapped thumbnail appear to grow and move into its final position on the new screen. The tag for each Hero must be unique, typically tied to the photo's ID.
Read the original → docs.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.