Flutter - How to get notified when the page I pushed was popped by back button?

隐身守侯 提交于 2021-01-07 01:23:51

问题


Assume this scenario: page1 Navigator.push to page2. user on page2 clicks the back button, so page2 pops and page1 regains view. How do I catch this event on page1?


回答1:


You can check like this by passing parameter from Navigator.pop...

from the second screen:-

 Navigator.pop(context, 'updateList'),

and on the first screen check like this and pass condition which you want:-

FloatingActionButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (BuildContext context) =>
                            new ManageAssignments(),
                      ),
                    ).then((val) {
                      if (val == 'updateList') getAssignementList();
                    });
                  },
                  backgroundColor: Theme.of(context).primaryColor,
                  child: Icon(Icons.add),
                )

Hope it will work for you :-)




回答2:


So when Navigator.push() happens it returns a Future. And on Navigator.pop() the future is resolved. You can return value from Navigator.pop like this Navigator.pop(context, "MyResult"); That result will be captured like this - final result = Navigator.push(). You can use the result in the widget from where Navigator.push() was called.




回答3:


Use WillPopScope widget in the pop screen, then to get notified when screen is popped use onWillPop function.

Like this:

return WillPopScope(
      child: Scaffold(
          body: new Container(), // your body content
      onWillPop: (){
      //Screen is poped.
    },
    );
    
)


来源:https://stackoverflow.com/questions/58850661/flutter-how-to-get-notified-when-the-page-i-pushed-was-popped-by-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!