Flutter 在颤振中接收推送通知时以编程方式更改底部导航栏的选项卡
我正在我的颤振应用程序中集成推送通知。我可以在主屏幕上接收消息,但现在我想更改底部导航栏中的选项卡,但即使尝试了几次,我还是无法完成。以下是我的导航类:Flutter 在颤振中接收推送通知时以编程方式更改底部导航栏的选项卡,flutter,dart,Flutter,Dart,我正在我的颤振应用程序中集成推送通知。我可以在主屏幕上接收消息,但现在我想更改底部导航栏中的选项卡,但即使尝试了几次,我还是无法完成。以下是我的导航类: class IFBottomNavigationBar extends StatefulWidget { List<Widget> tabWidgetItems = List<Widget>(); NavBarItem initialTabState; IFBottomNavigationBar({@
class IFBottomNavigationBar extends StatefulWidget {
List<Widget> tabWidgetItems = List<Widget>();
NavBarItem initialTabState;
IFBottomNavigationBar({@required this.tabWidgetItems, this.initialTabState});
@override
State<StatefulWidget> createState() => _IFBottomNavigationBarState();
}
class _IFBottomNavigationBarState extends State<IFBottomNavigationBar> {
int navigationIndex = 0;
BottomNavBarBloc _bottomNavBarBloc;
@override
void initState() {
super.initState();
_bottomNavBarBloc = BottomNavBarBloc();
_bottomNavBarBloc.defaultItem = widget.initialTabState;
}
@override
void dispose() {
_bottomNavBarBloc.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: StreamBuilder<NavBarItem>(
stream: _bottomNavBarBloc.itemStream,
initialData: _bottomNavBarBloc.defaultItem,
builder: (BuildContext context, AsyncSnapshot<NavBarItem> snapshot) {
switch (snapshot.data) {
case NavBarItem.PEOPLE:
return widget.tabWidgetItems[0];
case NavBarItem.FEEDBACK:
return widget.tabWidgetItems[1];
case NavBarItem.MEETINGS:
return widget.tabWidgetItems[2];
case NavBarItem.SURVEY:
return widget.tabWidgetItems[3];
break;
}
},
),
bottomNavigationBar: StreamBuilder(
stream: _bottomNavBarBloc.itemStream,
initialData: _bottomNavBarBloc.defaultItem,
builder: (BuildContext context, AsyncSnapshot<NavBarItem> snapshot) {
BottomNavigationBar bottomTab = BottomNavigationBar(
currentIndex: snapshot.data.index,
fixedColor: Colors.blueAccent,
showUnselectedLabels: true,
unselectedItemColor: Colors.black54,
type: BottomNavigationBarType.fixed,
onTap: _bottomNavBarBloc.pickItem,
items: [
BottomNavigationBarItem(
title: Text('People'),
icon: Icon(Icons.people),
),
BottomNavigationBarItem(
title: Text('Feedback'),
icon: Icon(Icons.compare_arrows),
),
BottomNavigationBarItem(
title: Text('Meetings'),
icon: Icon(Icons.event),
),
],
);
return bottomTab;
},
),
);
}
}
类IFBottomNavigationBar扩展StatefulWidget{
列表选项卡WidgetItems=List();
NavBarItem初始状态;
IFBottomNavigationBar({@required this.tabWidgetItems,this.initialTabState});
@凌驾
State createState()=>IFBottomNavigationBarState();
}
类_IFBottomNavigationBarState扩展状态{
int导航索引=0;
BottomNavBarBloc _BottomNavBarBloc;
@凌驾
void initState(){
super.initState();
_bottomNavBarBloc=bottomNavBarBloc();
_bottomNavBarBloc.defaultItem=widget.initialTabState;
}
@凌驾
无效处置(){
_bottomNavBarBloc.close();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.white,
正文:StreamBuilder(
流:_bottomNavBarBloc.itemStream,
initialData:_bottomNavBarBloc.defaultItem,
生成器:(BuildContext上下文,异步快照){
交换机(snapshot.data){
case NavBarItem.PEOPLE:
返回widget.tabWidgetItems[0];
案例NavBarItem.反馈:
return widget.tabWidgetItems[1];
案例NavBarItem.MEETINGS:
return widget.tabWidgetItems[2];
案例NavBarItem.SURVEY:
return widget.tabWidgetItems[3];
打破
}
},
),
底部导航栏:StreamBuilder(
流:_bottomNavBarBloc.itemStream,
initialData:_bottomNavBarBloc.defaultItem,
生成器:(BuildContext上下文,异步快照){
底部导航栏底部选项卡=底部导航栏(
currentIndex:snapshot.data.index,
fixedColor:Colors.blueAccent,
showUnselectedLabels:true,
unselectedItemColor:Colors.black 54,
类型:BottomNavigationBarType.fixed,
onTap:_bottomNavBarBloc.pickItem,
项目:[
底部导航气压计(
标题:文本(“人”),
图标:图标(Icons.people),
),
底部导航气压计(
标题:文本(“反馈”),
图标:图标(图标。比较箭头),
),
底部导航气压计(
标题:文本(“会议”),
图标:图标(Icons.event),
),
],
);
返回底部标签;
},
),
);
}
}
接收推送和更改选项卡的主屏幕代码(不工作):
@覆盖
void initState(){
super.initState();
_bottomNavBarBloc=bottomNavBarBloc();
bottombar=IFBottomNavigationBar(tabWidgetItems:[\u peopleArea(),\u feedbackArea(),\u meetingsArea(),],
初始状态:NavBarItem.PEOPLE);
_firebaseMessaging.configure(
onMessage:(映射消息){
打印('on message$message');
},
onResume:(映射消息){
打印('在简历上$message');
openFeedback=true;
_bottomNavBarBloc.defaultItem=NavBarItem.FEEDBACK;
bottombar=IFBottomNavigationBar(tabWidgetItems:[\u peopleArea(),\u feedbackArea(),\u meetingsArea(),],
initialTabState:NavBarItem.FEEDBACK);
},
onLaunch:(映射消息){
打印('on launch$message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(声音:true、徽章:true、警报:true));
_firebaseMessaging.getToken().then((令牌){
打印(代币);
});
}
@凌驾
无效处置(){
_bottomNavBarBloc.close();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回底杆;
}
@override
void initState() {
super.initState();
_bottomNavBarBloc = BottomNavBarBloc();
bottombar = IFBottomNavigationBar(tabWidgetItems: [_peopleArea(), _feedbackArea(),_meetingsArea(),],
initialTabState: NavBarItem.PEOPLE);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
openFeedback = true;
_bottomNavBarBloc.defaultItem = NavBarItem.FEEDBACK;
bottombar = IFBottomNavigationBar(tabWidgetItems: [_peopleArea(), _feedbackArea(),_meetingsArea(),],
initialTabState: NavBarItem.FEEDBACK);
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.getToken().then((token){
print(token);
});
}
@override
void dispose() {
_bottomNavBarBloc.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return bottombar;
}