【问题标题】:Flutter - How to get notified when the page I pushed was popped by back button?Flutter - 当我推送的页面被后退按钮弹出时如何得到通知?
【发布时间】:2019-11-14 06:28:59
【问题描述】:

假设这种情况:page1 Navigator.push 到 page2。 page2 上的用户单击后退按钮,因此 page2 弹出并且 page1 重新获得视图。如何在 page1 上捕捉到此事件?

【问题讨论】:

    标签: flutter flutter-navigation


    【解决方案1】:

    所以当 Navigator.push() 发生时,它会返回一个 Future。在 Navigator.pop() 上,未来已经解决。您可以像这样从 Navigator.pop 返回值 Navigator.pop(context, "MyResult");该结果将像这样被捕获 - 最终结果 = Navigator.push()。您可以在调用 Navigator.push() 的小部件中使用结果。

    【讨论】:

      【解决方案2】:

      您可以通过从 Navigator.pop 传递参数来检查...

      从第二个屏幕:-

       Navigator.pop(context, 'updateList'),
      

      并在第一个屏幕上像这样检查并通过您想要的条件:-

      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),
                      )
      

      希望它对你有用:-)

      【讨论】:

      【解决方案3】:

      在弹出屏幕中使用WillPopScope 小部件,然后在弹出屏幕时使用onWillPop 函数获得通知。

      像这样:

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

      【讨论】:

        【解决方案4】:

        您可以使用提供程序包并在推送时发送通知并通知侦听器并更新您想要的内容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多