From 68efa6a3fc17b3c6b503f928c3208e885b5ddb87 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Thu, 23 Jun 2016 15:17:17 -0700 Subject: [PATCH] Correct scrolling tabbar tab indicator size (#4723) --- packages/flutter/lib/src/material/tabs.dart | 37 ++++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 81a24490a6..2cfdf442bc 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -683,6 +683,22 @@ class TabBarSelectionState extends State> { } } +// Used when the user is dragging the TabBar or the TabBarView left or right. +// Dragging from the selected tab to the left varies t between 0.5 and 0.0. +// Dragging towards the tab on the right varies t between 0.5 and 1.0. +class _TabIndicatorTween extends Tween { + _TabIndicatorTween({ Rect begin, this.middle, Rect end }) : super(begin: begin, end: end); + + final Rect middle; + + @override + Rect lerp(double t) { + return t <= 0.5 + ? Rect.lerp(begin, middle, t * 2.0) + : Rect.lerp(middle, end, (t - 0.5) * 2.0); + } +} + /// A widget that displays a horizontal row of tabs, one per label. /// /// Requires one of its ancestors to be a [TabBarSelection] widget to enable @@ -789,10 +805,21 @@ class _TabBarState extends ScrollableState> implements TabBarSelect // when it's under the tab on the right. void _initIndicatorTweenForDrag() { assert(!_valueIsChanging); - _indicatorTween = new RectTween( - begin: _tabIndicatorRect(math.max(0, _selection.index - 1)), - end: _tabIndicatorRect(math.min(config.labels.length - 1, _selection.index + 1)) - ); + int index = _selection.index; + int beginIndex = math.max(0, index - 1); + int endIndex = math.min(config.labels.length - 1, index + 1); + if (beginIndex == index || endIndex == index) { + _indicatorTween = new RectTween( + begin: _tabIndicatorRect(beginIndex), + end: _tabIndicatorRect(endIndex) + ); + } else { + _indicatorTween = new _TabIndicatorTween( + begin: _tabIndicatorRect(beginIndex), + middle: _tabIndicatorRect(index), + end: _tabIndicatorRect(endIndex) + ); + } } // Initialize _indicatorTween for animating the selected tab indicator from the @@ -869,7 +896,7 @@ class _TabBarState extends ScrollableState> implements TabBarSelect Size _tabBarSize; List _tabWidths; Rect _indicatorRect; - RectTween _indicatorTween; + Tween _indicatorTween; Rect _tabRect(int tabIndex) { assert(_tabBarSize != null);