Commit a9ddbb4e authored by Hans Muller's avatar Hans Muller

Merge pull request #1007 from HansMuller/revise_tabbar_selection

Make TabBarSelection a widget

TabBarSelection is now expected to be an ancestor of its TabBar and TabBarView.
parents 95b50915 6178fdff
...@@ -7,11 +7,16 @@ import 'package:flutter/material.dart'; ...@@ -7,11 +7,16 @@ import 'package:flutter/material.dart';
import 'widget_demo.dart'; import 'widget_demo.dart';
final List<String> _iconNames = <String>["event", "home", "android", "alarm", "face", "language"]; final List<String> _iconNames = <String>["event", "home", "android", "alarm", "face", "language"];
final TabBarSelection _selection = new TabBarSelection(maxIndex: _iconNames.length - 1);
Widget buildTabBar(_) { Widget _buildTabBarSelection(_, Widget child) {
return new TabBarSelection(
maxIndex: _iconNames.length - 1,
child: child
);
}
Widget _buildTabBar(_) {
return new TabBar( return new TabBar(
selection: _selection,
isScrollable: true, isScrollable: true,
labels: _iconNames.map((String iconName) => new TabLabel(text: iconName, icon: "action/$iconName")).toList() labels: _iconNames.map((String iconName) => new TabLabel(text: iconName, icon: "action/$iconName")).toList()
); );
...@@ -24,7 +29,6 @@ class TabsDemo extends StatefulComponent { ...@@ -24,7 +29,6 @@ class TabsDemo extends StatefulComponent {
class _TabsDemoState extends State<TabsDemo> { class _TabsDemoState extends State<TabsDemo> {
Widget build(_) { Widget build(_) {
return new TabBarView<String>( return new TabBarView<String>(
selection: _selection,
items: _iconNames, items: _iconNames,
itemBuilder: (BuildContext context, String iconName, int index) { itemBuilder: (BuildContext context, String iconName, int index) {
return new Container( return new Container(
...@@ -42,6 +46,7 @@ class _TabsDemoState extends State<TabsDemo> { ...@@ -42,6 +46,7 @@ class _TabsDemoState extends State<TabsDemo> {
final WidgetDemo kTabsDemo = new WidgetDemo( final WidgetDemo kTabsDemo = new WidgetDemo(
title: 'Tabs', title: 'Tabs',
routeName: '/tabs', routeName: '/tabs',
tabBarBuilder: buildTabBar, tabBarBuilder: _buildTabBar,
pageWrapperBuilder: _buildTabBarSelection,
builder: (_) => new TabsDemo() builder: (_) => new TabsDemo()
); );
...@@ -4,12 +4,22 @@ ...@@ -4,12 +4,22 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
typedef Widget PageWrapperBuilder(BuildContext context, Widget child);
class WidgetDemo { class WidgetDemo {
WidgetDemo({ this.title, this.routeName, this.tabBarBuilder, this.floatingActionButtonBuilder, this.builder }); WidgetDemo({
this.title,
this.routeName,
this.tabBarBuilder,
this.pageWrapperBuilder,
this.floatingActionButtonBuilder,
this.builder
});
final String title; final String title;
final String routeName; final String routeName;
final WidgetBuilder tabBarBuilder; final WidgetBuilder tabBarBuilder;
final PageWrapperBuilder pageWrapperBuilder;
final WidgetBuilder floatingActionButtonBuilder; final WidgetBuilder floatingActionButtonBuilder;
final WidgetBuilder builder; final WidgetBuilder builder;
} }
...@@ -63,20 +63,27 @@ class _GalleryPageState extends State<GalleryPage> { ...@@ -63,20 +63,27 @@ class _GalleryPageState extends State<GalleryPage> {
return builder != null ? builder(context) : null; return builder != null ? builder(context) : null;
} }
Widget _buildPageWrapper(BuildContext context, Widget child) {
final PageWrapperBuilder builder = config.active?.pageWrapperBuilder;
return builder != null ? builder(context, child) : child;
}
Widget _buildFloatingActionButton() { Widget _buildFloatingActionButton() {
final WidgetBuilder builder = config.active?.floatingActionButtonBuilder; final WidgetBuilder builder = config.active?.floatingActionButtonBuilder;
return builder != null ? builder(context) : null; return builder != null ? builder(context) : null;
} }
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( return _buildPageWrapper(context,
toolBar: new ToolBar( new Scaffold(
center: new Text(config.active?.title ?? 'Flutter Material gallery'), toolBar: new ToolBar(
tabBar: _buildTabBar() center: new Text(config.active?.title ?? 'Flutter Material gallery'),
), tabBar: _buildTabBar()
drawer: _buildDrawer(), ),
floatingActionButton: _buildFloatingActionButton(), drawer: _buildDrawer(),
body: _buildBody() floatingActionButton: _buildFloatingActionButton(),
body: _buildBody()
)
); );
} }
} }
...@@ -24,15 +24,9 @@ class StockHomeState extends State<StockHome> { ...@@ -24,15 +24,9 @@ class StockHomeState extends State<StockHome> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
bool _isSearching = false; bool _isSearching = false;
String _searchQuery; String _searchQuery;
TabBarSelection _tabBarSelection;
void initState() { void initState() {
super.initState(); super.initState();
_tabBarSelection = PageStorage.of(context)?.readState(context);
if (_tabBarSelection == null) {
_tabBarSelection = new TabBarSelection(maxIndex: 1);
PageStorage.of(context)?.writeState(context, _tabBarSelection);
}
} }
void _handleSearchBegin() { void _handleSearchBegin() {
...@@ -168,7 +162,6 @@ class StockHomeState extends State<StockHome> { ...@@ -168,7 +162,6 @@ class StockHomeState extends State<StockHome> {
) )
], ],
tabBar: new TabBar( tabBar: new TabBar(
selection: _tabBarSelection,
labels: <TabLabel>[ labels: <TabLabel>[
new TabLabel(text: StockStrings.of(context).market()), new TabLabel(text: StockStrings.of(context).market()),
new TabLabel(text: StockStrings.of(context).portfolio()) new TabLabel(text: StockStrings.of(context).portfolio())
...@@ -273,24 +266,26 @@ class StockHomeState extends State<StockHome> { ...@@ -273,24 +266,26 @@ class StockHomeState extends State<StockHome> {
} }
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( return new TabBarSelection(
key: _scaffoldKey, maxIndex: 1,
toolBar: _isSearching ? buildSearchBar() : buildToolBar(), child: new Scaffold(
floatingActionButton: buildFloatingActionButton(), key: _scaffoldKey,
drawer: _buildDrawer(context), toolBar: _isSearching ? buildSearchBar() : buildToolBar(),
body: new TabBarView<StockHomeTab>( floatingActionButton: buildFloatingActionButton(),
selection: _tabBarSelection, drawer: _buildDrawer(context),
items: <StockHomeTab>[StockHomeTab.market, StockHomeTab.portfolio], body: new TabBarView<StockHomeTab>(
itemBuilder: (BuildContext context, StockHomeTab tab, _) { items: <StockHomeTab>[StockHomeTab.market, StockHomeTab.portfolio],
switch (tab) { itemBuilder: (BuildContext context, StockHomeTab tab, _) {
case StockHomeTab.market: switch (tab) {
return _buildStockTab(context, tab, config.symbols); case StockHomeTab.market:
case StockHomeTab.portfolio: return _buildStockTab(context, tab, config.symbols);
return _buildStockTab(context, tab, portfolioSymbols); case StockHomeTab.portfolio:
default: return _buildStockTab(context, tab, portfolioSymbols);
assert(false); default:
assert(false);
}
} }
} )
) )
); );
} }
......
This diff is collapsed.
...@@ -8,7 +8,6 @@ import 'constants.dart'; ...@@ -8,7 +8,6 @@ import 'constants.dart';
import 'icon_theme.dart'; import 'icon_theme.dart';
import 'icon_theme_data.dart'; import 'icon_theme_data.dart';
import 'material.dart'; import 'material.dart';
import 'tabs.dart';
import 'theme.dart'; import 'theme.dart';
import 'typography.dart'; import 'typography.dart';
...@@ -30,7 +29,7 @@ class ToolBar extends StatelessComponent { ...@@ -30,7 +29,7 @@ class ToolBar extends StatelessComponent {
final Widget center; final Widget center;
final List<Widget> right; final List<Widget> right;
final Widget bottom; final Widget bottom;
final TabBar tabBar; final Widget tabBar;
final int elevation; final int elevation;
final Color backgroundColor; final Color backgroundColor;
final TextTheme textTheme; final TextTheme textTheme;
......
...@@ -7,14 +7,15 @@ import 'package:flutter/material.dart'; ...@@ -7,14 +7,15 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
TabBarSelection selection;
Widget buildFrame({ List<String> tabs, bool isScrollable: false }) { Widget buildFrame({ List<String> tabs, bool isScrollable: false }) {
return new Material( return new Material(
child: new TabBar( child: new TabBarSelection(
labels: tabs.map((String tab) => new TabLabel(text: tab)).toList(), index: 2,
selection: selection, maxIndex: tabs.length - 1,
isScrollable: isScrollable child: new TabBar(
labels: tabs.map((String tab) => new TabLabel(text: tab)).toList(),
isScrollable: isScrollable
)
) )
); );
} }
...@@ -23,9 +24,10 @@ void main() { ...@@ -23,9 +24,10 @@ void main() {
test('TabBar tap selects tab', () { test('TabBar tap selects tab', () {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
List<String> tabs = <String>['A', 'B', 'C']; List<String> tabs = <String>['A', 'B', 'C'];
selection = new TabBarSelection(index: 2, maxIndex: tabs.length - 1);
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: false)); tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: false));
TabBarSelectionState selection = tester.findStateOfType(TabBarSelectionState);
expect(selection, isNotNull);
expect(tester.findText('A'), isNotNull); expect(tester.findText('A'), isNotNull);
expect(tester.findText('B'), isNotNull); expect(tester.findText('B'), isNotNull);
expect(tester.findText('C'), isNotNull); expect(tester.findText('C'), isNotNull);
...@@ -51,9 +53,10 @@ void main() { ...@@ -51,9 +53,10 @@ void main() {
test('Scrollable TabBar tap selects tab', () { test('Scrollable TabBar tap selects tab', () {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
List<String> tabs = <String>['A', 'B', 'C']; List<String> tabs = <String>['A', 'B', 'C'];
selection = new TabBarSelection(index: 2, maxIndex: tabs.length - 1);
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: true)); tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: true));
TabBarSelectionState selection = tester.findStateOfType(TabBarSelectionState);
expect(selection, isNotNull);
expect(tester.findText('A'), isNotNull); expect(tester.findText('A'), isNotNull);
expect(tester.findText('B'), isNotNull); expect(tester.findText('B'), isNotNull);
expect(tester.findText('C'), isNotNull); expect(tester.findText('C'), isNotNull);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment