I want to change my BottomNavigationBar's selected index from one of its items but that item is implemented in a different .dart file and is a separate StatefulWidget. My BottomNavigationBar (navbar.dart):
class NavbarRouter extends StatefulWidget {
const NavbarRouter({Key? key}) : super(key: key);
@override
_NavbarRouterState createState() => _NavbarRouterState();
}
class _NavbarRouterState extends State<NavbarRouter> {
final List<Widget> pages = [
const YesillemePage(),
const YesillenecekPaletListesiPage(),
const PaletIcerigiPage(),
const RedPage()
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: selectedIndexGlobal,
children: pages,
),
bottomNavigationBar: SizedBox(
height: MediaQuery.of(context).size.height * .11,
child: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.bar_chart), label: "Yeşilleme\n Kontrol"),
BottomNavigationBarItem(
icon: Icon(Icons.featured_play_list_outlined),
label: "Yeşillenecek\n Palet"),
BottomNavigationBarItem(
icon: Icon(
Icons.content_paste_search,
color: Colors.grey,
),
label: "Palet İçeriği"),
BottomNavigationBarItem(
icon: Icon(Icons.warning_amber_outlined), label: "Red")
],
backgroundColor: Colors.orange[300],
currentIndex: selectedIndexGlobal,
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.white,
selectedFontSize: 12.0,
unselectedFontSize: 10.0,
onTap: (index) {
setState(() {
if (index == 0) {
focusNodeY1!.requestFocus();
} else if (index == 3) {
focusNodeRed!.requestFocus();
} else if (index == 2) {
return;
}
selectedIndexGlobal = index;
});
},
),
));
}
}
And the place I want it to change (greenitem.dart, 2nd item):
DataRow(onLongPress: () {
//here i wanto to go to index 2
},
color: item.ACIL == "X"
? MaterialStateProperty.all<
Color>(Colors.red)
: MaterialStateProperty.all<
Color>(Colors.white),
cells: [
DataCell(Text(item.PLTNO!)),
DataCell(Text(
item.BOLUM!.toString())),
]))
What I tried:
onLongPress: () {
setState(){selectedIndexGlobal = 2;}
},
This does not refresh the state of the navbar so didn't work.
And I tried to give a GlobalKey to my NavbarRouter and
onLongPress: () {
navbarKey.currentState!.setState(() {selectedIndexGlobal = 2});
},
But that gave me a "duplicate global key detected in widget tree" error. What should I do?