简体   繁体   English

Flutter, 如何在 Navigator.pop 上刷新最后一页 state

[英]Flutter, How to refresh the last page state on Navigator.pop

I have to pages, p1 and p2.我必须页,p1 和 p2。 When you launch the app, you're on p1 and can open p2.当您启动应用程序时,您在 p1 上并且可以打开 p2。 I want that when you leave p2 with the back button, the p1 state refresh (like calling again initState() without creating a new route with Navigator.push or using WillPopScope).我希望当您使用后退按钮离开 p2 时,p1 state 会刷新(例如再次调用 initState() 而不使用 Navigator.push 或使用 WillPopScope 创建新路由)。

Is it possible?可能吗?

you can call a method to refresh your first page state after the second screen is popped.在弹出第二个屏幕后,您可以调用一个方法来刷新您的第一页 state。

p1: p1:

refreshState() {
  // change your state to refresh the screen
}

Navigator.push(context, MaterialPageRoute(builder: (context) => p2()),).then((res) => refreshPage());

p2: p2:

Navigator.pop(context);

Yes, you can.是的你可以。

When you push a new page you can have its pop callback.当你推送一个新页面时,你可以有它的弹出回调。

// 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();
      }
    });

In your next screen在您的下一个屏幕中

// Let say if i want to pass bool value arguments then pass it inside pop method.
Navigator.of(context).pop(true);

So, in flutter you can pass get pop callback as well as arguments.因此,在 flutter 中,您可以传递 get pop 回调以及 arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM