From 566cacbd838aeb3948c2a3d1fae1619075454df2 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Fri, 29 Jul 2016 14:15:59 -0700 Subject: [PATCH] Don't leave tabs hanging in the middle (#5153) --- packages/flutter/lib/src/material/tabs.dart | 6 +- packages/flutter/test/material/tabs_test.dart | 83 ++++++++++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index c40516d399..50d7c8b618 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1217,8 +1217,10 @@ class _TabBarViewState extends PageableListState> implements Ta if (scrollVelocity.abs() > _kMinFlingVelocity) { final int selectionDelta = scrollVelocity.sign.truncate(); final int targetIndex = (_selection.index + selectionDelta).clamp(0, _tabCount - 1); - _selection.value = _selection.values[targetIndex]; - return new Future.value(); + if (_selection.index != targetIndex) { + _selection.value = _selection.values[targetIndex]; + return new Future.value(); + } } final int selectionIndex = _selection.index; diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 2aeb7fe69e..50219f739c 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -31,7 +31,6 @@ Widget buildFrame({ List tabs, String value, bool isScrollable: false, K child: new TabBarSelection( value: value, values: tabs, - onChanged: null, child: new TabBar( key: tabBarKey, labels: new Map.fromIterable(tabs, value: (String tab) => new TabLabel(text: tab)), @@ -41,6 +40,31 @@ Widget buildFrame({ List tabs, String value, bool isScrollable: false, K ); } + +Widget buildLeftRightApp({ List tabs, String value }) { + return new MaterialApp( + theme: new ThemeData(platform: TargetPlatform.android), + home: new TabBarSelection( + value: value, + values: tabs, + child: new Scaffold( + appBar: new AppBar( + title: new Text('tabs'), + bottom: new TabBar( + labels: new Map.fromIterable(tabs, value: (String tab) => new TabLabel(text: tab)), + ) + ), + body: new TabBarView( + children: [ + new Center(child: new Text('LEFT CHILD')), + new Center(child: new Text('RIGHT CHILD')) + ] + ) + ) + ) + ); +} + void main() { testWidgets('TabBar tap selects tab', (WidgetTester tester) async { List tabs = ['A', 'B', 'C']; @@ -228,4 +252,61 @@ void main() { await tester.pumpWidget(builder()); expect(findStateMarkerState(tabs[1]).marker, equals('marked')); }); + + testWidgets('TabBar left/right fling', (WidgetTester tester) async { + List tabs = ['LEFT', 'RIGHT']; + + await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT')); + expect(find.text('LEFT'), findsOneWidget); + expect(find.text('RIGHT'), findsOneWidget); + expect(find.text('LEFT CHILD'), findsOneWidget); + expect(find.text('RIGHT CHILD'), findsNothing); + + TabBarSelectionState selection = TabBarSelection.of(tester.element(find.text('LEFT'))); + expect(selection.value, equals('LEFT')); + + // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT' + Point flingStart = tester.getCenter(find.text('LEFT CHILD')); + await tester.flingFrom(flingStart, new Offset(-200.0, 0.0), 1000.0); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); // finish the scroll animation + expect(selection.value, equals('RIGHT')); + expect(find.text('LEFT CHILD'), findsNothing); + expect(find.text('RIGHT CHILD'), findsOneWidget); + + // Fling to the right, switch back to the 'LEFT' tab + flingStart = tester.getCenter(find.text('RIGHT CHILD')); + await tester.flingFrom(flingStart, new Offset(200.0, 0.0), 1000.0); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); // finish the scroll animation + expect(selection.value, equals('LEFT')); + expect(find.text('LEFT CHILD'), findsOneWidget); + expect(find.text('RIGHT CHILD'), findsNothing); + }); + + // A regression test for https://github.com/flutter/flutter/issues/5095 + testWidgets('TabBar left/right fling reverse', (WidgetTester tester) async { + List tabs = ['LEFT', 'RIGHT']; + + await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT')); + expect(find.text('LEFT'), findsOneWidget); + expect(find.text('RIGHT'), findsOneWidget); + expect(find.text('LEFT CHILD'), findsOneWidget); + expect(find.text('RIGHT CHILD'), findsNothing); + + TabBarSelectionState selection = TabBarSelection.of(tester.element(find.text('LEFT'))); + expect(selection.value, equals('LEFT')); + + // End the fling by reversing direction. This should cause not cause + // a change to the selected tab, everything should just settle back to + // to where it started. + Point flingStart = tester.getCenter(find.text('LEFT CHILD')); + await tester.flingFrom(flingStart, new Offset(-200.0, 0.0), -1000.0); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); // finish the scroll animation + expect(selection.value, equals('LEFT')); + expect(find.text('LEFT CHILD'), findsOneWidget); + expect(find.text('RIGHT CHILD'), findsNothing); + }); + }