The bottom Navigation Bar helps us to navigate from top-level views of an app. In the bottom navigation bar, we display a row of widgets, and by clicking on those widgets, we can navigate from one screen to another. Usually, we display 3 to 5 items in the bottom navigation bar. Let’s check how can we implement the bottom navigation bar in Flutter.
It is really easy to implement in Flutter. You can select one item at a time navigate to the required screen and add text and labels.
Read more about Flutter app development services from Mobikul.
Three important points to remember while using the bottom navigation bar:
- We can display only a small number of widgets in the bottom navigation which can be 2 to 5.
- It must have at least two bottom navigation items. Otherwise, we will get an error.
- It is required to have the icon and title properties, and we need to set relevant widgets for them.
In your scaffold add a bottom navigation bar and bottom navigation bar items.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
bottomNavigationBar: BottomNavigationBar( items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home' ), BottomNavigationBarItem( icon: Icon(Icons.call), label: 'Call' ), BottomNavigationBarItem( icon: Icon(Icons.chat), label: "Chat" ) ], |
Now show a selection of items by using two properties current index and onTap property.
1 2 3 4 5 6 7 8 |
int selectedItemIndex = 0; currentIndex: selectedItemIndex, onTap: (int index){ setState(() { selectedItemIndex = index; }); } |
Now we have to show some pages, on tap of an item.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<Widget> _widgets = [ Text("Home", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold, color: Colors.amber),), Text("Calls", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold, color: Colors.blueAccent),), Text("Chats", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold, color: Colors.black ),), ]; body: Container( child: Center( child: _widgets[selectedItemIndex], ), ), |
That’s it, now we can review and run our code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int selectedItemIndex = 0; List<Widget> _widgets = [ Text("Home", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold, color: Colors.amber),), Text("Calls", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold, color: Colors.blueAccent),), Text("Chats", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold, color: Colors.black ),), ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Bottom Navigation Bar "), ), body: Container( child: Center( child: _widgets[selectedItemIndex], ), ), bottomNavigationBar: BottomNavigationBar( items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home' ), BottomNavigationBarItem( icon: Icon(Icons.call), label: 'Call' ), BottomNavigationBarItem( icon: Icon(Icons.chat), label: "Chat" ) ], currentIndex: selectedItemIndex, onTap: (int index){ setState(() { selectedItemIndex = index; }); } ), ); } } |
OUTPUT:
So, that’s how we implement the bottom navigation bar in Flutter.
Thanks for reading this article.
If I got something wrong, let me know in the comments. I would love to improve.
Reference link: https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html
You can also read: https://mobikul.com/dark-theme-in-the-flutter/