1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2026-08-25 01:05:36 +00:00
Files
flutter-examples/using_tabs/lib/example.dart
Nishant Srivastava e143b4c3a2 refactor: split app wrapper from focused example code (pilot)
Apply a consistent structure to 5 example apps (simple_material_app,
stateful_widget, dropdown_button, using_gradient, using_tabs):

- lib/main.dart now contains only the app bootstrap: runApp + a const
  MyApp widget wrapping the app in a MaterialApp.
- lib/example.dart holds the full screen (Scaffold, AppBar, and the
  focused code that demonstrates the example feature), marked with a
  '// Example:' banner comment.

This makes it easy to pinpoint the code that teaches each concept while
keeping the wrapping/chrome code uniform and minimal. Review this pilot
before rolling the pattern out to the remaining apps.
2026-08-18 00:24:22 +02:00

75 lines
1.8 KiB
Dart

import 'package:flutter/material.dart';
import 'tabs/first.dart';
import 'tabs/second.dart';
import 'tabs/third.dart';
// Example: the focused code for this app.
// A TabController wired to a TabBar (in the AppBar) and a TabBarView
// showing three tabs.
class Example extends StatefulWidget {
const Example({super.key});
@override
State<Example> createState() => _ExampleState();
}
// SingleTickerProviderStateMixin is used for animation
class _ExampleState extends State<Example> with SingleTickerProviderStateMixin {
late TabController controller;
@override
void initState() {
super.initState();
// Initialize the Tab Controller
controller = TabController(length: 3, vsync: this);
}
@override
void dispose() {
// Dispose of the Tab Controller
controller.dispose();
super.dispose();
}
TabBar getTabBar() {
return TabBar(
tabs: <Tab>[
// set icon to the tab
const Tab(icon: Icon(Icons.favorite)),
const Tab(icon: Icon(Icons.adb)),
const Tab(icon: Icon(Icons.airport_shuttle)),
],
// setup the controller
controller: controller,
);
}
TabBarView getTabBarView(List<Widget> tabs) {
return TabBarView(
// set the controller
controller: controller,
// Add tabs as widgets
children: tabs,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
// Appbar
appBar: AppBar(
// Title
title: const Text("Using Tabs"),
// Set the background color of the App Bar
backgroundColor: Colors.blue,
// Set the bottom property of the Appbar to include a Tab Bar
bottom: getTabBar(),
),
// Set the TabBar view as the body of the Scaffold
body:
getTabBarView(<Widget>[const First(), const Second(), const Third()]),
);
}
}