Hi all I am new to flutter. I was trying to use bottom navigation bar but it includes indexing navigation over which I got stuck for a long time now. I don’t know how to do it so please if anyone know’s how to do it let me know.
Here’s my code
class DashboardScreen extends StatefulWidget {
const DashboardScreen({Key? key}) : super(key: key);
@override
State<DashboardScreen> createState() => _DashboardScreenState();
}
class _DashboardScreenState extends State<DashboardScreen> {
@override
Widget build(BuildContext context) {
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
return Scaffold(
extendBody: true,
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.white,
selectedIconTheme: const IconThemeData(color: Colors.white),
elevation: 100,
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.transparent,
currentIndex: _selectedIndex,
onTap: _onItemTapped,
items: const [
BottomNavigationBarItem(
icon: Icon(
Icons.people,
),
label: "Modes"),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
),
label: "Profile"),
BottomNavigationBarItem(icon: Icon(Icons.chat), label: "Chats"),
BottomNavigationBarItem(
icon: Icon(Icons.settings), label: "Settings"),
],
),
body: _selectedIndex == 0
? const Das()
: _selectedIndex == 1
? const Das()
: _selectedIndex == 2
? const SaySomethingAboutYou()
: _selectedIndex == 3
? const SaySomethingAboutYou()
: Container(),
);
}
}
>Solution :
Move the following code out of the build method
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
Keep it above the @override statement.