The new ‘animations’ package explained — gskinner blog

gskinner team
Mar 20, 2020 · 4 min read

The Flutter team recently dropped a great new transitions package, based on the new Material 2 design spec, the somewhat ambiguously-named: animations package.

They are super cool to look at and appear to be highly performant. The only issue? The examples they’ve provided with the package are pretty hard to follow (coming in at close to 1500 lines!) and there’s no code snippets at all in the README.

But fear not! This package is actually extremely simple to use once you clear away the noise, and can see how it works.

First, lets recap. The package itself is composed of 4 “transition patterns”:

  • SharedAxis — A PageRoute that transitions on the X, Y or Z axis, where Z represents scale. Recommended for pages that are related to one another (login, onboarding etc)
  • FadeThrough — A PageRoute where outgoing elements fade out, then incoming elements fade in and scale up. Recommended for pages that have no strong relationship to eachother.
  • FadeScale- A Modal PageRoute (meant for dialogs). Elements that enter use a quick fade in and scale from 80% to 100%. Elements that exit simply fade out.
  • OpenContainer — A container that grows to fill the screen to reveal new content when tapped. Similar to a Hero widget.

We’ll show you how to use the various page routes first, and then we’ll talk a bit more about using OpenContainer.

They’re all just page transitions…

Using SharedAxis, FadeThrough and FadeScale really could not be easier. They’re just Transitions you can use inside your PageRoutes, so you can just define some static builder methods like so:

Now you can call any of these transition types with the following:

That’s really all there is to it for those. They’re just fancy page routes, that you can use like any other! You can read more about the recommended usage of each pattern in the Material Design Specs.

OpenContainer Widget

You can kind of think about OpenContainer as an easier to use Hero. The main difference is that while a Hero could have any defined region, Container is meant to always have a partial-screen portion, and a full-screen portion. Also, no matching tag is required!

As you can see, it’s also a little more robust than the Hero. By using a simple cross-fade + scale effect it’s much more forgiving of layout differences between the start and end views.

Where Hero would really have to be massaged to look good, OpenContainer tends to look nice out of the box. It also doesn’t seem to have any of the Text rendering issues that sometimes plague Hero. All in all, a much more practical and easier to use solution, producing a fairly similar effect in the end.

There is really no great trick to using OpenContainer it’s just a simple little widget, with a couple of builders:

One important thing to note about OpenContainer, is that it does actually use a PageRoute under the hood. This means a simple Navigator.pop() is all you need to close the container when it’s open, and that hardware back button support will work as you’d expect it. *fist pump*

The other thing that needs explaining here is this action() callback. The purpose of these, are to manually open or the close the container.

By default, the OpenContainer will open itself if you have .tappable set to true, and it will close itself, when you Navigator.pop(), but what if you don’t want the entire Widget to be tappable? That’s where the actions come in.

With the action, we can set .tappable to false, and just open things ourselves by calling the action() inside our own button handler:

Here you can see that we used a button, to manually call the action, which will open up the OpenContainer. We could also do the same thing inside the openBuilder, but as far as we know, this is the same as just calling Navigator.pop().

Speaking of Navigator.pop, there is a rather large flaw currently in the component. The Future that is normally generated by a Navigator.push() is not made available anywhere. This means that there is no way to await the results from the page that was opened, which is a very common use case in Flutter.

This does limit the potential of this component in a pretty severe way, but it should also be a relatively easy fix, so hopefully the team will add that in. If you agree, you can up-vote the issue here:

That’s about it! Thanks for checking out the post, and we hope this has given you a good overview of the new animations package and some of its capabilities.

Flutter Community

Articles and Stories from the Flutter Community

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store