我一直在查看这里的所有答案,以便在进行命名路线导航时传递参数,但它们似乎是旧答案或者不起作用。
从所写的内容来看,它应该可以工作,但它似乎没有做任何事情,所以我不确定我的错误在哪里。
这就是我的设置方式:
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
方法中。让我们开始吧。
覆盖顶部屏幕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(... },
从小部件构造函数中获取参数:
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; ...
点击发送参数:
onTap: () {Navigator.pushNamed(context, ItemPageProfileScreen.id, arguments: 'MyTestString');}
希望这有帮助。
关于Flutter 使用命名路由导航直接传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314738/