Introduction Mobile apps often use bottom navigation. You can reach all entries with one hand and it’s the same for Android and iOS. For Flutter apps, there is the widget BottomNavigationBar to accomplish this. In this article, I’ll share how to use BottomNavigationBar in Flutter apps.
We start with this standard implementation:
class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', debugShowCheckedModeBanner: false, theme: ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: Colors.deepPurple, ), ), home: const MainPage(), ); } } class MainPage extends StatefulWidget { const MainPage({super.key}); @override State<MainPage> createState() => _MainPageState(); } class _MainPageState extends State<MainPage> { final List<String> pages = ["Home", "Search", "Settings"]; String _selectedPage = "Home"; @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: BottomNavigationBar( currentIndex: pages.indexOf(_selectedPage), onTap: (index) => setState(() { _selectedPage = pages[index]; }), items: [ BottomNavigationBarItem(icon: Icon(Icons.home), label: pages[0]), BottomNavigationBarItem(icon: Icon(Icons.search), label: pages[1]), BottomNavigationBarItem(icon: Icon(Icons.settings), label: pages[2]), ], ), body: Center(child: Text(_selectedPage)), ); } } There are 3 item that set the page to the corresponding entry when they are clicked. Every item consists of an icon and a label.
...