gpt4 book ai didi

flutter - 单击通知路由到特定的 flutter 屏幕

转载 作者:行者123 更新时间:2023-12-05 08:51:49 32 4
gpt4 key购买 nike

如何在点击通知时转到特定屏幕?我在 javascript 中使用了云功能,当我点击通知时,它打开了我的应用程序而不是特定的屏幕

 _fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
getOrder(message['data']['order_id']);
showDialog(
context: context,
builder: (context) => AlertDialog(
content: ListTile(
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailOrder()));
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailOrder()));
},
);

最佳答案

你可以尝试使用命名路由 + 全局导航键,它在我的情况下工作得有点不同。
首先像这样设置一个全局导航键:

...
class MyApp extends StatelessWidget {
static final navigatorKey = GlobalKey<NavigatorState>();
...

然后在下面定义命名的路由管理器:

...
Route routes(RouteSettings settings) {
if (settings.name == "/") {
return MaterialPageRoute(
builder: (_) => HomePage(),
);
} else if (settings.name.startsWith("/details/")) {
try {
String id = settings.name.split("/")[2];
return MaterialPageRoute(
builder: (_) => DetailPage(id),
);
} catch (e) {
return MaterialPageRoute(
builder: (_) => HomePage(),
);
}
} else {
return MaterialPageRoute(
builder: (_) => HomePage(),
);
}
}
...

并将 navigatorKey 和路由添加到 MaterialApp:

...
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: "/",
onGenerateRoute: routes,
title: 'MyApp',
navigatorKey: navigatorKey,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
...

完成所有这些设置后,您可以使用导航键在主页和详细信息之间导航,如下所示:

MyApp.navigatorKey.currentState.pushNamed("/"); // to navigate to HomePage
MyApp.navigatorKey.currentState.pushNamed("/details/1"); // to navigate to DetailPage

希望它能成功,祝你好运!

关于flutter - 单击通知路由到特定的 flutter 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463196/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com