可能已经有人问过,但我找不到,但是如何将参数传递给命名路由?
这就是我建立路线的方式
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()
现在支持从合并的合并请求开始的参数。如果您迫不及待,请切换至频道master
(flutter channel master
可能紧随其后flutter upgrade
)。
如何发送:
Navigator.pushNamed(ctx, '/foo', arguments: someObject);
如何领取:
... return MaterialApp( ... onGenerateRoute: _getRoute, ... ); ... Route_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, ); }
“参数”可以是任何对象,例如地图。