问题
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