Flutter 使用命名路由导航直接传递参数

标签 flutter flutter-navigation

我一直在查看这里的所有答案,以便在进行命名路线导航时传递参数,但它们似乎是旧答案或者不起作用。

从所写的内容来看,它应该可以工作,但它似乎没有做任何事情,所以我不确定我的错误在哪里。

这就是我的设置方式:

Main.dart(使用我的命名路由设置):

  void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primaryColor: Colors.white,
        ),
        initialRoute: HomePageScreen.id,
        routes: {
          HomePageScreen.id: (context) => HomePageScreen(),
          AddItemScreen.id: (context) => AddItemScreen(),
          AdvertiseScreen.id: (context) => AdvertiseScreen(),
          HomePageFilterScreen.id: (context) => HomePageFilterScreen(),
          HomePageResultsScreen.id: (context) => HomePageResultsScreen(),
          ItemPageProfileScreen.id: (context) => ItemPageProfileScreen(),
          ItemPageProfileSuggestUpdateScreen.id: (context) => ItemPageProfileSuggestUpdateScreen(),
          ItemPageWhereToBuyAddStoreToDatabaseScreen.id: (context) => ItemPageWhereToBuyAddStoreToDatabaseScreen(),
          ItemPageWhereToBuyMapScreen.id: (context) => ItemPageWhereToBuyMapScreen(),
          ItemPageWhereToBuyScreen.id: (context) => ItemPageWhereToBuyScreen(),
          MenuScreen.id: (context) => MenuScreen(),
          NotAvailableScreen.id: (context) => NotAvailableScreen(),
          TermsScreen.id: (context) => TermsScreen(),
          }
   );
  }
}

HomePageResultsScreen.dart(单击按钮时,我使用名为的推送导航到下一页,这是有效的,因为新页面“ItemPageProfileScreen”正在打开):

onTap: () {
           Navigator.pushNamed(context, ItemPageProfileScreen.id, arguments: 'MyTestString');
          }

ItemPageProfileScreen.dart(我尝试使用 MaterialApp onGenerateRoute 获取参数并打印到屏幕进行测试,但它不起作用):

  class ItemPageProfileScreen extends StatefulWidget {

  static const String id = 'item_page_profile_screen';

  @override
  _ItemPageProfileScreenState createState() => _ItemPageProfileScreenState();
}

class _ItemPageProfileScreenState extends State<ItemPageProfileScreen> {
  @override
  Widget build(BuildContext context) {

    MaterialApp(
      onGenerateRoute: (routeSettings){
        final arguments = routeSettings.arguments;
        print(arguments.toString());
      },
    );

    return Scaffold(),

感谢您的帮助。

编辑第二次尝试:

class ItemPageProfileScreen extends StatefulWidget {

  final String argument;

  ItemPageProfileScreen(this.argument);

  static const String id = 'item_page_profile_screen';

  @override
  _ItemPageProfileScreenState createState() => _ItemPageProfileScreenState();
}

class _ItemPageProfileScreenState extends State<ItemPageProfileScreen> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Text(widget.argument),

最佳答案

有一篇关于如何使用命名路由传递参数的官方文章。 https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments

主要思想非常简单:将参数传递到屏幕小部件的构造函数中

在官方文档(在上面的链接中)中,他们实际上使用了命名路由和常规路由这两种方法,尽管文章中提到了命名路由。

无论如何。重点关注构造函数和参数。

如果您在导航时仅传递路线名称,则可以在哪里访问具有命名路由的屏幕构造函数?在 onGenerateRoute 方法中。让我们开始吧。

  1. 覆盖顶部屏幕MyApp中的onGenerateRoute方法(这就是你的错误所在)。如果你这样做,你就不需要 routes: {} 那里(你的第二个错误)

    class MyApp extends StatelessWidget {
       @override
       Widget build(BuildContext context) {
          return MaterialApp(
              title: 'Flutter Demo',
              theme: ThemeData(
                 primaryColor: Colors.white,
              ),
              initialRoute: HomePageScreen.id,
              onGenerateRoute: (settings) {
                 if(settings.name == ItemPageProfileScreen.id) {
                    String msg = settings.arguments;
                    return MaterialPageRoute(builder: (_) => ItemPageProfileScreen(msg));
                 } else if(...
     },
    
  2. 从小部件构造函数中获取参数:

    class ItemPageProfileScreen extends StatefulWidget {
        final String argument;
    
        ItemPageProfileScreen(this.argument);
    
        static const String id = 'item_page_profile_screen';
    
        @override
        _ItemPageProfileScreenState createState() => _ItemPageProfileScreenState();
    }
    
    class _ItemPageProfileScreenState extends State<ItemPageProfileScreen> {
        @override
        Widget build(BuildContext context) {
           String msg = widget.argument;
           ...
    
  3. 点击发送参数:

     onTap: () {Navigator.pushNamed(context, ItemPageProfileScreen.id, arguments: 'MyTestString');}
    

希望这有帮助。

关于Flutter 使用命名路由导航直接传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314738/

相关文章:

flutter - 在 Flutter 应用程序中通过向后滑动手势关闭键盘

flutter - 我怎样才能有一个像这样的动画SliverAppBar?

flutter - 我可以禁用 Flutter Web 的特定依赖项吗?

flutter - Flutter 中的命名路由如何消除重复?

flutter - 如何在 Flutter 中进行嵌套导航

flutter - 当我们依赖应用程序状态(例如登录)时,在 Flutter 中导航的最佳方法是什么?

使用 onAuthStateChanged 使用 Flutter 登录 Firebase

flutter - Provider/ChangeNotifier:在构建小部件后设置状态

asynchronous - Flutter 异步代码中错误的执行顺序

flutter - 为什么 Flutter 在尝试退出应用程序时会在控制台中抛出 NoSuchMethodError,即使它在模拟器中运行?