Hero animations in Flutter: solved exercise
Hero animations in Flutter: solved exercise
The Hero widget creates a continuous visual transition of an element between two routes. Flutter automatically animates the widget’s position, size, and shape from the source to the destination — without a single line of animation code. It is the most impactful technique for connecting screens with a shared visual element.
Problem statement
Implement an image gallery where:
- Thumbnails in the grid each have a
Herowith a unique tag. - Tapping a thumbnail animates the image into the detail screen.
- Going back animates the image back to its original position.
- The detail screen includes an animated title using a text
Hero.
Dependencies
Flutter SDK only.
Full solution
How it works internally
- Flutter identifies pairs of
Herowidgets with the sametagin the source and destination routes. - It creates a temporary overlay layer and “flies” the widget from the source
Rectto the destinationRectduring the transition. - When complete, the widget occupies its final position in the destination widget tree.
Customizing the flight with flightShuttleBuilder
Use it to display a different version of the widget during the flight (for example, the high-resolution version).
Common mistakes
- Duplicate tags on the same route: two
Herowidgets with the sametagon the same screen throwFlutterError: There are multiple heroes that share the same tag. Tags must be unique per route. Textwith Hero withoutMaterial: the text inherits the style of the source screen during the flight, producing unexpected white/black text. WrapTextinMaterial(color: Colors.transparent).- Hero inside
Opacitywith value 0: the source widget must be visible for the flight to work. If you hide it withOpacity(opacity: 0), useVisibilityor remove it entirely.
Hero with go_router
Practical use
Hero animations are used in image galleries, product cards that expand on tap, user avatars that grow when viewing a profile, and any shared element that visually connects two screens.
Recommended next exercise
- Implicit animations in Flutter: solved exercise
- go_router with auth guards in Flutter: solved exercise
- CustomPainter in Flutter: solved exercise
- All Flutter exercises
Guided practice and next step
- More Flutter exercises
- C exercises to strengthen fundamentals
- Programming in C in 100 Solved Exercises
- View the book on Amazon (included in Kindle Unlimited)
- Subscribe to the newsletter
FAQ
Does Hero work with go_router?
Yes, as long as the routes share the same Navigator. In go_router with ShellRoute or nested routes, make sure both pages are under the same Navigator level.
Can I animate multiple Hero elements at the same time in the same transition?
Yes. You can have as many Hero pairs as you want in the same route transition. Each one flies independently.
Does Hero work with showDialog or showModalBottomSheet?
Not directly — these methods create a secondary Navigator. For Hero-style animations in dialogs, use a custom overlay or a full-page transition with PageRouteBuilder.