Learn How to Effortlessly Navigate Your Flutter App with Tab Navigation

Ajiyemi Michael
3 min readApr 3, 2023

--

Tabs are a popular and convenient way to navigate through mobile apps. They provide a modern look and feel, allowing users to quickly move from one screen to another. Tabs can be placed either at the top or the bottom of the app, depending on your preference. To create a tab in your Flutter app, you will need three components: a TabBar, a TabBarView, and a TabController. These components work together to create a seamless tabbed interface.

The TabBar represents a list of tabs that the user can select to navigate to different sections of the app. The TabBarView displays the corresponding section of the app that the user selected. The TabController connects both the TabBar and the TabBarView together, allowing Flutter to coordinate which tab is selected and which content to display.

While you can create a TabController manually, Flutter provides a convenient and easy way to do this out of the box with the DefaultTabController widget. This widget creates a TabController for you under the hood and handles all the necessary syncing between the TabBar and the TabBarView. Now, let's see how to implement this in code.

void main() => runApp(
MaterialApp(
home: DefaultTabController(
length: 4,
child: Scaffold(),
),
),
);

When using the DefaultTabController widget, it's important to set the length property to match the number of tabs you want to display on the screen. This value must also match the number of tabs in your TabBar widget and the number of children in your TabBarView widget. By ensuring that these values are all in sync, you can create a smooth and seamless tabbed interface.

To create a TabBar widget, you simply pass a list of Tab widgets to the tabs parameter. Each Tab widget represents a single tab in the tab bar. Here’s an example of creating a TabBar widget:

child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: [
Tab(
icon: Icon(Icons.directions_car),
),
Tab(
icon: Icon(Icons.directions_transit),
),
Tab(
icon: Icon(Icons.directions_bike),
),
Tab(
icon: Icon(Icons.directions_train),
),
],
),

It’s important to note that your TabBar must be wrapped inside a DefaultTabController widget. This is necessary to establish a connection between the TabBar and the TabBarView, and to ensure that they work together seamlessly.

The TabBarView widget takes a list of widgets, which can represent different screens or sections of your app. Each widget corresponds to a single tab in the TabBar. By default, the TabBarView widget will display the first widget in the list, but the user can easily switch between tabs by tapping on the corresponding tab in the TabBar. Here's an example of creating a TabBarView widget:

body: const TabBarView(
children: [
CarScreen(),
HouseScreen(),
ParkScreen(),
BusStopScreen(),
],
),

Bringing everything together, your code for creating a tabbed interface in Flutter might look something like this:

void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: [
Tab(
icon: Icon(Icons.directions_car),
),
Tab(
icon: Icon(Icons.directions_transit),
),
Tab(
icon: Icon(Icons.directions_bike),
),
Tab(
icon: Icon(Icons.directions_train),
),
],
),
title: const Text('Tabs Demo'),
),
body: const TabBarView(
children: [
CarScreen(),
HouseScreen(),
ParkScreen(),
BusStopScreen(),
],
),
),
),
),
);

In conclusion, using tabs is an excellent way to provide easy navigation for your Flutter app users. By implementing a TabBar and a TabBarView connected by a TabController, you can quickly and easily display multiple screens within your app. Remember to use the DefaultTabController widget to simplify the process and ensure that everything is in sync. With these tools and a little bit of customization, you can create a modern and intuitive interface for your app. So go ahead and start experimenting with tabs in your next Flutter project. And lastly, don’t forget to have fun while you’re working with tabs in your Flutter app! While implementing this feature may seem daunting at first, it can be a great opportunity to learn and improve your development knowledge. With practice and experimentation, you’ll gain the confidence you need to create even more complex and impressive interfaces for your users. So take the time to enjoy the process, and see how far you can take your Flutter app with the power of tabs.

--

--

Ajiyemi Michael
Ajiyemi Michael

Written by Ajiyemi Michael

i AM a flutter developer, i write because it is my way of giving back to the community, to share my knowledge and experiences with others

No responses yet