I have demo code for bottom bar below but when I tap on second tab, related screen is not changing.
It is very basic example and should change tabs, may be it is silly mistake as I am just beginner in flutter.
I am using GetX for routing via GetMatrialApp.
so IntiialRoute is tab screen.
class TabScreen extends StatefulWidget {
const TabScreen({super.key});
@override
State<TabScreen> createState() => _TabScreenState();
}
class _TabScreenState extends State<TabScreen> {
@override
Widget build(BuildContext context) {
int currentIndex = 0;
final List<Widget> screens = [const Text('A'), const Text('B')];
return Scaffold(
body: screens[currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
onTap: (index) {
setState(() {
currentIndex = index;
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}
It should show second tab screen.
nick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
int currentIndex = 0;
should defined in _TabScreenState
instead of build
method.
class _TabScreenState extends State<TabScreen> {
int currentIndex = 0;
@override
Widget build(BuildContext context) {
final List<Widget> screens = [const Text('A'), const Text('B')];
return Scaffold(
body: screens[currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
onTap: (index) {
setState(() {
currentIndex = index;
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}