Created
May 10, 2020 01:23
-
-
Save stegrams/24ba7954d5bc0676f1274c0a19ab412e to your computer and use it in GitHub Desktop.
Minimalistic bottom tabbar sample inspired by Paul Halliday's Flutter Shopping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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