How to use Flutter Bottom Navigation Bar

I love to make my Flutter app easy to navigate for better user experience and less taps. That’s one of the reason I’ve to use Flutter bottom navigation bar widget.

By using this widget, now I can easily switch between different Flutter screens.

Let’s understand importance of this bottom navigator bar Flutter and how to add it in our app. Also, I’ll explain its parameters with code examples and proper explanation so you can properly customize this widget in your own Flutter app as well.

About Flutter Bottom Navigation Bar Widget

We now know that we can use this bottom navigator to move between screens efficiently. It’ll not only save’s user time but also make the user’s experience good.

This widget typically appears in the bottom of the Flutter app. It can take multiple items and these are connected to different screens(or you can say Flutter widgets). By tapping on any of these items, the screen associated with it will appear instantly.

Importance of Bottom Navigation (Simple theoretical Example)

For instance, you have a chat app in which there are some specific screens. These are:

  • Home
  • Chat
  • Notifications
  • User Profile
  • Settings

Wrong Approach

The non professional approach for that would be to make one main screen and put buttons on it for these above specified screens. Now user have to move to the screen then come back again to open the other screen and so on. This is not very user friendly.

Right Approach(Using Bottom Navigator)

Here, we’ll list items and attach the above screens to them. Now user can easily open the screens and the bottom nav will still be visible for the user to switch to other screen without moving back.

We’ll understand this theory practically with code example as well.

Syntax

