Skip to content

Instantly share code, notes, and snippets.

@stegrams
Created May 10, 2020 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stegrams/24ba7954d5bc0676f1274c0a19ab412e to your computer and use it in GitHub Desktop.
Save stegrams/24ba7954d5bc0676f1274c0a19ab412e to your computer and use it in GitHub Desktop.
Minimalistic bottom tabbar sample inspired by Paul Halliday's Flutter Shopping
// Inspired by https://github.com/PaulHalliday/flutter_indexed_stack_tab_view
import 'package:flutter/material.dart';
typedef RouteFor = Future<dynamic> Function(BuildContext);
RouteFor createRoute(Widget widget) =>
(BuildContext context) => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => widget,
));
abstract class TabPage extends StatelessWidget {
static Tab getTab(TabPage page) => page.tab;
Tab get tab;
Widget body(BuildContext context);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(tab.text),
),
body: body(context),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final List<TabPage> pages = [
HomePage(),
ShopPage(),
SearchPage(),
];
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
theme: ThemeData(
primaryColor: Colors.green,
tabBarTheme: TabBarTheme(
labelColor: Colors.green,
unselectedLabelColor: Colors.grey,
),
),
home: DefaultTabController(
length: pages.length,
child: Scaffold(
body: TabBarView(
children: pages,
),
bottomNavigationBar: TabBar(
tabs: pages.map(TabPage.getTab).toList(),
),
),
),
);
}
}
class HomePage extends TabPage {
static RouteFor route = createRoute(HomePage());
@override
final Tab tab = Tab(
text: 'Home',
icon: Icon(Icons.home),
);
@override
Widget body(BuildContext context) {
return Center(
child: Text('Hello, Home!'),
);
}
}
class ShopPage extends TabPage {
static RouteFor route = createRoute(ShopPage());
@override
final Tab tab = Tab(
text: 'Shop',
icon: Icon(Icons.shopping_basket),
);
@override
Widget body(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () => ProductDetailPage.route(context),
child: Text('Navigate to Product Detail Page'),
),
);
}
}
class ProductDetailPage extends TabPage {
static RouteFor route = createRoute(ProductDetailPage());
@override
final Tab tab = Tab(
text: 'Product Detail',
icon: Icon(Icons.details),
);
@override
Widget body(BuildContext context) {
return Center(
child: Text('Hello, Product!'),
);
}
}
class SearchPage extends TabPage {
static RouteFor route = createRoute(SearchPage());
@override
final Tab tab = Tab(
text: 'Search',
icon: Icon(Icons.search),
);
@override
Widget body(BuildContext context) {
return Center(
child: Text('Hello, Search!'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment