The Wayback Machine - https://web.archive.org/web/20240627214620/https://www.geeksforgeeks.org/bottomnavigationbar-widget-in-flutter/
Open In App

BottomNavigationBar Widget in Flutter

Last Updated : 27 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The BottonNavigationBar widget is used to show the bottom of an app. It can consist of multiple items such as icons, text, or both that leads to a different route depending upon the design of the application. It is meant to help the user navigate to different sections of the application in general.

Constructors:

Syntax:
BottomNavigationBar(
{Key key, 
@required List<BottomNavigationBarItem> items, 
ValueChanged<int> onTap, 
int currentIndex: 0, 
double elevation, 
BottomNavigationBarType type, 
Color fixedColor, 
Color backgroundColor, 
double iconSize: 24.0, 
Color selectedItemColor, 
Color unselectedItemColor, 
IconThemeData selectedIconTheme, 
IconThemeData unselectedIconTheme, 
double selectedFontSize: 14.0, 
double unselectedFontSize: 12.0, 
TextStyle selectedLabelStyle, 
TextStyle unselectedLabelStyle, 
bool showSelectedLabels: true, 
bool showUnselectedLabels, 
MouseCursor mouseCursor})

Properties:

  • hashCode: The hash code for this object.
  • key: Controls how one widget replaces another widget in the tree.
  • runtimeType: A representation of the runtime type of the object.
  • backgrounColor: The color of the BottomNavigationBar itself.
  • elevation: The z-coordinate of this BottomNavigationBar.
  • fixedColor: The z-coordinate of this BottomNavigationBar.
  • items: Defines the appearance of the button items that are arrayed within the bottom navigation bar.
  • onTap: Called when one of the items is tapped.
  • currentIndex: This property takes an int value as the object to assign index t the items.
  • fixedColor: This property takes in Color class as the object to assign a fixed value to the SelectedItemColor.
  • iconSize: It takes a double value as the object to determine the size of all the icons in the BottomNavigationBar.
  • mouseCursor: The mouseCursor property takes MouseCursor class as the object. It determines the type of cursor this widget will have.
  • selectedFontSize: This property controls the font size in the BottomNavigationBar when the items are selected. It takes a double value as the object.
  • selectedIcontheme: This property holds IconThemeData class as the object to controls the appearance of the icons this widget when it is selected.
  • selectedIconColor: This property determines the color of the icons inside this widget will hold when they are selected. This property takes Color class as the property.
  • selectedLabelStyle: TextStyle class is the object assigned to this property which controls the text style at the instance of selection.
  • showSelectedLabele: This property takes a boolean value as the object to determine whether or not the labels for the unselected item will be shown.
  • showUnselectedLabels: This property also takes in a boolean value as the object to determine whether or not the labels for selected items will be shown.
  • type: The type property controls the behaviour and the layout of the BottomNavigationBar. It takes BottomNavigationBarType enum as the object.
  • unselectedFontSize: This property also takes a double value as e object to determine the size of font when the item is not selected.
  • unselectedIconTheme: This property also holds IconThemeData class as the object to controls the appearance of the icons this widget when it is unselected or not selected.
  • unselectedItemColor: This property determines the color the icons inside this widget will hold when they are unselected. This property takes Color class as the property.
  • unselectedLabelStyle: TextStyle class is the object assigned to this property which controls the text style when the item is unselected.

Example:

Dart




import 'package:flutter/material.dart';
 
void main() => runApp(MyApp());
 
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}
 
class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);
 
  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
 
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
  TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'HOME PAGE',
      style: optionStyle,
    ),
    Text(
      'COURSE PAGE',
      style: optionStyle,
    ),
    Text(
      'CONTACT GFG',
      style: optionStyle,
    ),
  ];
 
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GeeksForGeeks'),
        backgroundColor: Colors.green,
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.bookmark),
            title: Text('Courses'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.contact_mail),
            title: Text('Mail'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}


 
 

Output:

Explanation:

  1. First, create the stateless main widget.
  2. Second use the widget builder to create an appbar inside the scaffold.
  3. Third use the ButtomNavigationBar widget in the body of the main app
  4. Do not forget to set the navbar at the bottom of the app using the style property.

 



Similar Reads

Expandable Widget in Flutter
The expandable widgets do not have a fixed size, they expand on the screen according to the available areas on the screen. Due to size constraints, certain widgets can throw rendering errors, so widgets are made expandable. The one use of expandable widgets is cards that include images, texts, and to further read the text we can click "Read More" o
3 min read
Flutter - Card Widget
Card is a build-in widget in flutter which derives its design from Google's Material Design Library. The functionality of this widget on screen is, that it is a bland space or panel with round corners and a slight elevation on the lower side. It comes with many properties like color, shape, shadow color, etc which lets developers customize it the w
4 min read
Flutter - ColoredBox Class Widget
A Colored Box is a container that fills it with color according to its child or A widget that paints its area with a specified Color and then draws its child on top of that color. A sample image is given below to get an idea about what we are going to do in this article. How to use it? You can simple use this widget anywhere in your project as you
3 min read
How to Get the Height of a Widget in Flutter?
To get the height of a widget in Flutter, you can use the LayoutBuilder widget to obtain the constraints of the parent container and then get the height from those constraints. How to use it? You can simply use this widget anywhere in your project as you needed that. LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { doubl
3 min read
Difference Between Stateless and Stateful Widget in Flutter
As we all know the flutter app consists of widgets only, these are broadly classified into two types: Stateless WidgetStateful WidgetState: Before knowing the difference between the stateless and stateful widget we should take a look at the definition of State of a widget. The State is information that can read synchronously when the widget is buil
3 min read
Flutter - Wrap Widget
Wrap widget aligns the widgets in a horizontal and vertical manner. Generally, we use Rows and Columns to do that but if we have some widgets which are not able to fit in the Row/Column then it will give us Overflow Message ( for ex: Right Overflowed by 570 pixels). Constructor of Wrap class:Wrap({Key key, Axis direction, WrapAlignment alignment, d
3 min read
Raised Button widget in Flutter
RaisedButton is the material design button based on a Material widget that elevates when pressed upon in flutter. It is one of the most widely used buttons in the flutter library. Let's understand this button with the help of an example. Disclaimer: As of May 2021 the RaisedButton class in flutter is deprecated. ElevatedButton class should be used
5 min read
OffStage Widget in Flutter
Flutter provides an inbuilt widget called “Offstage”, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit-testing, and without taking any room in the paren
4 min read
NotificationListener Widget in Flutter
In Flutter, every Scrollable sends Notifications that contain information about the current scroll state. So to catch these notifications we use NotificationListener Widget. NotificationListener Widget will listen for Notifications bubbling up the tree. In this, article we will learn about NotificationListener Widget Default Constructor:Notificatio
4 min read
endDrawer Widget in Flutter
The endDrawer is the panel displayed to the side of the body (Scaffold Widget). It is generally hidden in mobile devices. We can open it by swiping in from right to left, or we can customise it to open on-press of an icon or a button. This widget is similar to the already present Drawer widget in flutter except for the fact the Drawer by default op
2 min read
Article Tags :
three90RightbarBannerImg