LibraryBottom Navigation Bar and TabBar

Bottom Navigation Bar and TabBar

Learn about Bottom Navigation Bar and TabBar as part of Flutter App Development with Dart

Flutter Navigation: BottomNavigationBar and TabBar

In Flutter, users often need to switch between different sections or views within an application. Two common widgets for achieving this are the

code
BottomNavigationBar
and
code
TabBar
. These widgets provide intuitive ways for users to navigate through your app's content.

Understanding BottomNavigationBar

The

code
BottomNavigationBar
widget is typically placed at the bottom of the screen and displays a series of icons and labels. Tapping an item in the
code
BottomNavigationBar
usually changes the main content displayed in the center of the screen. It's ideal for apps with a small number of distinct top-level destinations.

BottomNavigationBar provides persistent navigation at the bottom of the screen.

It's a row of icons and labels, allowing users to switch between major sections of an app. Each item is a BottomNavigationBarItem.

The BottomNavigationBar widget is a Material Design component that displays a list of destinations at the bottom of the screen. It's commonly used for apps with 3-5 primary destinations. Each destination is represented by a BottomNavigationBarItem, which consists of an icon and an optional label. The currentIndex property of the BottomNavigationBar determines which item is currently selected, and the onTap callback is triggered when an item is tapped, allowing you to update the currentIndex and thus change the displayed content.

What is the primary purpose of the BottomNavigationBar widget in Flutter?

To provide persistent navigation at the bottom of the screen, allowing users to switch between major sections of an app.

Understanding TabBar

The

code
TabBar
widget is a Material Design component that displays a horizontal set of tabs. It's often used in conjunction with a
code
TabBarView
to allow users to switch between different content panels. This is particularly useful when you have related content that can be organized into distinct sections.

TabBar organizes related content into swipeable panels.

It's a row of tabs, usually at the top, that correspond to different views managed by a TabBarView. Swiping left or right on the TabBarView also changes the active tab.

The TabBar widget is typically placed within an AppBar or as a standalone widget. It displays a list of Tab widgets, each with a label and an optional icon. The TabBar is usually controlled by a TabController, which manages the selection and animation between tabs. The TabBarView widget displays the content associated with each tab. When a tab is selected, the TabBarView animates to display the corresponding child widget. This combination is excellent for presenting different facets of the same feature or data.

FeatureBottomNavigationBarTabBar
Primary PlacementBottom of screenTypically top (AppBar) or below AppBar
Use CaseSwitching between major app sections (3-5 destinations)Organizing related content into distinct panels
InteractionTap to change viewTap to change view; often swipeable with TabBarView
Visual StyleIcons and labels at the bottomTabs (text/icons) in a row, often with indicator

Visualizing the structure of a BottomNavigationBar with its BottomNavigationBarItems and a TabBar with its Tabs and TabBarView helps understand their distinct roles in app navigation. The BottomNavigationBar is for high-level navigation between independent sections, while the TabBar is for navigating within a specific section, often presenting related content.

📚

Text-based content

Library pages focus on text content

Implementing Navigation

To implement these navigation patterns, you'll typically use a

code
Scaffold
widget. For
code
BottomNavigationBar
, you'll assign your
code
BottomNavigationBar
widget to the
code
bottomNavigationBar
property of the
code
Scaffold
. For
code
TabBar
, you'll usually place the
code
TabBar
widget within the
code
AppBar
's
code
bottom
property and use a
code
TabBarView
as a child of a widget that manages the
code
TabController
, such as a
code
Scaffold
or
code
StatefulWidget
.

Remember to manage the state of your selected index for BottomNavigationBar and use a TabController for TabBar to ensure smooth transitions and correct display of content.

Key Components

When working with

code
BottomNavigationBar
, you'll use
code
BottomNavigationBarItem
to define each selectable item. For
code
TabBar
, you'll use
code
Tab
widgets to define the tabs and
code
TabBarView
to display the corresponding content panels. A
code
TabController
is crucial for coordinating the
code
TabBar
and
code
TabBarView
.

Learning Resources

Flutter BottomNavigationBar Widget(documentation)

Official Flutter documentation for the BottomNavigationBar widget, explaining its properties and usage.

Flutter TabBar Widget(documentation)

Official Flutter documentation for the TabBar widget, detailing how to create tabbed interfaces.

Flutter TabBarView Widget(documentation)

Official Flutter documentation for the TabBarView widget, which displays content corresponding to each tab.

Flutter Navigation: Bottom Navigation Bar(video)

A video tutorial demonstrating how to implement a BottomNavigationBar in Flutter.

Flutter Tabs and TabBarView Tutorial(video)

A comprehensive video guide on using TabBar and TabBarView for navigation in Flutter.

Flutter Bottom Navigation Bar Example(documentation)

A practical example from the Flutter Cookbook showing how to build a bottom navigation bar.

Flutter Tabs Example(documentation)

A practical example from the Flutter Cookbook demonstrating the use of Tabs and TabBarView.

Flutter TabController Explained(blog)

A blog post that dives deep into the Flutter TabController and its role in managing tabs.

Material Design - Navigation(documentation)

The official Material Design guidelines for bottom navigation, providing best practices and design principles.

Material Design - Tabs(documentation)

The official Material Design guidelines for tabs, covering their usage and visual design.