简体   繁体   English

Flutter 如何导航回特定页面

[英]Flutter how to navigate back to a particular page

I have an app with multiple pages where some pages are skip depending on the user selection...我有一个包含多个页面的应用程序,其中一些页面会根据用户选择被跳过...

for example, I have the following page例如,我有以下页面

details.dart详细信息.dart

contact.dart联系.dart

address.dart地址.dart

code.dart代码.dart

summary.dart摘要.dart

I would like to go to another page from any of the above pages...However, I want first check to see if they are in the stack and then pop to it.我想从上述任何页面转到另一个页面……但是,我想先检查它们是否在堆栈中,然后再弹出。 Otherwise push that page onto the stack否则将该页面推入堆栈

How do I do this in Flutter我如何在 Flutter 中做到这一点

I think you might want to use Navigator.of(context).pushNamedAndRemoveUntil or Navigator.popUntil .我想你可能想使用Navigator.of(context).pushNamedAndRemoveUntilNavigator.popUntil

They are very similar however like the name suggests pushNamedAndRemoveUntil pushes a new route when the predicate matches, while popUntil re-uses an existing Route/Widget in the tree.它们非常相似,但正如名称所暗示的pushNamedAndRemoveUntil在谓词匹配时推送新路由,而popUntil重新使用树中现有的路由/小部件。

You can read more about them on this wonderful Medium post explaining all options in greater detail.您可以在这篇精彩的Medium 文章中阅读更多关于它们的信息,更详细地解释所有选项。

You can use routes inside materialApp, after home回家后,您可以在 materialApp 内使用路线

return MaterialApp(
    home:... ,
    routes:{
       "/routeName" : (context) => PageRoute()
    }

and then call route name from any pages Ex : app in Contact page need to move to PageRoute然后从任何页面调用路由名称例如:联系人页面中的应用程序需要移动到 PageRoute

//Contact Page
  ... 
 onTap:(){
   Navigator.pushReplacementName(context,'/routeName');
   }

只需使用多个Navigator.pop

Here is how to navigate between two routes, using these steps:以下是使用以下步骤在两条路线之间导航的方法:

-Create two routes. - 创建两条路线。

-Navigate to the second route using Navigator.push() . - 使用Navigator.push()导航到第二条路线。

-Return to the first route using Navigator.pop() . - 使用Navigator.pop()第一条路线。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

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

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