Change tab of BottomNavigation bar programmically on receiving a push notification in flutter

Viewed 338

I am integrating push notification in my flutter app. I am able to receive message on my home screen but now I want to change tab in my BottomNavigationBar but even after several tries I am not able to do it. Following is my BottomNavigation class:

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

Home screen code to receive push and change tab(not working):

    @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 Answers
Related