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: 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...

Flutter: DraggableScrollableSheet widget Tutorial

Introduction In this tutorial, we are going to learn about the DraggableScrollableSheet widget. It is a widget that responds to user gestures. When the user drags from the bottom of the screen in the upward direction, the widget opens up and when scrolls down the widget close. The final app will look as follows  Final App DraggableScrollableSheet It is a widget that scrolls up and down of the viewport or screen according to the user's screen gestures. This widget becomes handy when you want to give some extra details which are lengthy content but don't want to navigate to a new screen. You might have come across many apps that implement the same thing. So let us learn to implement DraggableScrollableSheet in our own app. Approach The approach of our project is very simple. In our app, we are going to display some names of countries with the help of the  ListView.builder  and Li...

Flutter : Image Picker Tutorial | Pick single image from Gallery

Introduction In this tutorial, we will create an image picker and display the content on the screen . I mage Picker picks a file from the Storage or Camera and stores it in an XFile object. Implementation Install dependency We will first need the image_picker dependency . To install it, add the following dependency to pubspec.yaml file. image_picker: ^0.8.4+3 Then click on Get Packages in your IDE or run the following command on your Terminal / Command Prompt. flutter pub get Example Import the dependency in your dart file. import 'package:image_picker/image_picker.dart' ; Syntax Image picking is a future task . So we need to await the image picking . Here is the syntax. await _picker.pickImage( source : ImageSource.gallery); Here we have provided the source from the gallery . We can also provide the source as a Camera using the following syntax. await _picker.pickImage( source : Ima...