Promptless AI is here soon - Production-ready contextual code. Don't just take our word for it. Know more
Know More

Flutter Hero Animation: Transform and Fly Widgets Between Screens

No items found.

Nidhi Sorathiya

August 9, 2023

Nidhi Sorathiya

August 9, 2023

Hero animation in Flutter is one of the most popular visual designs which can be easily implemented using Flutter's hero widget. When we discuss hero animation, it refers to a style of animation where a widget(or an element) "flies" between two screens. This pattern of animation is commonly known as shared element transitions or shared element animations.

Hero animations are commonly used in mobile applications. You might have noticed such a transition while using apps. One typical use case scenario would be in a news app, where one would select a news item featuring a thumbnail image of the story in a list. Once selected, the image 'flies' to the top of the detail page, where we find more information about the story.

As we proceed further into these techniques, you will learn how to fly widgets from one page to another, animate transformations of widgets' shapes, and understand the process behind these animations.

Understanding the Concept of the Hero Widget in Flutter

Having already introduced the basics of hero animation in Flutter, let's proceed to explore the hero widget more profoundly.

Creating Hero Animations Using Flutter's Hero Widget

One of the defining features of the hero widget in Flutter is its ability to create visually delightful animations between two pages in an application. What technically happens under the hood is this: two hero widgets — one on each page, carrying the same tag — animate between screens when the user navigates from one page to the next. This transition makes it appear as if the same image or widget is 'flying' from the source route to the destination route, providing a smooth and continuous user experience.

Here is a sample Flutter code snippet for creating hero widgets on two screens:

In this Flutter code snippet, we can create a simple MyApp with two pages that allow us to navigate back and forth. The hero widgets, carrying the same tag 'imageHero' and the same image, animate nicely between the two pages.

Transforming a Hero's Shape

The hero widget in Flutter not only offers simple animations but also lets you transform the widget's shape along its journey from the source to the destination. This can be achieved with custom-built tweens and rect changes. Whether you need to transition a widget from circular to rectangular or vice versa, hero animation offers endless possibilities to the developers.

Fundamentals of Hero Animation

Now we've seen the basic usage of the hero widget, let's better understand the principles and structure behind it.

Basic Structure of a Hero Animation

The structure of a hero animation in Flutter revolves around two hero widgets with the same tag, one each on the source and destination routes.

In the animation process, the hero widget 'flies' from the source route to the destination route. Simultaneously, the destination route (minus the hero) gradually fades in. This makes it seem as if the hero is 'travelling' between screens, creating a visually pleasing experience for the user.

It's essential to note that both hero widgets have the same tag — this is crucial for Flutter to recognize which widgets to animate between screens. The tags are typically objects that represent the underlying data. For instance, you can use the unique ID of an item as the hero tag.

Behind the Scenes: How Flutter Performs a Hero Animation

Flutter uses an intermediate step to perform the hero transition animation. When pushing a route onto the Navigator's stack triggers the animation, Flutter calculates the destination hero's bounds change, during which the hero flies in an application overlay. This overlay ensures that the hero appears on top of both routes during the transition.

Flutter also implements a rectangle tween, known as RectTween, to define the hero's boundary as it flies from the source to the destination route. By default, Flutter uses the MaterialRectArcTween instance, which animates the rectangle's opposing corners along a curved path.

Implementing Standard Hero Animations

In a project that uses hero animations in Flutter, understanding how to structure and trigger them effectively is crucial. Let's delve deeper into implementing hero animations.

Flutter Hero Animation: Transform and Fly Widgets Between Screens
Implementing Standard Hero Animations

Defining Source and Destination Hero Widgets

The first step to the implementation of hero animations is defining the source and destination hero widgets. Both heroes are required to specify their graphical representation (typically an image) and an identical tag. These heroes' tags are usually objects that represent the underlying data. Remember, the two hero widgets must share the same tag for the hero transition animation to take place successfully.

In this Flutter code snippet, notice that both SourceHero and DestinationHero have the same tag and display the same image. They represent the source and destination heroes respectively for the hero animation.

Crafting Routes that Contain Destination Hero

Once the heroes are in place, you need to define a route that contains the destination hero. The destination route essentially defines the widget tree that exists at the end of the animation, including the destination hero.

This is when the Navigator comes into play — pushing the destination route onto the Navigator's stack triggers the animation. The Navigator recognizes the heroes with the same tag and initiates an overlay transition from the source hero to the destination hero.

Animating Hero Transitions