Scaffold(
  bottomNavigationBar: BottomNavigationBar(
        items: [  //list of bottom navigation bar items  ]
            ),
    )
  1. The Flutter scaffold has a parameter named bottomNavigationBar which takes a Flutter widget.
  2. We can pass our bottom navigation bar widget to it.
  3. The Flutter bottom navigation items parameter takes a list of bottom navigation bar item class and its required.

Parameters

Parameters of this navigation widget is listed below:

  • items
  • type(explained in items parameter example 1.1)
  • currentIndex
  • onTap
  • backgroundColor
  • elevation
  • iconSize

Let’s understand them with practical Flutter code examples.

Examples to explain Flutter Bottom Navigator Bar Widget

Example 1: items parameter

BottomNavigationBar(
   items: [
      BottomNavigationBarItem(label: 'home', icon: Icon(Icons.home)),
      BottomNavigationBarItem(label: 'profile', icon: Icon(Icons.person))
      ])
flutter bottom navigation bar widget

The parameter items takes a list of only BottomNavigationBarItem class. For demonstration, we only gave it 2 items and the minimum range is 2 as well. You can give it more depending on your requirements.

Example 1.1: BottomNavigationBarItem Class parameters and the ‘type’ parameter of BottomNavigatorBar Widget

BottomNavigationBarItem(
       label: 'home',
       icon: Icon(Icons.home),
       activeIcon: Icon(Icons.home_outlined),
       tooltip: 'This is home',
       backgroundColor: Colors.red)
Image 1
flutter bottom navigation bar item active icon
Image 2
bottom navigation bar flutter item tooltip
Image 3
flutter bottom navigation bar item background color
  1. label – This parameter takes a string and its used to show a label text unde the icon of bottom navigation item.
  2. icon – This takes a Flutter widget so I passed it a simple Flutter icon widget. You can decorate this icon by its size, color etc. or try it with other widgets as well.
  3. activeIcon – This parameter also takes a Flutter widget and can be used to show a specific widget when that item is selected as seen in the 1st image. For demonstration, I gave it an outline home icon.
  4. toolTip – The tool tip paramater takes a simple string. It can be used to guide the user about the role of this item. Just long press on it and it’ll show the text specified. See it in the 2nd image.
  5. backgroundColor – It takes a color so we gave it red. When the item is selected, background color is set and the type parameter of bottom navigation bar is set to shifting like this,  type: BottomNavigationBarType.shifting, then the selected item background color will be shown in the bottom nav bar. Other items icon will be visible but not their labels. Use this to give a good color changing effect. Make sure the type is shifting if you want the color to cover the bottom bar as shown in the 3rd image.

Example 2: currentIndex parameter

 currentIndex: index
This parameter of bottom nav bar takes an integer value and by default, its 0. It actually sets which item of bottom nav bar should be selected. Its index start from 0, so 0 means 1st item, 1 means 2nd item and so on. It moves from left to right order.
int index = 0;

As you can see above, that I created an integer variable of value 0(you can give other value too) and pass it to the current index parameter. In the next example, we’ll see how it’ll select the tapped item.

Example 3: onTap parameter

 onTap: (value) { 
     setState(() {
         index = value;
              });}
bottom navigation ontap
  1. The onTap parameter of bottom navigation bar takes a function of a single parameter(its type is integer) in which the current index is stored when the item is tapped.
  2. I have assigned value of that parameter to the integer variable and pass that integer to the currentIndex parameter of bottom navigation bar.
  3. I have used the stateful widget of Flutter because I have to rebuild the UI using Flutter setState so the change in focused bottom nav bar item is seen. You can do it with state management too like riverpod, etc. It totally depends on the requirements of your Flutter project.
  4. For demonstration, I tapped on the second item and in the above image, we can see that its now focused.

Example 4: backgroundColor parameter of bottom navigation bar Flutter

backgroundColor: Colors.blue
flutter bottom navigation bar background Color
The parameter ‘backgroundColor’ is used to change the background color of bottom navigation bar. It takes a color so for demonstration, I have it a blue color. You can try other colors too or give them values like this:
  • Colors.blue.shade200
  • Color(0xff758585)
  • Colors.blue.withOpacity(0.2)
  • Colors.blue[200]
These are some examples through which you can customize the background color of bottom nav bar Flutter.

Example 5: elevation parameter

 elevation: 0
flutter bottom bar shadow
elevation: 10

bottom bar elevation

The parameter ‘elevation’ is used to give the bottom navigation bar a shadown effect. It takes a double(decimal) value but we can pass integer to it as well. By default, it has some elevation value.
In the first example, I have given the value 0 to the elevation parameter and you can see that shadow effect is gone.
In the second example, I’ve specified the value 10 to the elevation and now we can see a bit of a shadow effect on the bottom nav bar Flutter.

Example 6: iconSize parameter

 iconSize: 30
flutter bottom bar icon size
If you want to change the size of bottom navigation bar items, then use the iconSize parameter. If the item is an icon, then the size can be changed using iconSize. It takes a decimal/double value but you can pass integer without any issue.
Its default value is 24.0, but I’ve passed 30 to it and you can see that the size of bottom navigation icons has been increased.

Example 7: Chat App screens navigation using Flutter Bottom Navigation

As I’ve promised that I’ll give a code example on different screens of chat app whose navigation will be controlled by navigation bottom bar Flutter. See below:

Step 1

int index = 0;

  List chatBottomNavIcons = [
    {'title': 'Home', 'icon': Icons.home, 'bColor': Colors.red},
    {'title': 'Chat', 'icon': Icons.chat, 'bColor': Colors.blue},
    {'title': 'Profile', 'icon': Icons.person, 'bColor': Colors.grey},
    {'title': 'Settings', 'icon': Icons.settings, 'bColor': Colors.purple},
  ];
Here, I created a variable of type integer and a list of maps in which the label, icon, and color of bottom nav bar will be specified for each item.

Step 2

bottomNavigationBar: BottomNavigationBar(
          currentIndex: index,

          type: BottomNavigationBarType.shifting,

          onTap: (value) {
            setState(() {
              index = value;
            });
          },

          items: [
            for (int i = 0; i < chatBottomNavIcons.length; i++)
              BottomNavigationBarItem(
                label: chatBottomNavIcons[i]['title'],
                backgroundColor: chatBottomNavIcons[i]['bColor'],
                icon: Icon(
                  chatBottomNavIcons[i]['icon'],
                ),
              ),
          ])
Bottom navigation bar with a list of items specified in step 1. I used ‘for loop’ to iterate over the list of maps. Here the label is assigned the ‘title’. The backgroundColor of bottom nav bar will be changed for each selected item. Finally, I set the icons as well.

Step 3

class NewScreen extends StatelessWidget {

  final String name;
  const NewScreen({super.key, required this.name});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        name,
        style: TextStyle(fontSize: 21, fontWeight: FontWeight.w500),
      ),
    );
  }
}
A simple stateless widget that will be passed to the body parameter of the scaffold in which the bottom navigation bar is specified.
This widget will take a string through its constructor. Then it’ll show that data in the text widget which is passed to its body.
I wrapped the text widget with center class so it can be seen in the center of screen.

Step 4

Scaffold(
      body: NewScreen(
        name: chatBottomNavIcons[index]['title'],
      ), 
// bottom navigation bar code specified here
)
This is the scaffold in which bottom nav bar is present. I passed the widget(name is NewScreen) and also pass the title of each selected item to it as parameter.

Step 5 (Testing)

bottom bar flutter

bottom navigation flutter bar color

We can see that the color of bottom nav bar and the screen content is changing which means the bottom navigation is working perfect.

This was a simple example, I want you to try it yourself so you can understand it better.

Conclusion

We’ve learned how Flutter bottom navigation bar widget can be implemented in a Flutter app and how to properly customize its parameters with easy and detailed code examples. Do share if you still face any issues related to the bottom navigation bar Flutter. I’ll be very pleased to help you.

Thank you and don’t forget to visit my other informative posts on other Flutter widgets as well.

Leave a Comment

Your email address will not be published. Required fields are marked *