Skip to main content

Bottom Navigation Bar in Flutter. How to make Bottom Navigation Bar in Flutter?

Bottom Navigation Bar in Flutter. 

Introduction

Bottom navigation bar are one of the most important part of modern apps and so in Flutter right out of the box we have got a widget that is named similarly as BottomNavigationBar.
Bottom Navigation Bar
Bottom Navigation Bar


Table of Contents

  • Setup project
  • Logic code
  • Design
  • Result
  • Other parameters

Setup project

In order to make a bottom navigation bar we need no dependency to be install.
So start by just creating a new project or use the existing project you are working on. To create a new project use Android Studio, Visual Studio Code or just by Command Prompt / Terminal.
flutter create bottom_nav_bar

Logic Code

In this app we will make an app with three options on bottom navigation bar - 
 Home, Account, Dashboard.
When clicked on any option, the body of Scaffold will contain a Text widget showing one of the three options.
Now we have to use the field already present in Scaffold of our app. Flutter by default creates a demo project, so clean the MyHomePage stateful widget just for simplicity.
Instead of that create a new stateful widget and name it HomePage.
Let's come to build method.
In the build method we will return a Scaffold widget.
For our app we will use three fields of Scaffold, that are 
  1. appBar
  2. body
  3. bottomNavigationBar
Now we are already familiar with appBar and body so we we will focus on bottomNavigationBar. But before that initialize two variables as follows. We will require it.
int _selectedIndex = 0;
List<String> options = ["Home", "Account", "Dashboard"];

In the Scaffold enter the field bottomNavigationBar.
Scaffold(      
  bottomNavigationBar: ,
  appBar: AppBar(),
  body: Container(),  
);

In the bottomNavigationBar we can provide many parameters but the one we are going to use in our app is BottomNavigationBar(Note: here b is capital).
BottomNavigationBar requires a field and that is items:[].
items is a list of widget. But here the only widget that you can use is BottomNavigationBarItem. In our app we will require 3 BottomNavigationBarItem.
So our code should look like
bottomNavigationBar: BottomNavigationBar(
  items: [
    BottomNavigationBarItem(icon: icon),
    BottomNavigationBarItem(icon: icon),
    BottomNavigationBarItem(icon: icon),
  ],
),

The icon here is required field. We have large variety of icons in Flutter. According to our app we have Home,  Account. Dashboard. So we do not require to get icons from other place.
Also there is another required field for label which takes String parameter. So provide the respective labels here.
Now we have complete code for design.
bottomNavigationBar: BottomNavigationBar(
  items: [
    BottomNavigationBarItem(icon: Icon(Icons.home),label: "Home",),
    BottomNavigationBarItem(icon: Icon(Icons.account_circle),label: "Account",),
    BottomNavigationBarItem(icon: Icon(Icons.dashboard),label: "Dashboard",),
  ],
),

But wait our app won't work because the bottomNavigationBar has no functionality. To do that there are some fields, that are
currentIndex: ,
selectedItemColor: ,
onTap: ,
 
Let's discuss one by one.

  • The currentIndex field specifies which item is being selected and for that we have made _selectedIndex field. The first item has index equals zero and so on.
  • selectedItemColor as the name states the color of item which when selected.
  • onTap is function which has an argument of index. This is the index of tapped item. We will set the _selectedIndex as index here.

So this part of code should look like,
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber,
onTap: (int index) {
  setState(() {
    _selectedIndex = index;
  });
},

Congrats we completed our logic code for our app and now run it.
Simple Bottom Navigation Bar


Design

In the body section we are going to use a Text widget wrapped by Center widget and also apply some text styles. So copy the following code.
Center(
  child: Text(
    options[_selectedIndex],
    style: TextStyle(
      fontSize: 48,
      fontWeight: FontWeight.w700,
    ),
  ),
),

Result

This is our final app.
Complete Bottom Navigation Bar

Other paramters

