Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigator.pop() callback?

Tags:

flutter

dart

Is there a callback function or a way to know that Navigator.of(context).pop()'s animation back into the previous screen has finished? currently Im using await Future.delayed(Duration(milliseconds: 500)); to accomplish this. Is there a better way?

Im using it for showModalBottomSheet(useRootNavigator: true) then Navigator.pop() to close the bottom sheet, then push a new route.

code:

Navigator.of(context).pop();
await Future.delayed(Duration(milliseconds: 500));
Navigator.of(context).push(...) // push new screen
like image 356
AnsellC Avatar asked Jan 01 '23 10:01

AnsellC


1 Answers

Pushing a route already maintains a Future that completes when a pop occurs. This means you can do this:

Navigator.of(context).pushNamed('/someRoute').then((completion){
    //do something
});

The parameter named completion could also be any data you want to retrieve from the screen you are showing. For example let's say I have a group chat and I want to add a user to it. To do that I want to show a screen that allows me to pick a user then pass that user back to the previous screen.

Navigator.of(context).pushNamed('/InviteToChatScreen').then((object) {
    if (object != null) {
        var user = object as User;
        //add them to the chat
    }
});
like image 125
Emmett Deen Avatar answered Jan 02 '23 22:01

Emmett Deen