A month of Flutter: the real hero animation

For the last post before the month's wrap up tomorrow, I wanted to do something more fun: use a hero animation between the home page list and the individual post page.

When I first implemented the Hero animation it never worked going back from a PostPage to the HomePage. The reason was that HomePage would get rerendered and that would generate new fake posts. So I moved the fake data generation up a level to MyApp and pass it into HomePage. This is more realistic as going to the HomePage shouldn't request the Posts every time.

HomePage(
  title: 'Birb',
  posts: _loadPosts(context),
)

The PostPage implementation is a simple StatelessWidget that takes Post and renders a PostItem. This will become more complex as things like comments and likes are implemented but works for now.

class PostPage extends StatelessWidget {
  const PostPage({
    Key key,
    @required this.post,
  }) : super(key: key);

  final Post post;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Post'),
        centerTitle: true,
        elevation: 0.0,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 8.0),
          child: PostItem(post),
        ),
      ),
    );
  }
}

With PostItem being used to render on the HomePage and on the PostPage, wrapping the Image in a Hero is handled in a single place. tag is how Hero knows what to transition between pages.

Hero(
  tag: post.id,
  child: ClipRRect(
    child: Image.network(post.imageUrl),
    borderRadius: BorderRadius.circular(10.0),
  ),
)

The last piece is navigating from PostList to PostPage when a user taps on a PostItem. I'll handle this with an InkWell widget so there is a nice Material ripple.

InkWell(
  onTap: () => _navigateToPost(context, post),
  child: PostItem(post),
)

The navigation is more complex then opening the registration page for two reasons. Named routes don't support parameters and I wanted a simple transition between the rest of the content on the page.

void _navigateToPost(BuildContext context, Post post) {
  Navigator.of(context).push(
    PageRouteBuilder<PostPage>(
      pageBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
      ) {
        return PostPage(post: post);
      },
      transitionsBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        Widget child,
      ) {
        return FadeTransition(
          opacity: animation,
          child: child,
        );
      },
    ),
  );
}

Here I will push a PageRouteBuilder onto the navigation stack. PageRouteBuilder has two key builders in use here. pageBuilder builds the widget that should be rendered as the new page and transitionBuilder specifies how to transition between the old and new pages. Note that this FadeTransition is not related to implementing Hero earlier.

The tests for PostPage is simple and just checking that PostItem is rendered. I did update the PostItem test to expect that its Hero widget had the correct tag value.

expect(tester.widget<Hero>(hero).tag, post.id);

PostsList tests had to be wrapped in a MaterialApp as InkWell must have a Material widget ancestor.

The navigation and animation from PostsList to PostPage is now doing more work so I replaced several pump pauses with pumpAndSettle.

Here is the fancy Hero animation:

Code changes

Posts in this series

  • A month of Flutter
  • A month of Flutter: create the app
  • A month of Flutter: configuring continuous integration
  • A month of Flutter: continuous linting
  • A month of Flutter: upgrading to 1.0
  • A month of Flutter: initial theme
  • A month of Flutter: no content widget
  • A month of Flutter: a list of posts
  • A month of Flutter: extract post item widget
  • A month of Flutter: post model and mock data
  • A month of Flutter: rendering a ListView with StreamBuilder
  • A month of Flutter: Stream transforms and failing tests
  • A month of Flutter: real faker data
  • A month of Flutter: rendering network images
  • A month of Flutter: FABulous authentication
  • A month of Flutter: configure Firebase Auth for Sign in with Google on Android
  • A month of Flutter: configure Firebase Auth for Sign in with Google on iOS
  • A month of Flutter: Sign in with Google
  • A month of Flutter: mocking Firebase Auth in tests
  • A month of Flutter: delicious welcome snackbar
  • A month of Flutter: navigate to user registration
  • A month of Flutter: user registration form
  • A month of Flutter: testing forms
  • A month of Flutter: setting up Firebase Firestore
  • A month of Flutter: awesome adaptive icons
  • A month of Flutter: set up Firestore rules tests
  • A month of Flutter: Firestore create user rules and tests
  • A month of Flutter: WIP save users to Firestore
  • A month of Flutter: user registration refactor with reactive scoped model
  • A month of Flutter: the real hero animation
  • A month of Flutter: a look back