skip to Main Content

I have a bottom navigation bar like this:
enter image description here

When i click (1) in tab Home, it navigate to Detail page of item in tab My Learning (2) but not tab My Learning (using Navigator.of(context).push()). But not change tab to My Learning in bottom navigation bar, it still tab Home. So how i fix that. Thank you.

Some code:
Class main_view_model (contain bottom navigation bar):

  BottomTabItemAfterSignIn _currentBottomTab = BottomTabItemAfterSignIn.home;

  BottomTabItemAfterSignIn get currentBottomTab => _currentBottomTab;

  void changeTab(BottomTabItemAfterSignIn tab, {bool isBackClick = false}) {
    if (_currentBottomTab != tab) {
      _currentBottomTab = tab;
    }
  }

Class main_view:

  @override
  Widget buildView(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        _onPressBackDevice();

        return false;
      },
      child: Selector<MainUserViewModel, BottomTabItemAfterSignIn>(
        selector: (_, viewModel) => viewModel.currentBottomTab,
        builder: (_, currentBottomTab, __) {
          return Scaffold(
            backgroundColor: AppColor.neutrals.shade900,
            body: _buildBody(),
            bottomNavigationBar: BottomNavigationUserWidget(
              currentTab: currentBottomTab,
              onSelectTab: _selectTab,
            ),
          );
        },
      ),
    );
  }

  Widget _buildBody() {
    if (viewModel.currentBottomTab == BottomTabItemAfterSignIn.home) {
      return _buildTabItem(
        BottomTabItemAfterSignIn.home,
        _cacheBottomTabWidgets[BottomTabItemAfterSignIn.home],
      );
    } else if (viewModel.currentBottomTab == BottomTabItemAfterSignIn.course) {
      return _buildTabItem(
        BottomTabItemAfterSignIn.course,
        _cacheBottomTabWidgets[BottomTabItemAfterSignIn.course],
      );
    } else if (viewModel.currentBottomTab ==
        BottomTabItemAfterSignIn.myLearning) {
      return _buildTabItem(
        BottomTabItemAfterSignIn.myLearning,
        _cacheBottomTabWidgets[BottomTabItemAfterSignIn.myLearning],
      );
    } else {
      return _buildTabItem(
        BottomTabItemAfterSignIn.setting,
        _cacheBottomTabWidgets[BottomTabItemBeforeSignIn.setting],
      );
    }
  }

  Widget _buildTabItem(BottomTabItemAfterSignIn tabItem, Widget? child) {
    // Cache Widget
    return Offstage(
      offstage: viewModel.currentBottomTab != tabItem,
      child: child ??
          (viewModel.currentBottomTab == tabItem
              ? _buildCacheTab(tabItem)
              : Container()),
    );
  }

  Widget _buildCacheTab(BottomTabItemAfterSignIn tabItem) {
    return _cacheBottomTabWidgets[tabItem] =
        BottomBodyNavigationUserWidget(
      bottomMenuBar: tabItem,
      navigatorKey: _bottomTabKeys[tabItem]!,
    );
  }

  void _selectTab(BottomTabItemAfterSignIn bottomMenuBar) {
    viewModel.changeTab(bottomMenuBar);
  }

2

Answers


  1. I hope you are using stateful widget if so then you have to use setState((){}) to to effect any value change so your code for _currentBottomTab = tab; should like this:

        setState((){
            _currentBottomTab = tab;
        });
    

    For more info on stateful widget please check here.

    Login or Signup to reply.
  2. use the same fun on 1-tab nd pass the index number where you want to go

    changeTab(2),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search