![](/img/trans.png)
[英]Flutter: Using navigation functionalities for named routes with MaterialPageRoute without named routes
[英]Flutter pass argument directly with named routes 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 (單擊按鈕時,我正在使用名為 push 的按鈕導航到下一頁,這是有效的,因為新頁面“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');}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.