But some fields are not discussed yet. Let's discuss now
  1. elevation : This field places the bottom navigation bar in z index. It means it is the amount of shadow. It accepts double value and its default value is equal to 8.0. You can compare from following two examples.
    Elevation zero Bottom Navigation Bar
    elevation : 0


    Elevation 20 Bottom Navigation Bar
    elevation : 20

  2. type: This asks for type of bottom navigation bar and accepts value BottomNavigationBarType. There are two types:-
    Fixed type of Bottom Navigation Bar
    type: BottomNavigationBarType.shifting,
    

    Shifting type of Bottom Navigation Bar
    type: BottomNavigationBarType.fixed,
    

  3. fixedColor: This field asks for which Color you want to assign for selectected item. According to documentation of Flutter of this class selectedItemColor is preferred. 
  4. backgroundColor: In this field we set the color of BottomNavigationBar.
    Background color of Bottom Navigation Bar
    backgroundColor: Colors.green,
    

  5. iconSize: This field sets the size of icon. The input is of double type. It's default value is 24.0.
  6. unSelectedItemColor: Contrary to selectedItemColor, sets color of unselected item.
    Unselected color of Bottom Navigation Bar
    unselectedItemColor: Colors.deepOrangeAccent,
    

  7. selectedIconTheme: Sets the icon theme for selected item. It takes IconThemeData type value.
    Selected theme data of Bottom Navigation Bar
    selectedIconTheme: IconThemeData(
    color: Colors.blue, 
    size: 32.0,
    opacity: 0.5,),
    

  8. unselectedIconTheme: Sets the icon theme for unselected item. It takes IconThemeData type value.
    Unselected theme data of Bottom Navigation Bar
    selectedIconTheme: IconThemeData(color: Colors.blue, size: 32.0, opacity: 1),
    unselectedIconTheme: IconThemeData(color: Colors.orange, size: 12.0, opacity: 0.4),
    

  9. selectedFontSize: Sets the font size of label for selected item.
  10. unselectedFontSize: Sets the font size of label for unselected item
  11. selectedLabelStyle: Sets the label style for selected item. It takes input of TextStyle type. 
  12. unselectedLabelStyle: Sets the label style for unselected item. It takes input of TextStyle type. 
  13. showSelectedLabels: Sets if you want to show/hide the selected item label. It takes value of boolean type.
  14. showUnselectedLabels: Sets if you want to show/hide the unselected item label. It takes value of boolean type.
  15. enableFeedback: It takes boolean type of input. It is for whether you want a haptic feedback on press of item. For e.g. on Android device on tap will produce click sound and long press will make short vibration.
So we have learned how to make a Bottom Navigation Bar in Flutter. See you in next blog.
Thank you.


Comments

Popular posts from this blog

Flutter | Material Banner Tutorial

In this tutorial, we will create and display Material Banner in Flutter . Material Banners are displayed at the top of the screen . User interaction is required to dismiss the banner . Material Banner Material Banner alerts the user about action and p rovides some actions for the user to take . In brief, it alerts the user about an issue and the user address the issue . The user should dismiss the banner to remove it from the screen else it will be active. Syntax  ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner( content: content, actions: actions)) Code Here is our starting class. It is a stateless class and we will display a banner when the Button is pressed. class SnackBarTutorial extends StatelessWidget { const SnackBarTutorial({Key ? key}) : super ( key: key); @ override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( "Material Banner"

How to create Snackbars in Flutter | Snackbar Tutorial

In this article, we will create and display different types of SnackBars in Flutter.  SnackBars in Flutter SnackBars are used to briefly display some information or inform the user about an action. For instance, when a user deletes a mail or message you may want to inform the user about the action. Also, the user can undo the action he performed by the undo button in the Snackbar . Syntax For creating a SnackBar, we use the following code. ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: content)); Inside the content field of SnackBar, we will pass the content. Any content can be passed inside it, but in practice, small messages with or without a button. Example Simple SnackBar class SnackBarTutorial extends StatelessWidget {   const SnackBarTutorial({Key? key}) : super (key: key);   @override   Widget build(BuildContext context) {     return

Flutter: DatePicker Tutorial both with Material and Cupertino Style

Introduction DatePicker is very important when you want the user to pick his / her date of birth or something else. In Flutter, implementing DatePicker is very easy and we will implement DatePicker in both Android or Material style and Cupertino or IOS style.  Table of contents Approach Project Setup Material Style DatePicker Cupertino Style DatePicker Conclusion Approach Flutter has widgets for everything and even for DatePicker. DatePicker widget is loaded with all needed animation and colours. So implementation is very easy.  Project Setup No extra plugins are required for this project and you can continue with your existing project. Here is the starting code. class DatePickerTutorial extends StatefulWidget { const DatePickerTutorial({Key? key}) : super (key: key); @ override _DatePickerTutorialState createState() => _DatePickerTutorialState(); } class _DatePickerTutorialState extends State<DatePick