As an avid developer, you already know that Flutter is a prominent UI toolkit from Google, set to design nifty natively compiled applications. This convenience stems from the use of Dart, a language that provides robustness through structure combined with the expressiveness and fluidity of scripting languages.
Flutter’s appeal primarily lies in its speed and dynamic UI-building capability. In this blog post, we chart a course to explore a crucial aspect of Tabs in Flutter. Through this tutorial, we will learn the ins and outs of creating and controlling tabs, thereby making the most out of Flutter Tabs, Tabbar views, and Top Tab bars.
Before diving straight into coding in Flutter, let's first have a sound understanding of the terminology associated with tabs in our context.
Tabs in any UI are visual components that allow users to switch between different views or subpages. A common pattern in mobile applications and often aligned with Material Design guidelines, tabs provide an organized, accessible approach to navigation.
Flutter Tabs extend the functionality of tabs to Flutter app development. They are part of the rich widget functionality in Flutter and provide a way to navigate between multiple interfaces or views.
The Flutter Tab bar or TabBar widget comes into play when you want to create a structured tab system. It houses an array of tabs that can be scrolled horizontally and toggled. By default, the TabBar widget plays two roles. Firstly, it displays tabs in a single row. Secondly, it allows users to switch between these tabs.
Sometimes, you don't just want a Tab bar— you want it at the top! The Flutter Top Tab bar suits scenarios where you want to affix tabs at the top of your app interface for better visibility.
That sets the stage for our vocabulary sorted out. Now, let's jump to writing some engaging Flutter code to implement tabs.
To get started with tabs in Flutter, we will create a basic app with two tabs. Each tab will contain distinct content.
Our first step in creating tabs is to set up a TabController, which coordinates tab selection between a TabBar and a TabBarView. With this in mind, let's start by creating a new Flutter app.
Create a new Dart file and remember to import the Flutter material design package at the top of your Dart code file.
1 import 'package:flutter/material.dart'; 2
Next, we'll create our TabController. The TabController requires a length for the total number of tabs and, optionally, an initial index for the currently selected tab.
1 void main() { 2 runApp(const MaterialApp( 3 home: MyHomePage(), 4 )); 5 } 6 7 class MyHomePage extends StatelessWidget { 8 @override 9 Widget build(BuildContext context) { 10 return DefaultTabController( 11 length: 2, 12 child: Scaffold( 13 appBar: AppBar( 14 title: const Text('Flutter Tabs Demo'), 15 bottom: const TabBar( 16 tabs:[ 17 Tab(icon: const Icon(Icons.directions_car)), 18 Tab(icon: const Icon(Icons.directions_transit)), 19 ], 20 ), 21 ), 22 body: const TabBarView( 23 children:[ 24 Icon(Icons.directions_car), 25 Icon(Icons.directions_transit), 26 ], 27 ), 28 ), 29 ); 30 } 31 } 32
In this example, we've created a DefaultTabController widget and set its length to 2, covering our two tabs. The TabBar is a list of two Tab widgets, represented by car and transit icons. These icons correspond to our selected tab labels and will provide a visual indicator of the currently selected tab on our screen.
The TabBarView children are where the content of each tab will live.
With that, we have created a simple Flutter app displaying tabs at the top. We also have a Flutter tab bar and have learned how to switch between tabs interactively.
Now that you understand the basics of creating tabs in Flutter, let's take it up a notch. Flutter provides us with ample options to customize tabs as per our requirements.
If you want more than the default styling for your tabs, Flutter has got you covered. Let's discuss how to attain a different visual style for unselected tab labels, create a custom indicator, change the TabBar background color, and more.
We can modify the look of each tab using the Tab widget's optional properties, with a focus on our text style and icon.
1 Tab( 2 icon: const Icon(Icons.directions_car, size: 48.0, color: Colors.green), 3 text: 'Car', 4 ) 5
With this code, we've adjusted the dimensions of the icon and also changed its color. Moreover, we've labeled our tab for a clearer understanding.
Just as we can customize individual Tab widgets, Flutter grants us the power to customize our entire tab bar with properties such as indicatorColor, indicatorSize, unselectedLabelColor, and more. The below example demonstrates how you can fully customize your Flutter tab bar.
1 TabBar( 2 indicatorColor: Colors.purple, 3 indicatorSize: TabBarIndicatorSize.tab, 4 unselectedLabelColor: Colors.grey, 5 tabs: [ 6 Tab( 7 icon: const Icon(Icons.directions_car), 8 text: 'Car', 9 ), 10 Tab( 11 icon: const Icon(Icons.directions_transit), 12 text: 'Transit', 13 ), 14 ], 15 ) 16
This TabBar has a purple tab indicator and grey unselected tab labels. The indicatorSize property allows us to adjust the size of the tab indicator according to the tab's dimensions.
With just a few tweaks, you're now closer to designing your very own customized flutter tabs, providing a personalized user experience to your app users.
Now let's create a simple Flutter tabs application to fortify our understanding.
1 void main() { 2 runApp(const MaterialApp( 3 home: Home(), 4 )); 5 } 6 7 class Home extends StatelessWidget { 8 const Home({Key? key}) : super(key: key); 9 10 @override 11 Widget build(BuildContext context) { 12 return DefaultTabController( 13 length: 3, 14 child: Scaffold( 15 appBar: AppBar( 16 title: const Text('Flutter Tabs Example'), 17 bottom: const TabBar( 18 tabs:[ 19 Tab(text: 'Car', icon: const Icon(Icons.directions_car)), 20 Tab(text: 'Transit', icon: const Icon(Icons.directions_transit)), 21 Tab(text: 'Bike', icon: const Icon(Icons.directions_bike)), 22 ], 23 ), 24 ), 25 body: const TabBarView( 26 children:[ 27 Center(child: const Text('Car')), 28 Center(child: const Text('Transit')), 29 Center(child: const Text('Bike')), 30 ], 31 ), 32 ), 33 ); 34 } 35 } 36
In this application, we have created three tabs labeled 'Car', 'Transit', and 'Bike'. On switching tabs, the corresponding label is displayed in the Center widget.
1 void main() => runApp(const App()); 2 3 class App extends StatelessWidget { 4 const App({Key? key}) : super(key: key); 5 6 @override 7 Widget build(BuildContext context) { 8 return MaterialApp( 9 home: DefaultTabController( 10 length: 3, 11 child: Scaffold( 12 appBar: AppBar( 13 title: const Text('Flutter Demo'), 14 bottom: const TabBar( 15 tabs: [ 16 Tab(icon: const Icon(Icons.directions_car)), 17 Tab(icon: const Icon(Icons.directions_transit)), 18 Tab(icon: const Icon(Icons.directions_bike)), 19 ], 20 ), 21 ), 22 body: const TabBarView( 23 children: [ 24 Icon(Icons.directions_car), 25 Icon(Icons.directions_transit), 26 Icon(Icons.directions_bike), 27 ], 28 ), 29 ), 30 ), 31 ); 32 } 33 } 34
In this code, we have built on our basic tab app to include navigation between different Tab Views. Upon clicking the various tabs, different icons are displayed, indicating the present active tab view.
These were some examples of implementing simple navigation between different tab views.
Utilizing Flutter's tab utilities to their full potential means ensuring your approach adheres to best practices. This not only involves traits like code maintainability but also the app's responsiveness from the user's end. So, let's discuss some of those key practices.
DO make your tabs scrollable when there are many of them! Flutter gives us the option to make our tabs scrollable. This can be done by setting the isScrollable property of the TabBar to true. This way, the user can move sideways in response to swiping gestures.
1 TabBar( 2 isScrollable: true, 3 tabs: [ 4 // Your tabs here! 5 ], 6 ) 7
DO make use of the TabController. Taking control of tabs' switching behavior can be important in certain use cases. Becoming comfortable with using this element to manipulate how the user interacts with the tabs will aid you in these scenarios.
DON'T use too many tabs if they don't fit onto the screen. According to material design guidelines, tabs should be immediately visible and accessible for a user-friendly experience.
DO use sufficiently descriptive labels or icons. The user should be able to guess the nature of the tab's contents quickly.
With such best practices and careful planning, your tabs would not just be functional but also efficient and user-friendly.
One of the most commonly overlooked aspects of creating tabs is performance, particularly when dealing with complex tabs that include images, videos, or large amounts of data. Adhering to the best practices mentioned earlier is the first step toward performance optimization, but let's go a step further to ensure smooth navigation for our users.
Lastly, testing is crucial in assuring the performance and reliability of your app. Here are some key points to keep in mind while testing your app with Flutter tabs:
Keeping these best practices in mind will help you create a robust, user-friendly application using Flutter tabs.
Now that you've become proficient in creating and controlling tabs, it's time to explore what else Flutter has to offer.
The beauty of Flutter lies in its rich set of widgets. These widgets allow you to create powerful UIs with smooth animations and transitions. Flutter provides a comprehensive catalog of Material and Cupertino (iOS-style) widgets. You've got an array of options, from basic widgets for layout and interaction to complex ones for operation and activity.
Always a great starting point. It provides a comprehensive guide to all things Flutter.
An excellent resource for understanding the various widgets available in Flutter.
A lively community of developers that helps you troubleshoot any issues you might face during development.
With these resources at your disposal, coupled with experimenting and implementation, you're on a solid track to elevate your Flutter skills.
In this tutorial, we've learned the fundamentals of using tabs in a Flutter app, from understanding their inherent need to implementing basic tabs and customizing them for a unique user interface. We've also tread the paths of best practices, performance optimization, and testing methods.
While creating applications with Flutter, it's the multitude of widgets and their utilization that defines how enriching the end-user experience would be. Therefore, mastery of tabs in Flutter expands your toolkit and ensures a better, more functional, and user-friendly product.
It's time to apply what you've learned. Start with small projects, experiment with different types of tabs, and gradually move on to building complex Flutter applications. Keep learning and growing.
The golden rule of programming is; Practice is the key to proficiency. Keep fluttering! 💙
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.