From 4906297c28b4da83d7ab2c8397e56fc6759267eb Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 18 Apr 2017 13:11:35 -0700 Subject: [PATCH] Scrollable TabBar shows initialIndex (#9402) --- packages/flutter/lib/src/material/tabs.dart | 70 +++++++++++++++++-- packages/flutter/test/material/tabs_test.dart | 27 +++++++ 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index c8b0e82f16..629175276d 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -335,6 +335,54 @@ class _DragAnimation extends Animation with AnimationWithParentMixin { - final ScrollController _scrollController = new ScrollController(); + ScrollController _scrollController; TabController _controller; _IndicatorPainter _indicatorPainter; @@ -504,14 +552,22 @@ class _TabBarState extends State { // tabOffsets[tabOffsets.length] is the right edge of the last tab. int get maxTabIndex => _indicatorPainter.tabOffsets.length - 2; - double _tabCenteredScrollOffset(int tabIndex) { + double _tabScrollOffset(int index, double viewportWidth, double minExtent, double maxExtent) { + if (!widget.isScrollable) + return 0.0; final List tabOffsets = _indicatorPainter.tabOffsets; - assert(tabOffsets != null && tabIndex >= 0 && tabIndex <= maxTabIndex); + assert(tabOffsets != null && index >= 0 && index <= maxTabIndex); + final double tabCenter = (tabOffsets[index] + tabOffsets[index + 1]) / 2.0; + return (tabCenter - viewportWidth / 2.0).clamp(minExtent, maxExtent); + } + double _tabCenteredScrollOffset(int index) { final ScrollPosition position = _scrollController.position; - final double tabCenter = (tabOffsets[tabIndex] + tabOffsets[tabIndex + 1]) / 2.0; - return (tabCenter - position.viewportDimension / 2.0) - .clamp(position.minScrollExtent, position.maxScrollExtent); + return _tabScrollOffset(index, position.viewportDimension, position.minScrollExtent, position.maxScrollExtent); + } + + double _initialScrollOffset(double viewportWidth, double minExtent, double maxExtent) { + return _tabScrollOffset(_currentIndex, viewportWidth, minExtent, maxExtent); } void _scrollToCurrentIndex() { @@ -557,6 +613,7 @@ class _TabBarState extends State { }); } + // Called each time layout completes. void _saveTabOffsets(List tabOffsets) { _indicatorPainter?.tabOffsets = tabOffsets; } @@ -662,6 +719,7 @@ class _TabBarState extends State { ); if (widget.isScrollable) { + _scrollController ??= new _TabBarScrollController(this); tabBar = new SingleChildScrollView( scrollDirection: Axis.horizontal, controller: _scrollController, diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 61e927c5e4..d09ce967c5 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -806,6 +806,33 @@ void main() { // Close enough to switch to page 2 pageController.jumpTo(800.0 - 0.75 * position.physics.tolerance.distance); expect(tabController.index, 2); + }); + testWidgets('Scrollable TabBar with a non-zero TabController initialIndex', (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/9374 + + final List tabs = new List.generate(20, (int index) { + return new Tab(text: 'TAB #$index'); + }); + + final TabController controller = new TabController( + vsync: const TestVSync(), + length: tabs.length, + initialIndex: tabs.length - 1, + ); + + await tester.pumpWidget( + new Material( + child: new TabBar( + isScrollable: true, + controller: controller, + tabs: tabs, + ), + ), + ); + + // The initialIndex tab should be visible and right justified + expect(find.text('TAB #19'), findsOneWidget); + expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB #19')).dx, 800.0); }); }