Getting to know Flutter: Hero animations

Do you want to know how to make beautiful animations where the same image or text is animated alongside push/pop animations? This tutorial is for you.

As you already know in Flutter everything is a widget so guess what you should use to do those anima…


This content originally appeared on DEV Community and was authored by TheOtherDev/s

Do you want to know how to make beautiful animations where the same image or text is animated alongside push/pop animations? This tutorial is for you.

As you already know in Flutter everything is a widget so guess what you should use to do those animations? A widget of course and in particular the Hero widget. It will take care of moving and stretching the widgets from the position and size of the first screen to the position and size of the second one. It will make the widget "fly" from one screen to the other (yes that's why it's called Hero).

Here's an example of what you can build using the Hero widget:

Alt Text

Hero animations are one of the easiest animation to build in Flutter, you just have to wrap the 2 widgets on the different screens with an Hero widget and give them the same tag.

Here's a code example:

// Screen 1
...
Hero(
      tag: 'batman_image',
      child: Image.asset(
        'assets/images/batman.png',
        fit: BoxFit.cover,
      ),
    )
...

// Screen 2
...
Center(
      child: Hero(
        tag: 'batman_image',
        child: Image.asset('assets/batman.png'),
      ),
    )
...

The Hero Widget has several parameters that you can use if you want to play with the animations.

The flightShuttleBuilder parameter

The flightShuttleBuilder parameters lets you provide another widget during the transition. The code below will show a red container during the transition that animates the opacity but you can provide a different widget of your choice.

Hero(
      tag: 'batman_image',
      flightShuttleBuilder: (
        BuildContext flightContext,
        Animation<double> animation,
        HeroFlightDirection flightDirection,
        BuildContext fromHeroContext,
        BuildContext toHeroContext,
      ) {
        return AnimatedBuilder(
          animation: animation,
          builder: (context, child) => Container(
            color: Colors.red.withOpacity(1 - animation.value),
          ),
        );
      },
      child: Image.asset('assets/batman.png'),
    ),

The placeholderBuilder parameter

You can provide a Widget as a placeholder using the placeholderBuilderparameter, this will show the widget in the final position of the widget during the transition, for example the following code that returns just a red Container as a placeholder will produce the result that you see in the video. The red container is placed in the space that will be occupied by the image widget, during the transition.

Hero(
      tag: 'batman_image',
      placeholderBuilder: (
          BuildContext context,
          Size heroSize,
          Widget child,
          ) {
        return Container(
          width: heroSize.width,
          height: heroSize.height,
          color: Colors.red,
        );
      },
      child: Image.asset('assets/batman.png'),
    )

Alt Text

The createRectTween parameter

You could also change the position and size of the widgets during the transition using the createRectTween parameter.

Hero(
      tag: 'batman_image',
      createRectTween: (begin, end) {
        // Create and return your custom rect
        return MaterialRectArcTween(begin: begin, end: end);
      },
      child: Image.asset('assets/batman.png'),
    )

If you want to dig deeper into what's behind Hero check out the official documentation https://flutter.dev/docs/development/ui/animations/hero-animations


This content originally appeared on DEV Community and was authored by TheOtherDev/s


Print Share Comment Cite Upload Translate Updates
APA

TheOtherDev/s | Sciencx (2021-04-07T15:26:48+00:00) Getting to know Flutter: Hero animations. Retrieved from https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/

MLA
" » Getting to know Flutter: Hero animations." TheOtherDev/s | Sciencx - Wednesday April 7, 2021, https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/
HARVARD
TheOtherDev/s | Sciencx Wednesday April 7, 2021 » Getting to know Flutter: Hero animations., viewed ,<https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/>
VANCOUVER
TheOtherDev/s | Sciencx - » Getting to know Flutter: Hero animations. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/
CHICAGO
" » Getting to know Flutter: Hero animations." TheOtherDev/s | Sciencx - Accessed . https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/
IEEE
" » Getting to know Flutter: Hero animations." TheOtherDev/s | Sciencx [Online]. Available: https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/. [Accessed: ]
rf:citation
» Getting to know Flutter: Hero animations | TheOtherDev/s | Sciencx | https://www.scien.cx/2021/04/07/getting-to-know-flutter-hero-animations/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.