In Bottom Navigation Bar, When navigating to specific tab with index in Gesture detector navigation route, the page is rendering first tab data, instead of indexed tab. The icon in navigation bar is correctly displayed pressed, but the data of that icon-index is not being displayed.
**FIRST SCREEN**
GestureDetector(
onTap: () {
var route = MaterialPageRoute(
builder:
(BuildContext context) =>
WelcomeScreen(2),
);
**SECOND SCREEN**
class WelcomeScreen extends StatefulWidget {
static const String idScreen = "/WelcomeScreen";
int selectedIndex = 0;
WelcomeScreen(this.selectedIndex);
@override
_WelcomeScreenState createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen>
with SingleTickerProviderStateMixin {
late TabController tabController;
void onItemClicked(int index) {
setState(() {
widget.selectedIndex = index;
tabController.index = widget.selectedIndex;
});
}
@override
void initState() {
super.initState();
tabController = TabController(length: 3, vsync: this);
}
@override
void dispose() {
super.dispose();
tabController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: TabBarView(
physics: NeverScrollableScrollPhysics(),
controller: tabController,
children: [A(), B(), C()],
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: const Color.fromARGB(255, 55, 54, 54),
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "A",
),
BottomNavigationBarItem(
icon: Icon(Icons.people),
label: "B",
),
BottomNavigationBarItem(
icon: Icon(Icons.arrow_left),
label: "C",
),
],
unselectedItemColor: Colors.grey[600],
selectedItemColor: Colors.white,
type: BottomNavigationBarType.fixed,
selectedLabelStyle: const TextStyle(fontSize: 12.0),
showUnselectedLabels: true,
currentIndex: widget.selectedIndex,
onTap: onItemClicked,
),
);
}
}
Looking forward for help!