【问题标题】:Navigation Route not removed or Pop导航路线未删除或弹出
【发布时间】:2020-07-07 22:02:49
【问题描述】:

目前我正在我的应用程序上做一个安全设置页面。现在我遇到的问题是当我向后导航或按下返回按钮时。页面仍在导航甚至弹出。

我的代码我正在使用它进入第一个流程

Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => otp_mPin())

现在在第二次或回流时我已经使用了这个

Code used for second flow

在我使用的 OTP 页面上弹出并推送

Navigator.of(context).pop();
Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => mPin())

这是我得到的流程

屏幕 1 设置页面

 onTap: (){
                Navigator.push(context, MaterialPageRoute(builder: (context) => ProtectPhone()));
              },

带有密码的屏幕 2 页面

成功密码导航

   onPressed: (){
            Navigator.pop(context);
            Navigator.push(context, MaterialPageRoute(builder: (context) => selectScreenLock()));
          },

返回按钮/willPop

Navigator.pop(context);

屏幕 3 选择

选择或单击 MPin 将重定向到 OtpMpin 页面

  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => OtpMpin())
                );

返回或将弹出命令

  Navigator.of(context).pop();
  Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ProtectPhone())
  );

屏幕 4 OTP

OTP 输入成功

 Navigator.pop(context);
                      Navigator.push(context, MaterialPageRoute(builder: (context) => setMPin()));

屏幕 5 MPIN

返回或 Willpop 命令

        Navigator.of(context).pop();
      Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => selectScreenLock())

第一流

1 > 2 > 3 > 4 > 5

二次或回流

5 > 3 > 2 > 4 > 2 > 1

【问题讨论】:

  • 在推送新页面之前为什么要使用pop?你能添加你所有的 5 个文件导航代码吗?
  • @MSARKrish 我对推送的理解是它正在将页面堆叠到我想要从堆栈中删除并替换它的页面。由于第 3-5 页是安全页面,如果不转到第 2 页和第 4 页就无法访问。等等,我将每页放上导航代码
  • 你可以使用 Navigator.of(context).pushReplacement() 函数。

标签: flutter flutter-navigation


【解决方案1】:

在第 3 页到第 4 页中使用 pushReplacement 方法,以便在从第 3 页导航到第 4 页时,将第 3 页从堆栈中移除并推送到第 4 页。因此,如果用户在第 4 页按下返回按钮,则用户将获得第二页。

Navigator.pushReplacement(context,
                MaterialPageRoute(builder: (context) {
              return NewPage();
            }));

【讨论】:

  • 谢谢我会试试这个
  • 是的,但是在第 5 页上,当我按下返回时,我重定向到第 4 页
  • Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => setMPin()));在第 4 页
  • 我现在应该删除 navigator .pop 吗?
  • 在第 3 页使用 pushReplacement,因为你说第 3 和第 5 页是重要的用户在第 2 和第 4 页进入该页面,所以在第 3 页单独使用。如果您有第 6 页,请在第 5 页使用此方法。
猜你喜欢
  • 2020-01-06
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2017-05-19
  • 2018-03-09
  • 1970-01-01
相关资源
最近更新 更多