Skip to content

Commit db8400a

Browse files
committed
Make tab highlighting work properly only with _handleTabControllerIndexChangeStarted
1 parent 986b482 commit db8400a

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

packages/flutter/lib/src/material/tabs.dart

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -453,32 +453,8 @@ class _IndicatorPainter extends CustomPainter {
453453
}
454454
}
455455

456-
class _ChangeAnimation extends Animation<double> with AnimationWithParentMixin<double> {
457-
_ChangeAnimation(this.controller);
458-
459-
final TabController controller;
460-
461-
@override
462-
Animation<double> get parent => controller.animation;
463-
464-
@override
465-
void removeStatusListener(AnimationStatusListener listener) {
466-
if (parent != null)
467-
super.removeStatusListener(listener);
468-
}
469-
470-
@override
471-
void removeListener(VoidCallback listener) {
472-
if (parent != null)
473-
super.removeListener(listener);
474-
}
475-
476-
@override
477-
double get value => _indexChangeProgress(controller);
478-
}
479-
480-
class _DragAnimation extends Animation<double> with AnimationWithParentMixin<double> {
481-
_DragAnimation(this.controller, this.index);
456+
class _TabChangeAnimation extends Animation<double> with AnimationWithParentMixin<double> {
457+
_TabChangeAnimation(this.controller, this.index);
482458

483459
final TabController controller;
484460
final int index;
@@ -500,7 +476,6 @@ class _DragAnimation extends Animation<double> with AnimationWithParentMixin<dou
500476

501477
@override
502478
double get value {
503-
assert(!controller.indexIsChanging);
504479
return (controller.animation.value - index.toDouble()).abs().clamp(0.0, 1.0) as double;
505480
}
506481
}
@@ -860,13 +835,11 @@ class _TabBarState extends State<TabBar> {
860835

861836
if (_controllerIsValid) {
862837
_controller.animation.removeListener(_handleTabControllerAnimationTick);
863-
_controller.removeListener(_handleTabControllerTick);
864838
_controller.removeIndexChangeStartedListener(_handleTabControllerIndexChangeStarted);
865839
}
866840
_controller = newController;
867841
if (_controller != null) {
868842
_controller.animation.addListener(_handleTabControllerAnimationTick);
869-
_controller.addListener(_handleTabControllerTick);
870843
_controller.addIndexChangeStartedListener(_handleTabControllerIndexChangeStarted);
871844
_currentIndex = _controller.index;
872845
}
@@ -916,7 +889,6 @@ class _TabBarState extends State<TabBar> {
916889
_indicatorPainter.dispose();
917890
if (_controllerIsValid) {
918891
_controller.animation.removeListener(_handleTabControllerAnimationTick);
919-
_controller.removeListener(_handleTabControllerTick);
920892
_controller.removeIndexChangeStartedListener(_handleTabControllerIndexChangeStarted);
921893
}
922894
_controller = null;
@@ -985,16 +957,11 @@ class _TabBarState extends State<TabBar> {
985957
}
986958
}
987959

988-
void _handleTabControllerTick() {
989-
setState(() {
990-
// Rebuild the tabs after a (potentially animated) index change
991-
// has completed.
992-
});
993-
}
994-
995960
void _handleTabControllerIndexChangeStarted(int index, Duration duration, Curve curve) {
996961
if (index != _currentIndex) {
997-
_currentIndex = index;
962+
setState(() {
963+
_currentIndex = index;
964+
});
998965
if (widget.isScrollable)
999966
_scrollToCurrentIndex(duration, curve);
1000967
}
@@ -1070,28 +1037,24 @@ class _TabBarState extends State<TabBar> {
10701037
// controller during a Hero transition. See https://github.com/flutter/flutter/issues/213.
10711038
if (_controller != null) {
10721039
final int previousIndex = _controller.previousIndex;
1073-
1074-
if (_controller.indexIsChanging) {
1075-
// The user tapped on a tab, the tab controller's animation is running.
1076-
assert(_currentIndex != previousIndex);
1077-
final Animation<double> animation = _ChangeAnimation(_controller);
1078-
wrappedTabs[_currentIndex] = _buildStyledTab(wrappedTabs[_currentIndex], true, animation);
1040+
// Need to check range because tabs size could have been changed.
1041+
if (previousIndex >= 0 && previousIndex < widget.tabs.length) {
1042+
final Animation<double> animation = ReverseAnimation(_TabChangeAnimation(_controller, previousIndex));
10791043
wrappedTabs[previousIndex] = _buildStyledTab(wrappedTabs[previousIndex], false, animation);
1080-
} else {
1081-
// The user is dragging the TabBarView's PageView left or right.
1082-
final int tabIndex = _currentIndex;
1083-
final Animation<double> centerAnimation = _DragAnimation(_controller, tabIndex);
1084-
wrappedTabs[tabIndex] = _buildStyledTab(wrappedTabs[tabIndex], true, centerAnimation);
1085-
if (_currentIndex > 0) {
1086-
final int tabIndex = _currentIndex - 1;
1087-
final Animation<double> previousAnimation = ReverseAnimation(_DragAnimation(_controller, tabIndex));
1088-
wrappedTabs[tabIndex] = _buildStyledTab(wrappedTabs[tabIndex], false, previousAnimation);
1089-
}
1090-
if (_currentIndex < widget.tabs.length - 1) {
1091-
final int tabIndex = _currentIndex + 1;
1092-
final Animation<double> nextAnimation = ReverseAnimation(_DragAnimation(_controller, tabIndex));
1093-
wrappedTabs[tabIndex] = _buildStyledTab(wrappedTabs[tabIndex], false, nextAnimation);
1094-
}
1044+
}
1045+
1046+
final int tabIndex = _currentIndex;
1047+
final Animation<double> centerAnimation = _TabChangeAnimation(_controller, tabIndex);
1048+
wrappedTabs[tabIndex] = _buildStyledTab(wrappedTabs[tabIndex], true, centerAnimation);
1049+
if (_currentIndex > 0) {
1050+
final int tabIndex = _currentIndex - 1;
1051+
final Animation<double> previousAnimation = ReverseAnimation(_TabChangeAnimation(_controller, tabIndex));
1052+
wrappedTabs[tabIndex] = _buildStyledTab(wrappedTabs[tabIndex], false, previousAnimation);
1053+
}
1054+
if (_currentIndex < widget.tabs.length - 1) {
1055+
final int tabIndex = _currentIndex + 1;
1056+
final Animation<double> nextAnimation = ReverseAnimation(_TabChangeAnimation(_controller, tabIndex));
1057+
wrappedTabs[tabIndex] = _buildStyledTab(wrappedTabs[tabIndex], false, nextAnimation);
10951058
}
10961059
}
10971060

0 commit comments

Comments
 (0)