【问题标题】:Flutter FCM Navigation onResume/onLaunchFlutter FCM 导航 onResume/onLaunch
【发布时间】:2020-04-22 04:38:53
【问题描述】:

在我的应用程序中,我有一个类“家”,用户在获得授权后会被导航到该类。在“home”中有一个带有 BottomNavigationBar 的 PageView。当用户收到来自 firebase 的推送通知(新消息)时,他应该导航到 ChatHome 并在点击后导航到特定聊天。我该怎么做?

首页确实是这样的

@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: PageView(
        children: <Widget>[
          Feed(userID: widget.userID),
          SearchView(),
          ChatHome(),
          Profile(
              uid: currentUser?.uid,
              auth: widget.auth,
              logoutCallback: widget.logoutCallback,
          ),
        ],
        controller: pageController,
        onPageChanged: onPageChanged,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: CupertinoTabBar(
          currentIndex: pageIndex,
          inactiveColor: Colors.white,
          backgroundColor: Color.fromRGBO(0, 111, 123, 1),
          activeColor: Color.fromRGBO(251, 174, 23, 1),
          onTap: onTap,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home, size: 20),
              title: Text("Home"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.search, size: 20),
              title: Text("Search"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.chat, size: 20),
              title: Text("Chats"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.profile, size: 20),
              title: Text("Profile"),
            ),
          ]),
    );
  }

在 ChatHome 中,我与所有聊天伙伴一起获得了 ListTiles。单击后,用户将导航到特定聊天。

Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => Chat(
                    chatPartnerId: chatPartner[index].uid,
                    chatPartnerName: chatPartner[index].username,
                    chatPartnerPhotoUrl: chatPartner[index].photoUrl)));

在首页,我得到了我的onResume

我尝试了不同的方法,但可以找到一个好的解决方案

使用以下代码,我能够导航到 ChatHome,但 BottomNavigationBar 消失了。

onResume: (Map<String, dynamic> message) async {
          final screen = message['screen'];
          print(screen);
          if(screen == "ChatHome"){
          Navigator.pushNamed(context, '/chatHome');
        } 
      },

以下代码无法正常运行,因为应用程序跳转并始终以 home 结束。

onResume: (Map<String, dynamic> message) async {
          final screen = message['screen'];
          print(screen);
          if(screen == "ChatHome"){
          Navigator.pushNamed(context, '/home').then((_) => pageController.jumpToPage(2));
        } 
      },
routes: {
          '/rootPage': (BuildContext context) => new RootPage(auth: new Auth()),
          '/chatHome': (BuildContext context) => ChatHome(),
          '/home': (BuildContext context) => Home(),
        },


感谢任何帮助。

【问题讨论】:

标签: flutter dart navigation firebase-cloud-messaging


【解决方案1】:

您需要为您的 MaterialApp Widget 提供一个 navigatorKey:

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
            ....
            navigatorKey: navigatorKey,
            .....
        );
    }
}

然后,您可以使用此 navigatorKey 在您的 onResume 处理程序中导航:

onResume: (message) async {
    navigatorKey.currentState.pushNamed("/sample-route/" + message["data"]["..."])
}

对于 onLaunch 处理程序(直接调用 onLaunch,表示第一个应用程序视图尚未构建,这意味着您无法立即使用 navigatorKey,我只找到了一个 hacky 解决方案:

onLaunch: (message) async {
    Timer.periodic(
        Duration(milliseconds: 500),
        (timer) {
            if (navigatorKey.currentState == null) return;
            navigatorKey.currentState.pushNamed("/sample-route/" + message["data"]["..."]);
            timer.cancel();
        },
    );
}

当然,onLaunch 的解决方案并不干净,但它确实有效。

【讨论】:

  • 未定义名称'navigatorKey'。这就是我得到的。
  • 您需要确保变量 navigatorKey 是全局声明的(在类之外),并且您必须导入声明变量的文件。
猜你喜欢
  • 2020-03-25
  • 2020-06-03
  • 2021-01-06
  • 2021-06-04
  • 2019-09-25
  • 2020-11-14
  • 2018-11-30
  • 2020-05-05
  • 2021-04-18
相关资源
最近更新 更多