Flutter,如何在 Navigator.pop 上刷新最后一个页面状态
Flutter, How to refresh the last page state on Navigator.pop
我必须翻页,p1 和 p2。当您启动该应用程序时,您位于 p1 并且可以打开 p2。我希望当您使用后退按钮离开 p2 时,p1 状态会刷新(就像再次调用 initState() 而无需使用 Navigator.push 或使用 WillPopScope 创建新路由)。
可能吗?
您可以调用方法在弹出第二个屏幕后刷新您的第一页状态。
p1:
refreshState() {
// change your state to refresh the screen
}
Navigator.push(context, MaterialPageRoute(builder: (context) => p2()),).then((res) => refreshPage());
p2:
Navigator.pop(context);
是的,你可以。
当你推送一个新页面时,你可以有它的弹出回调。
// If you want to get particular type of data arguments, then can add it with MaterialPageRoute<Data_Type_Value_You_Want_In_Return>
Navigator.push(context, MaterialPageRoute<bool>(builder: (context) => page2()),).then((bool res) {
// check here if you got your data or not
if(res!=null && res==true){
refreshPage();
}
});
在您的下一个屏幕中
// Let say if i want to pass bool value arguments then pass it inside pop method.
Navigator.of(context).pop(true);
因此,在 flutter 中,您可以传递 get pop 回调以及参数。
我必须翻页,p1 和 p2。当您启动该应用程序时,您位于 p1 并且可以打开 p2。我希望当您使用后退按钮离开 p2 时,p1 状态会刷新(就像再次调用 initState() 而无需使用 Navigator.push 或使用 WillPopScope 创建新路由)。
可能吗?
您可以调用方法在弹出第二个屏幕后刷新您的第一页状态。
p1:
refreshState() {
// change your state to refresh the screen
}
Navigator.push(context, MaterialPageRoute(builder: (context) => p2()),).then((res) => refreshPage());
p2:
Navigator.pop(context);
是的,你可以。
当你推送一个新页面时,你可以有它的弹出回调。
// If you want to get particular type of data arguments, then can add it with MaterialPageRoute<Data_Type_Value_You_Want_In_Return>
Navigator.push(context, MaterialPageRoute<bool>(builder: (context) => page2()),).then((bool res) {
// check here if you got your data or not
if(res!=null && res==true){
refreshPage();
}
});
在您的下一个屏幕中
// Let say if i want to pass bool value arguments then pass it inside pop method.
Navigator.of(context).pop(true);
因此,在 flutter 中,您可以传递 get pop 回调以及参数。