简体   繁体   English

如何从 Flutter 中的通知导航到应用程序中的特定 MaterialPageRoute

[英]How to navigate to specific MaterialPageRoute in app from a notification in Flutter

Is it possible to navigate to specific MaterialPageRoute in app from notification click?是否可以通过点击通知导航到应用程序中的特定 MaterialPageRoute? I have notifications configured in the main screen:我在主屏幕中配置了通知:

void _configureNotifications() {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  _firebaseMessaging.requestNotificationPermissions();
  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
      _goToDeeplyNestedView();
    },
    onLaunch: (Map<String, dynamic> message) {
      _goToDeeplyNestedView();
    },
    onResume: (Map<String, dynamic> message) {
      _goToDeeplyNestedView();
    },
  );
}

_goToDeeplyNestedView() {
  Navigator.push(
      context,
      MaterialPageRoute(
          builder: (_) => DeeplyNestedView()));
}

The problem is, that when i configure it like this, it woks only from Widget where i configured notifications (I guess it's because of using 'context' in Navigator.push(). Is there some way to access to MaterialPageRoute from anywhere in the app without using any context?问题是,当我这样配置它时,它只能从我配置通知的 Widget 中起作用(我猜这是因为在 Navigator.push() 中使用了“context”。有没有办法从任何地方访问 MaterialPageRoute不使用任何上下文的应用程序?

Thank you in advance for your answers.预先感谢您的回答。

There aren't very many cases where a GlobalKey is a good idea to use, but this might be one of them.使用 GlobalKey 是个好主意的情况并不多,但这可能是其中之一。

When you build your MaterialApp (which I assume you're using), you can pass in a navigatorKey parameter which specifies the key to use for the navigator.当您构建MaterialApp (我假设您正在使用)时,您可以传入一个navigatorKey参数,该参数指定用于导航器的键。 You can then use this key to access the navigator's state.然后您可以使用此键访问导航器的状态。 That would look something like this:这看起来像这样:

class _AppState extends State<App> {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      navigatorKey: navigatorKey,
      home: new Scaffold(
        endDrawer: Drawer(),
        appBar: AppBar(),
        body: new Container(),
      ),
    );
  }
}

And then to use it you access the navigatorKey.currentContext:然后要使用它,您可以访问 navigatorKey.currentContext:

_goToDeeplyNestedView() {
  navigatorKey.currentState.push(
    MaterialPageRoute(builder: (_) => DeeplyNestedView())
  );
}

Initialize navigatorState globalkey初始化navigatorState globalkey

final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");

navigatorKey.currentState.push(
    MaterialPageRoute(builder: (_) => classname())
  );

I was facing the same issue and fixed it using the below code :我遇到了同样的问题并使用以下代码修复了它:

  1. Creating 1 global key like :创建 1 个全局密钥,例如:

     final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  2. Assigning the key to Scaffold in build method :在构建方法中为 Scaffold 分配密钥:

     return Scaffold( key: _scaffoldKey, appBar: Container(), body: Container() );
  3. printing ('something') before this to make sure your app is reaching to below code:在此之前打印 ('something') 以确保您的应用程序达到以下代码:

     Navigator.push( _scaffoldKey.currentContext, MaterialPageRoute( builder: ( _) => NotificationDetailEn(notificationModel: model)));

I think it will be helpful.我认为这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM