【发布时间】: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(),
},
感谢任何帮助。
【问题讨论】:
-
你可以试试this article
标签: flutter dart navigation firebase-cloud-messaging