Last active
November 9, 2022 12:01
-
-
Save NHuyHoang/39f1f1a03d6b504569e8318de1b55d9f to your computer and use it in GitHub Desktop.
Programmatically select BottomNavigationBar Tab in Flutter instead of built in onTap callback
It would be possible by using for example Provider package with the ChangeNotifierProvider
Is there any way to change tab from another page
You can follow these steps to achieve the required purpose:
- Create a file MainProvider.dart with the following code -
import 'package:flutter/material.dart';
class MainProvider with ChangeNotifier {
int currentPageIndex = 0;
switchToPage(int index) {
currentPageIndex = index;
notifyListeners();
}
}
- Where you want to change the tab use the code -
final mainProv = Provider.of<MainProvider>(context, listen: false);
CustomButtonWithoutIcon(
text: 'Explore Products',
onPressed: () {
setState(() {
mainProv.switchToPage(0);
});
},
bgColor: blueColor,
borderColor: blueColor,
textIconColor: whiteColor,
),
Hi @vasudevsoni, I'm new at flutter, I'm new at flutter and I'm trying to change the page through the same page but from a different file. I tried to pass the global key in the widget argument but it didn't work. Can you please help me with this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@atul-chaudhary, were you able to find a way to do it?