TabBar in Flutter

18-Sep-2024

Learn how to create tab-based navigation in Flutter using the TabBar widget

TabBar is a widget used to display tabs in a horizontal row. It is often used in conjunction with  TabBarView to create  tabbed interfaces, where each tab corresponds to a different view of content. This is a simple example of how to use  TabBar in Flutter.

There Are Some Common Attributes for TabBar

lengthspecify the number of tabs
tabsAll Tabs Name

Here's a simple example of using a TabBar in Flutter:

File open lib->main.dart file −



import 'package:flutter/material.dart';

void main() {
runApp(
MyApp()
);
}
class MyApp extends StatefulWidget{

@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
return MaterialApp(
home:
DefaultTabController(
length: 4, // specify the number of tabs
child: Scaffold(
appBar: AppBar(
title: Text('TabBar Example'),
bottom: TabBar(
tabs: [
Tab(text: 'Flutter'),
Tab(text: 'Java'),
Tab(text: 'Kotlin'),
Tab(text: 'JetPack'),
],
),
),
body: TabBarView(
children: [

Container(
color: Colors.blue,
child:
Center(
child: Text('Flutter Example Here')
),
),

Container(
color: Colors.greenAccent,
child:
Center(
child: Text('Java Example Here')
),
),

Container(
color: Colors.pink,
child:
Center(
child: Text('Kotlin Example Here')
),
),
Container(
color: Colors.deepPurple,
child:
Center(
child: Text('JetPack Example Here')
),
),
],
),
),
),
);


}


}






Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments