Flutter Tabbar

Tabs is a widget of Flutter, it's a part of the UI that navigates the user through different tabs. The Flutter mobile application provides a simple way to create tab layouts using the material library.


To better understand the concept of tab view or functionality in a Flutter application, consider the following points:

  •   Design a tab controller.
  •   Add tabs in the app.
  •   Add content in every tab.


Let’s discuss them in detail

The tab controller is used to add tabs on the screen. The default tab controller widget is the simplest way to create tabs in the Flutter application.

Step 1: Default tab controller used in creating tabs.

DefaultTabController(

// total 5 tabs

length: 5,

child:

);

Step 2: Adding tabs 

You can add tabs in the Flutter using a TabBar widget, shown below:

home: DefaultTabController(

length: 5,

child: Scaffold(

appBar: AppBar(

bottom: const TabBar(

tabs: [

Tab(icon: Icon(Icons.music_note)),

Tab(icon: Icon(Icons.music_video)),

Tab(icon: Icon(Icons.camera_alt)),

Tab(icon: Icon(Icons.grade)),

Tab(icon: Icon(Icons.email)),],), //TabBar

Step 3: Adding content to tabs: 

The TabBarView widget can be used to specify the contents of each tab. We will display icons in the tab as the content of the tab as shown below.

body: const TabBarView(

children: [

Icon(Icons.music_note),

Icon(Icons.music_video),

Icon(Icons.camera_alt),

Icon(Icons.grade),

Icon(Icons.email),

],

), // TabBarView

Step 4: Full code of tab view example 

import 'package:flutter/material.dart';

void main() {

runApp(const TabBarDemo());

}

// class to build the app

class TabBarDemo extends StatelessWidget {

const TabBarDemo({Key? key}) : super(key: key);

// build the app

@override

Widget build(BuildContext context) {

return MaterialApp(

home: DefaultTabController(

length: 5,

child: Scaffold(

appBar: AppBar(

bottom: const TabBar(

tabs: [

Tab(icon: Icon(Icons.email)),

Tab(icon: Icon(Icons.music_video)),

Tab(icon: Icon(Icons.music_note)),

Tab(icon: Icon(Icons.camera_alt)),

Tab(icon: Icon(Icons.grade)),

],

), // TabBar

title: const Text('Candidroot solutions'),

titleTextStyle: const TextStyle(

color: Colors.white,fontSize: 20

),

backgroundColor: Colors.blue,

), // AppBar

body: const TabBarView(

children: [

Icon(Icons.email),

Icon(Icons.music_video),

Icon(Icons.music_note),

Icon(Icons.camera_alt),

Icon(Icons.grade),

],

), // TabBarView

), // Scaffold

), // DefaultTabController

); // MaterialApp

}

}

Happy coding!


365Bloggy May 13, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive