可能以前曾有人问过,但我找不到它,但是如何将参数传递给命名路由?

这就是我建立路线的方式

Widget build(BuildContext context) {
    return new Navigator(
      initialRoute: 'home/chooseroom',
      onGenerateRoute: (RouteSettings settings) {
        WidgetBuilder builder;
        switch (settings.name) {
          case 'home/chooseroom':
            // navigates to 'signup/choose_credentials'.
            builder = (BuildContext _) => new ChoosePage();
            break;
          case 'home/createpage':
            builder = (BuildContext _) => new CreateRoomPage();
            break;
          case 'home/presentation':
            builder = (BuildContext _) => new Presentation();
            break;
          default:
            throw new Exception('Invalid route: ${settings.name}');
        }
        return new MaterialPageRoute(builder: builder, settings: settings);
      },
    );

这就是你的称呼Navigator.of(context).pushNamed('home/presentation')
但是,如果我的小部件是new Presentation(arg1, arg2, arg3)怎么办?

最佳答案

pushNamed()现在支持从merged pull request开始的参数。如果您迫不及待,请切换到 channel master(flutter channel master,然后可能是flutter upgrade)。

如何发送:

    Navigator.pushNamed(ctx, '/foo', arguments: someObject);

如何接收:
...
    return MaterialApp(
        ...
        onGenerateRoute: _getRoute,
        ...
    );
...

Route<dynamic> _getRoute(RouteSettings settings) {
    if (settings.name == '/foo') {
        // FooRoute constructor expects SomeObject
        return _buildRoute(settings, new FooRoute(settings.arguments));
    }

    return null;
}

MaterialPageRoute _buildRoute(RouteSettings settings, Widget builder) {
    return new MaterialPageRoute(
        settings: settings,
        builder: (ctx) => builder,
    );
}

“参数”可以是任何对象,例如一张 map 。

关于flutter - 导航器通过pushNamed传递参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53304340/

10-12 00:27
查看更多