Flutter:移动到新屏幕而不提供返回上一屏幕的导航

Flutter: Move to a new screen without providing navigate back to previous screen

我正在我的 Flutter 应用中实施身份验证流程。

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

  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");
      }
    });
  }

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

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

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

离开 授权屏幕时,您也需要使用Navigator.pushReplacement。不仅仅是在重定向到登录页面时。

您需要使用

Navigator
    .of(_context)
    .pushReplacement(new MaterialPageRoute(builder: (BuildContext context) => page));

其中 _contextBuildContext 的对象 page 是您定向到的页面。

您可以使用pushAndRemoveUntil方法:

Push the given route onto the navigator that most tightly encloses the given context, and then remove all the previous routes until the predicate returns true. To remove all the routes below the pushed route, use a [RoutePredicate] that always returns false (e.g. (Route<dynamic> route) => false).

Navigator.pushAndRemoveUntil(
  context,
  MaterialPageRoute(builder: (context) => MainPage()),
  (Route<dynamic> route) => false,
);

我们可以使用相同的路由 喜欢:

routes: {

        LoginScreen.route_name: (_) => LoginScreen(),
        .....
      },  

并在您想要推送和删除后台堆栈时使用以下代码

Navigator.of(context).pushReplacementNamed(LoginScreen.route_name);

注意:在小部件 LoginScreen 中定义静态字符串

只要你想按下和移除后退箭头就使用下面的代码

onPressed: () {
          Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(builder: (context) => PathName()),
            (Route<dynamic> route) => false,
          );
},

我想你可能已经解决了这个问题。但是你可以在你导航到的Scaffold的AppBar中设置“automaticallyLeadingImplied: false”。

我已经通过从当前页面弹出并显示新页面解决了这个问题:

Navigator.pop(context);
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => newPage));

只需添加以下代码即可:

Navigator.of(context).pushNamedAndRemoveUntil('/routeName', (route) => false);

如果您正在使用 Getx 状态管理,那么您可以试试这个

Get.of(()=> NewPage());

对于任何想知道有一个新参数需要返回 false 的人来说 Navigator.of(context).pushNamedAndRemoveUntil('/', (route) => false);

Navigator.of(context, rootNavigator: true).pop();
Navigator.pushNamed(context, '/');

这是解决方案 -

Use pushAndRemoveUntil instead of pushReplacement

Also, can be use maintainState: true

设置根页面

Navigator.pushAndRemoveUntil(
  context,
  MaterialPageRoute(
      builder: (context) => pageName, maintainState: true),
      (Route<dynamic> route) => false);

将页面从一页推送到另一页

Navigator.push(
context,
MaterialPageRoute(
    builder: (context) => pageName,
    maintainState: false),)

**如果您想在出现页面时始终刷新,请使用:**

maintainState: false

它对我不起作用,因为我在 MaterialApp 上使用的是 home 属性而不是 initialRoute。

这是一个 link,其中有以下警告帮助我发现错误:https://docs.flutter.dev/cookbook/navigation/named-routes#2-define-the-routes

Warning: When using initialRoute, don’t define a home property.