With the necessary widgets and routes in place, it's time to delve into how Flutter animates the transformation of the hero widget from the source route to the destination route.

Flutter Hero Animation: Transform and Fly Widgets Between Screens
Animating Hero Transitions

Triggers for Hero Animation

In Flutter, the primary triggers for hero animation are the pushing and popping of routes from the Navigator's stack. The Navigator, being a stack of routes in Flutter, initiates the hero animation for every pair of heroes with matching tags in the source and destination routes when you push or pop a route.

Pushing a route onto the stack, such as when a user taps a hero widget, initiates the hero's flight from the source route to the destination route. Popping a route, such as when the user uses a back swipe gesture or presses the back button, triggers the hero's return journey.

This interaction is made possible by the 'InkWell' widget surrounding the hero widget in both routes. In Flutter, InkWell is a material design concept that implements a rectangular area of Material that responds to touch. For example:

In this code sample, we have an InkWell widget that wraps the Hero widget. When a user taps on the hero, the Navigator pushes a new route onto the stack, triggering the hero animation.

The Overlay in Flutter's Hero Widget

As we explored earlier, during the transition, the hero flies in an application overlay, appearing on top of both routes. This is made possible by placing the destination hero in the overlay at the same location and size as the source hero. As the hero's flight progresses, the overlay adjusts the rectangular bounds of the hero using a Tween of type Rect (RectTween).

Flutter's Process for Hero Transition

Having learned about the triggers for the hero animation and the role of the overlay, let's delve into how Flutter performs the transition from one route to another using the hero animation.

Before the Transition

Flutter Hero Animation: Transform and Fly Widgets Between Screens
Before the Transition

Before the transition begins, the source hero waits in the source route’s widget tree. At this point, the overlay is empty, and the destination route does not exist yet.

During the Transition

Flutter Hero Animation: Transform and Fly Widgets Between Screens
During the Transition

Once the Navigator triggers the animation, several things occur.

Initially, Flutter calculates the final location and size of the hero on the destination route offscreen using a curved path as described in the Material motion specification. Then, it places the destination hero on the overlay at the same location and size as the source hero.

A point worthy of note here is that adding a hero to the overlay changes its Z-order so that it appears on top of all routes. After this, the source hero moves offscreen.

Throughout the hero's flight in the overlay to its final position, its rectangular bounds, as specified in Hero’s createRectTween property, get animated using Tween<Rect>. By default, Flutter uses an instance of MaterialRectArcTween for this, which animates the rectangle's opposing corners along a curved path.

After the Transition

Flutter Hero Animation: Transform and Fly Widgets Between Screens
After the Transition

Once the flight completes, Flutter moves the hero widget from the overlay to the destination route, leaving the overlay empty. At this point, the destination hero appears in its final position in the destination route.

Popping the route triggers the same series of actions as before but in reverse order, animating the hero back to its original size and location in the source route.

Exploring Essential Classes for Hero Animations

Flutter offers several easy-to-use classes to implement Hero Animations. These classes form the backbone of a hero animation, serving different purposes in the hero's flight.


The Hero class is the fundamental widget that transitions across screens. It is defined by a tag that uniquely identifies it. Notice that it is this tag that allows Flutter to recognize the widget that must be animated when we move from one screen to another.

In the code snippet above, we are defining a Hero widget with the tag 'hero1'.


We surround the hero with an InkWell widget, which comes with an onTap() method. We can hence make use of InkWell to define what happens when the user taps on our hero. When the user taps on the hero, we can use Navigator to push a new route to the Navigator's stack and begin the hero's flight.


The Navigator holds and manages a stack of routes. It is an essential class for creating multi-screen applications. Pushing and popping routes using the Navigator triggers the animation for pairs of heroes with matching tags in the source and destination routes.

In the provided code snippet, we are pushing a new route onto the Navigator's stack when the user taps the hero. This trigger initiates the hero's flight from the source route to the destination route.


A Route describes a page or screen in a Flutter app. We can define our destination route, which may contain key elements like Scaffold and AppBar along with the destination hero.

Detailed Breakdown of Standard Hero Animations

Understanding and implementing standard hero animations are fundamental for any developer intending to use Flutter's hero widget. Let's dive into it.

Example Code for Standard Hero Animation

Creating a standard hero animation is straightforward. A standard hero animation flies the hero from one route to a new route, usually landing at a different location and with a different size.

An example of the code necessary to create such an animation is below:

In this example, we defined a SourceHero and a DestinationHero. Each has the Hero widget representing an image, and both carries the same tag 'imageHero'. We've also encapsulated the hero code within an InkWell. On tapping the widget, we pushed a new route to the Navigator’s stack, triggering the animation.

