Flutter:移动到新屏幕而不提供返回上一屏幕的导航 [英] Flutter: Move to a new screen without providing navigate back to previous screen

查看:22
本文介绍了Flutter:移动到新屏幕而不提供返回上一屏幕的导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 Flutter 应用中实现身份验证流程.

I'm implementing an authentication flow in my Flutter app.

在尝试登录后,CheckAuth (检查用户是否已登录,然后相应地打开主屏幕或注册屏幕) 使用以下代码打开:

After a sign in attempt, the CheckAuth (which checks whether a user is signed in or not and then opens home screen or sign up screen accordingly) is opened with this code:

  void _signIn() async {
    await _auth
        .signInWithEmailAndPassword(
            email: _userEmail.trim(), password: _userPassword.trim())
        .then((task) {
      // go to home screen
      if (task.getIdToken() != null) {
        setState(() {
          Navigator.pushReplacement(
              context,
              new MaterialPageRoute(
                  builder: (BuildContext context) => new CheckAuth()));
        });
      } else {
        print("Authentication failed");
      }
    });
  }

问题:我可以成功登录应用程序,但如果我在登录后点击返回按钮,它会返回登录屏幕(虽然我希望它退出应用程序).

Problem: I can successfully sign in to the app, but if I tap back button after I sign in, it goes back to the sign in screen (while I expect it to exit from the app).

问题:如何在 Flutter 中从一个屏幕移动到另一个屏幕而无需返回?

我是否需要以某种方式删除导航器历史记录?或者根本不使用导航器?我尝试了 Navigator.replace 方法,但它似乎不起作用.

Do I need to somehow delete the navigator history? Or don't use navigator at all? I tried Navigator.replace method, but it didn't seem to work.

推荐答案

离开认证界面时也需要使用Navigator.pushReplacement.不仅在重定向到登录页面时.

You need to use Navigator.pushReplacement when leaving the auth screen too. Not just when redirecting to login page.

这篇关于Flutter:移动到新屏幕而不提供返回上一屏幕的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