在Flutter中收到推送通知时,以编程方式更改BottomNavigation栏的选项卡

时间:2019-10-13 15:27:07

标签: flutter dart

我将推送通知集成到我的Flutter应用中。我可以在主屏幕上接收消息,但是现在我想更改BottomNavigationBar中的选项卡,但是即使经过几次尝试,我也无法做到。以下是我的BottomNavigation类:

    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;
        },
      ),
    );
  }
}

用于接收推送和更改标签的主屏幕代码(不起作用):

    @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;
  }

0 个答案:

没有答案