Key Components and Understanding of HeroAnimation Class

The HeroAnimation class is a key component for utilizing and managing Hero animations. This class facilitates defining heroes and setting up the transition.

In the HeroAnimation class, you can set the speed of the animation, design the transition, and define the destination route for the hero’s journey. It's in this class you also manage the user gestures, such as onTap to push a new screen onto the Navigator’s stack, initiating the Hero Transition.

Radial Hero Animation in Flutter

Beyond standard hero animation, Flutter also offers the option to create more intricate and engaging animations such as the radial hero animation.

Radial Transformation in Hero Animation

In a radial hero animation, as the hero flies between routes, its shape appears to change from circular to rectangular. This transformation is visually impressive and can add a unique aesthetic to your application.

In addition to the visual appeal, the radial hero animation also provides a clear guide for the user, smoothly transitioning them from one screen to another.

The Process and Implementation of Radial Hero Animation

Implementing a radial hero animation involves calculating and animating the intersection of two clip shapes: a circle and a square. Throughout the animation, the circular clip (and the image) scales from a minimum radius to a maximum radius, while the square clip maintains a constant size. At the same time, the image flies from its position on the source route to its position on the destination route.

To simplify this process, Flutter provides a MaterialRectCenterArcTween, which defines the tween that animates the hero's rectangle from the starting point to the endpoint.

Working with Radial Hero Animation Code

Now that we understand what radial hero animation is and its visual appeal let's look at some ways to code a radial hero animation.

Example Code for Radial Hero Animation

Creating a radial hero animation may seem complex, given that it involves manipulating two different shapes (a circle and a square). However, with the use of Flutter's predefined classes and functions, the steps required become more approachable. Here's a simplified code snippet for radial hero animation:

In this example, we start with an image. When you tap this image (thanks to the InkWell widget), a new route (detail page) is pushed onto the stack via the Navigator’s push method. This causes our hero (the image) to "fly" across the screen, morphing from a circular to rectangular shape.

Incorporating RadialExpansion Widget in Radial Hero Animation

An essential part of the radial hero animation is dealing with the change in shape of the widget. This is where the RadialExpansion Widget comes into play, which builds a widget tree that includes two clip elements: ClipOval and ClipRect. As the widget flies from the source route to the destination route, ClipRect remains a constant size while ClipOval scales from the minimum radius to the maximum radius, changing the perceived shape of our hero image.

Implementing a RadialExpansion widget in your code would look something like this:

In this example, we added clipRectSize so that we can systematically control the size of the ClipRect inside our widget. We also allow child widgets through the child parameter, offering more flexibility with what widget to animate.

Improving Hero Animations in Flutter

After diving into creating standard and radial hero animations, let's now turn our attention to tips and techniques to ensure these animations run smoothly and look fantastic.

Suggestions for Optimizing Hero Animation

While working with hero animations in Flutter, there are a few key considerations you should bear in mind:

  • Consistency in Tags: Since tags are needed to identify heroes across routes, be careful to use the exact same tag for the same hero in different routes.
  • Use of Unique Tags: To prevent any unexpected behavior, each hero should have a unique tag unless they connect the same two routes.
  • Proper Animation Speed: The speed of a hero's flight can greatly affect user experience. Speeds that are too fast may make it difficult for the user to follow the animation, while overly slow speeds may wear out user patience. A sweet spot, generally, is ensuring the animation completes in 300-400ms.
  • Appropriate Hero Animation Type: While deciding to use a standard or radial hero animation, consider the user interaction with your app. Standard animations are typically better suited for list-type data, while radial animations are excellent for applications featuring icons and circular images.
  • Limit "Jumping": Finally, avoid scenarios where the hero "jumps" or quickly changes in size by placing heroes at similar locations and sizes in different routes.

Tips and Tricks for Better Hero Animation

Some handy tools can enhance your experience while working with hero animations. Flutter offers properties like flightShuttleBuilder and transitionOnUserGestures in the Hero class.

The flightShuttleBuilder property allows customization of the hero widget that flies between routes, and transitionOnUserGestures enables users to control the animation when swiping back to the previous route.

Improving Hero Animations in Flutter

So, you've got your hero animations working, and they look pretty good. But there are a few things you can do to take your animations to the next level and make them look even better.

Specify a Route using MaterialPageRoute, CupertinoPageRoute, or build a Custom Route using PageRouteBuilder

