Commit 6494cd1f authored by Hans Muller's avatar Hans Muller

Make TabBarSelection a parameterized type

parent 81b70675
...@@ -9,16 +9,15 @@ import 'widget_demo.dart'; ...@@ -9,16 +9,15 @@ 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"];
Widget _buildTabBarSelection(_, Widget child) { Widget _buildTabBarSelection(_, Widget child) {
return new TabBarSelection( return new TabBarSelection<String>(values: _iconNames, child: child);
maxIndex: _iconNames.length - 1,
child: child
);
} }
Widget _buildTabBar(_) { Widget _buildTabBar(_) {
return new TabBar( return new TabBar<String>(
isScrollable: true, isScrollable: true,
labels: _iconNames.map((String iconName) => new TabLabel(text: iconName, icon: "action/$iconName")).toList() labels: new Map.fromIterable(
_iconNames,
value: (String iconName) => new TabLabel(text: iconName, icon: "action/$iconName"))
); );
} }
......
...@@ -161,17 +161,15 @@ class StockHomeState extends State<StockHome> { ...@@ -161,17 +161,15 @@ class StockHomeState extends State<StockHome> {
onPressed: _handleMenuShow onPressed: _handleMenuShow
) )
], ],
tabBar: new TabBar( tabBar: new TabBar<StockHomeTab>(
labels: <TabLabel>[ labels: <StockHomeTab, TabLabel>{
new TabLabel(text: StockStrings.of(context).market()), StockHomeTab.market: new TabLabel(text: StockStrings.of(context).market()),
new TabLabel(text: StockStrings.of(context).portfolio()) StockHomeTab.portfolio: new TabLabel(text: StockStrings.of(context).portfolio())
] }
) )
); );
} }
int selectedTabIndex = 0;
Iterable<Stock> _getStockList(Iterable<String> symbols) { Iterable<Stock> _getStockList(Iterable<String> symbols) {
return symbols.map((String symbol) => config.stocks[symbol]) return symbols.map((String symbol) => config.stocks[symbol])
.where((Stock stock) => stock != null); .where((Stock stock) => stock != null);
...@@ -266,8 +264,8 @@ class StockHomeState extends State<StockHome> { ...@@ -266,8 +264,8 @@ class StockHomeState extends State<StockHome> {
} }
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new TabBarSelection( return new TabBarSelection<StockHomeTab>(
maxIndex: 1, values: <StockHomeTab>[StockHomeTab.market, StockHomeTab.portfolio],
child: new Scaffold( child: new Scaffold(
key: _scaffoldKey, key: _scaffoldKey,
toolBar: _isSearching ? buildSearchBar() : buildToolBar(), toolBar: _isSearching ? buildSearchBar() : buildToolBar(),
......
This diff is collapsed.
...@@ -7,13 +7,13 @@ import 'package:flutter/material.dart'; ...@@ -7,13 +7,13 @@ 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';
Widget buildFrame({ List<String> tabs, bool isScrollable: false }) { Widget buildFrame({ List<String> tabs, String value, bool isScrollable: false }) {
return new Material( return new Material(
child: new TabBarSelection( child: new TabBarSelection<String>(
index: 2, value: value,
maxIndex: tabs.length - 1, values: tabs,
child: new TabBar( child: new TabBar<String>(
labels: tabs.map((String tab) => new TabLabel(text: tab)).toList(), labels: new Map<String, TabLabel>.fromIterable(tabs, value: (String tab) => new TabLabel(text: tab)),
isScrollable: isScrollable isScrollable: isScrollable
) )
) )
...@@ -25,28 +25,48 @@ void main() { ...@@ -25,28 +25,48 @@ void main() {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
List<String> tabs = <String>['A', 'B', 'C']; List<String> tabs = <String>['A', 'B', 'C'];
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: false)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
TabBarSelectionState selection = tester.findStateOfType(TabBarSelectionState); TabBarSelectionState<String> selection = TabBarSelection.of(tester.findText('A'));
expect(selection, isNotNull); expect(selection, isNotNull);
expect(selection.indexOf('A'), equals(0));
expect(selection.indexOf('B'), equals(1));
expect(selection.indexOf('C'), equals(2));
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);
expect(selection.index, equals(2)); expect(selection.index, equals(2));
expect(selection.previousIndex, equals(2));
expect(selection.value, equals('C'));
expect(selection.previousValue, equals('C'));
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: false)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C' ,isScrollable: false));
tester.tap(tester.findText('B')); tester.tap(tester.findText('B'));
tester.pump(); tester.pump();
expect(selection.valueIsChanging, true);
tester.pump(const Duration(seconds: 1)); // finish the animation
expect(selection.valueIsChanging, false);
expect(selection.value, equals('B'));
expect(selection.previousValue, equals('C'));
expect(selection.index, equals(1)); expect(selection.index, equals(1));
expect(selection.previousIndex, equals(2));
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: false)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
tester.tap(tester.findText('C')); tester.tap(tester.findText('C'));
tester.pump(); tester.pump();
tester.pump(const Duration(seconds: 1));
expect(selection.value, equals('C'));
expect(selection.previousValue, equals('B'));
expect(selection.index, equals(2)); expect(selection.index, equals(2));
expect(selection.previousIndex, equals(1));
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: false)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
tester.tap(tester.findText('A')); tester.tap(tester.findText('A'));
tester.pump(); tester.pump();
tester.pump(const Duration(seconds: 1));
expect(selection.value, equals('A'));
expect(selection.previousValue, equals('C'));
expect(selection.index, equals(0)); expect(selection.index, equals(0));
expect(selection.previousIndex, equals(2));
}); });
}); });
...@@ -54,28 +74,28 @@ void main() { ...@@ -54,28 +74,28 @@ void main() {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
List<String> tabs = <String>['A', 'B', 'C']; List<String> tabs = <String>['A', 'B', 'C'];
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: true)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
TabBarSelectionState selection = tester.findStateOfType(TabBarSelectionState); TabBarSelectionState<String> selection = TabBarSelection.of(tester.findText('A'));
expect(selection, isNotNull); 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);
expect(selection.index, equals(2)); expect(selection.value, equals('C'));
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: true)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
tester.tap(tester.findText('B')); tester.tap(tester.findText('B'));
tester.pump(); tester.pump();
expect(selection.index, equals(1)); expect(selection.value, equals('B'));
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: true)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
tester.tap(tester.findText('C')); tester.tap(tester.findText('C'));
tester.pump(); tester.pump();
expect(selection.index, equals(2)); expect(selection.value, equals('C'));
tester.pumpWidget(buildFrame(tabs: tabs, isScrollable: true)); tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
tester.tap(tester.findText('A')); tester.tap(tester.findText('A'));
tester.pump(); tester.pump();
expect(selection.index, equals(0)); expect(selection.value, equals('A'));
}); });
}); });
} }
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