Flutter animations package - how to await Navigator.push() to do something when user navigates back

Viewed 1081

First of all, I'm using the animations package from Flutter and my question is only about using this package:

Before using that package, I pushed the second view to the Navigator with the await keyword, so when the user goes back from this second view, the code after this gets called:

await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => DetailView()),
    );
loadData();

Now after integrating this animations library, I don't call the Navigator.push() myself, but only define the target widget and the navigation is done by the library. Whereas till now, the code for any list item was just the ListTile widget, now it looks like that:

return OpenContainer(
  openBuilder: (BuildContext _, VoidCallback closeContainer) {
    return DetailView();
  },
  closedBuilder: (BuildContext _, VoidCallback openContainer) {
    return _buildListTile(openContainer);
  },
);

In the _buildListTile method the ListTile is wrapped with an InkWell which takes this VoidCallback openContainer for the onTap parameter.

What I can't find out is how to wait for the user clicking back on the second and thus coming back to the first view. I need to (re)load the data as shown in the first code snippet. Has anyone done that and can tell me? I tried to play around with the openBuilder and closedBuilder, but unfortunately without success...

1 Answers

Thank to this GitHub issue two days ago the user 'The-Redhat' push on animations package master branch the change that enable OpenContainer onClose event

To use it before package official version release simply replace animations package in your pubspec.yaml with

animations:
    git:
      url: git://github.com/flutter/packages.git
      path: packages/animations

At this point you can simply pass your custom function to OpenContainer widget that will be executed every time it will be closed. In you case you can now update data when user come back to "parent widget".

UPDATE From 2 June 2020 this feature is available on animations v1.1.0

Add OpenContainer.onClosed, which is called with a returned value when the container was popped and has returned to the closed state.

Related