The type of page route used to navigate to the next page can have a significant effect on how the Hero's animations are handled. MaterialPageRoute and CupertinoPageRoute are two options that come built-in with Flutter and provide some pre-defined animations. However, if you want full control, you can use PageRouteBuilder to build your custom routes and define precisely how the animations should run.

Change the Size of the Image at the End of the Transition by Wrapping the Destination’s Image in a SizedBox

To easily adjust the ending size of your Hero widget, consider wrapping the Destination Hero in a SizedBox. This allows you to explicitly set the width and height that the Hero widget should animate towards.

In this code snippet, the destination hero is wrapped in a SizedBox, which specifies the hero size once the animation is finished.

Change the Location of the Image by Placing the Destination’s Image in a Layout Widget

Just as you can control the ending size of your Hero, you also have full control over its final location. By placing the Hero within different layout widgets, such as Container, Padding, or Align, you can specify where on the screen the Hero should animate towards.

Avoiding Common Issues in Hero Animations

One common problem developers run into while implementing Hero animations is the 'Hero animations in parallel navigation history' exception. This happens if you push two routes to the Navigator's stack and both routes contain a Hero with the same tag. Flutter won't be able to manage the simultaneous animations of two heroes with the same tag. To avoid this, make sure every hero has a unique tag unless they connect the same two routes.

Using the HeroController Class

One fundamental class is HeroController. This class creates a new Hero controller that can be used to manually manage hero animations. By setting the createRectTween property, we get full reign over how the target and destination rectangles interpolate during a hero's flight.

Ensuring Smooth Animations

To create a smooth transition, consider wrapping the widgets before and after hero animations are the same in terms of widget hierarchy.

Also, it might be tempting to use high-resolution images as your heroes, but remember that the larger the image you're trying to animate, the more memory it uses and the longer it may take to load, which can cause a noticeable delay in the animation.

Debugging Hero Animation

Flutter also comes with a handy debugging feature for hero animations. By setting the timeDilation property to a number larger than one, we can slow down the animations, making it easier to observe and debug them.

The Power of Hero Animations in Flutter using WiseGPT!

Hero animation is unarguably one of the most potent tools in the world of Flutter app development. Not only does it provide a smooth and visually appealing transition between widgets, but it also plays a crucial role in enhancing the users' experience within an app.

WiseGPT is a game-changing plugin that brings a revolutionary code generation experience to your Flutter projects. Specifically designed for Flutter, this powerful IDE plugin allows you to effortlessly create complex animation code without any output size restrictions, significantly enhancing your productivity.‍

Flutter Hero Animation: Transform and Fly Widgets Between Screens

Seamless Integration with Your Coding Style

One of the standout features of WiseGPT is its ability to mirror your coding style seamlessly. No more worrying about disjointed code snippets or inconsistent formatting. The code generated by WiseGPT effortlessly blends with your existing codebase, maintaining consistency and reducing the need for tedious adjustments.

This means more time spent building exceptional animations and less time grappling with code structure.

Intuitive and Promptless Code Generation

Gone are the days of dealing with prompts or spending precious minutes tweaking the generated code. WiseGPT intuitively understands your requirements without any prompts, ensuring a smooth and efficient code generation process. It effortlessly grasps the essence of your animation needs and generates the corresponding code, saving you valuable time and effort.

Automated File and Function Creation

WiseGPT takes automation to a new level by automatically creating files and functions for your animations. No more manual code management or setting up boilerplate code. With WiseGPT, you can focus on defining the core aspects of your animations while the tool handles the rest.

This automation streamlines your development workflow and allows you to build more robust and scalable Flutter applications in a fraction of the time.

Elevate Your Flutter Development Experience

Incorporating WiseGPT into your Flutter development toolkit empowers you to take your animation projects to new heights. This plugin streamlines the code generation process, saving you time and effort while maintaining the quality of your animations. Say goodbye to mundane coding tasks and embrace the seamless integration and automation that WiseGPT brings to your Flutter projects.

Flutter Hero Animation: Transform and Fly Widgets Between Screens
Elevate Your Flutter Development Experience

Unleash New Possibilities with WiseGPT

By combining the power of Flutter Animations with WiseGPT, you unlock a whole new realm of possibilities for your app development. Embrace the potential of this innovative tool and witness a significant transformation in your Flutter projects.

Whether you're a skilled developer or just starting with Flutter, WiseGPT is a must-try addition to your toolkit. Streamline your animation workflow and unleash your creativity with WiseGPT for Flutter!

Frequently asked questions

Frequently asked questions

No items found.