-
-
Save NHuyHoang/39f1f1a03d6b504569e8318de1b55d9f to your computer and use it in GitHub Desktop.
I'd love to find a way to change the tab from outside the state/class(on another page) as well. I got it working with one indexed tab in the Bottom Navigation Bar, but.. it only works on one, the other ones come back as null, so It won't change it. I'm still a beginner at Flutter, so it seems like it's related to how states work, but I'm not sure how to do a work around on this one yet.
Is there any way to change tab from another page
@atul-chaudhary, were you able to find a way to do it?
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?
Is there any way to change tab from another page