xxxxxxxxxx
// Import the required Flutter libraries
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('TabBar Example'),
),
body: DefaultTabController(
length: 3, // Number of tabs
child: Column(
children: <Widget>[
Container(
constraints: BoxConstraints.expand(height: 50),
child: TabBar(
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
),
),
Expanded(
child: TabBarView(
children: [
// Content of Tab 1
Center(
child: Text('Tab 1 Content'),
),
// Content of Tab 2
Center(
child: Text('Tab 2 Content'),
),
// Content of Tab 3
Center(
child: Text('Tab 3 Content'),
),
],
),
),
],
),
),
),
);
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
class MyTabBarView extends StatefulWidget {
@override
_MyTabBarViewState createState() => _MyTabBarViewState();
}
class _MyTabBarViewState extends State<MyTabBarView> with SingleTickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this); // Replace '3' with the number of tabs you have
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tab Bar View Example'),
bottom: TabBar(
controller: _tabController,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
),
),
body: TabBarView(
controller: _tabController,
children: [
// Widgets to display for each tab
Container(
color: Colors.red,
child: Center(child: Text('Tab 1 Content')),
),
Container(
color: Colors.blue,
child: Center(child: Text('Tab 2 Content')),
),
Container(
color: Colors.green,
child: Center(child: Text('Tab 3 Content')),
),
],
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tab Bar View Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyTabBarView(),
);
}
}
xxxxxxxxxx
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
),
),
);
xxxxxxxxxx
body: TabBarView(
children: [
Center(
child: Text("It's cloudy here"),
),
Center(
child: Text("It's rainy here"),
),
Center(
child: Text("It's sunny here"),
),
],
),
xxxxxxxxxx
int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Home',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
xxxxxxxxxx
An example of Flutter Tabbar implementation is given here:
https://blog.logrocket.com/flutter-tabbar-a-complete-tutorial-with-examples/