Skip to content

Instantly share code, notes, and snippets.

@hamishnorton
Last active August 26, 2022 03:24
Show Gist options
  • Save hamishnorton/3ff28c9d083da7c58d32644b3ef54b92 to your computer and use it in GitHub Desktop.
Save hamishnorton/3ff28c9d083da7c58d32644b3ef54b92 to your computer and use it in GitHub Desktop.
Flutter Tabs Example
import 'package:flutter/material.dart';
// https://api.flutter.dev/flutter/material/DefaultTabController-class.html
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String title = 'Flutter: Tabs Example';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MainScreen(title: title),
);
}
}
class MainScreen extends StatelessWidget {
const MainScreen({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tabs Example'),
),
body: DefaultTabController(
length: 4, // length of tabs
initialIndex: 0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const TabBar(
labelColor: Colors.green,
unselectedLabelColor: Colors.black,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
Tab(text: 'Tab 4'),
],
),
Container(
height: 400, //height of TabBarView
decoration: const BoxDecoration(
border: Border(
top: BorderSide(color: Colors.grey, width: 0.5))),
child: const TabBarView(children: <Widget>[
Center(
child: Text('Display Tab 1',
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 2',
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 3',
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 4',
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold)),
),
]))
])),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment