joisberg
joisberg

Reputation: 187

How to change currentIndex in BottomNavigationBar from button in another page

I have a page called FirstScreen.dart

SafeArea(
          child: Scaffold(
            key: _scaffoldKey,
            body: PageView(
              controller: _pageController,
              children: [
                HomeScreen(controller: _pageController),
                PrenotationScreen(),
                AccountScreen(),
                NotificationScreen()
              ],
              physics: NeverScrollableScrollPhysics(),
            ),
            bottomNavigationBar: BottomNavigationBar(
              onTap: _onItemTapped,
              currentIndex: _selectedIndex,
              elevation: 0,
              selectedItemColor: kBlackColor,
              backgroundColor: kGoldColor,
              type: BottomNavigationBarType.fixed,
              iconSize: 26,
              items: [...]

enter image description here

Inside the HomeScreen, I have a button with this function:

onPressed: () {
    controller.jumpToPage(1);
},

When I click, the function is performed correctly, but does not color the tab.

Change the page but does not color the new current tab

enter image description here

Upvotes: 0

Views: 63

Answers (1)

Andrej
Andrej

Reputation: 3235

You have to add a onPageChanged callback to your PageView. Like this:

PageView(
  ...
  onPageChanged: (int index)=>setState(()=>_selectedIndex = index),
  ...
),

Upvotes: 1

Related Questions