【发布时间】:2019-10-12 08:39:32
【问题描述】:
我一直在查看此处的所有答案,以便在进行命名路线导航时传递参数,但它们似乎是旧答案或者它们不起作用。
根据所写的内容,它应该可以工作,但它似乎没有做任何事情,所以我不确定我的错误在哪里。
我是这样设置的:
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(点击按钮时,我正在使用 push named 导航到下一页,这是有效的,因为新页面 '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),
【问题讨论】:
-
谢谢。我不明白这如何应用于我的代码,请您详细说明。
-
您必须为
ItemPageProfileScreen类添加一个或多个参数的构造函数(与我在Hello类中使用的方法相同) -
你能说得更具体些吗?我不明白您的代码如何获得问候,如何将参数“世界”分配给问候以及如何将其传递给有状态小部件而不是无状态小部件。谢谢
-
它是有状态的还是无状态的并不重要——它的工作原理是一样的——如果你不喜欢这种方法,请使用flutter.dev/docs/cookbook/navigation/…
标签: flutter flutter-navigation