Skip to content

Commit b007899

Browse files
authored
Fix Tab linear and elastic animation blink (#162315)
Fixes [https://github.com/flutter/flutter/issues/162098](https://github.com/flutter/flutter/issues/162098) ### Description This PR fixes `Tab` linear and elastic animation blinks/flickers when skipping multiple tabs. Previous attempt to fix elastic animation didn't cover linear animation tests and didn't have enough number of tab items which this PR fixes. - Fixed Linear and elastic animation blink issue. - Added tests for linear and elastic animation with various tab sizes (LTR and RTL) - Added tests for linear and elastic animation when skipping tabs (LTR and RTL) ### Code Sample <details> <summary>expand to view the code sample</summary> ```dart import 'package:flutter/material.dart'; // import 'package:flutter/scheduler.dart'; void main() { // timeDilation = 10; runApp(const TabBarDemo()); } class TabBarDemo extends StatelessWidget { const TabBarDemo({super.key}); @OverRide Widget build(BuildContext context) { final List<Widget> tabs = <Widget>[ const Tab(text: 'Short'), const Tab(text: 'A Bit Longer Text'), const Tab(text: 'An Extremely Long Tab Label That Overflows'), const Tab(text: 'Tiny'), const Tab(text: 'Moderate Length'), const Tab(text: 'Just Right'), const Tab(text: 'Supercalifragilisticexpialidocious'), const Tab(text: 'Longer Than Usual'), ]; return MaterialApp( home: DefaultTabController( length: tabs.length, child: Scaffold( appBar: AppBar( bottom: TabBar( tabAlignment: TabAlignment.start, isScrollable: true, indicatorAnimation: TabIndicatorAnimation.elastic, tabs: tabs, ), title: const Text('Tabs Demo'), ), body: TabBarView( children: <Widget>[ for (int i = 0; i < tabs.length; i++) const Icon(Icons.directions_car), ], ), ), ), ); } } ``` </details> ### Before https://github.com/user-attachments/assets/5c271948-5a01-4520-90a3-921c20c79470 ### After https://github.com/user-attachments/assets/6af32d43-3588-488f-ba50-be59323ed692 ### Linear animation before (left) and After (right) comparison. <img width="1048" alt="Screenshot 2025-01-28 at 17 27 50" src="https://github.com/user-attachments/assets/4ba587a5-24d0-40ce-817c-366d004abc05" /> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 6d6ebee commit b007899

File tree

2 files changed

+432
-98
lines changed

2 files changed

+432
-98
lines changed

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -586,27 +586,10 @@ class _IndicatorPainter extends CustomPainter {
586586
_painter ??= indicator.createBoxPainter(markNeedsPaint);
587587

588588
final double value = controller.animation!.value;
589-
final int to =
590-
controller.indexIsChanging
591-
? controller.index
592-
: switch (textDirection) {
593-
TextDirection.ltr => value.ceil(),
594-
TextDirection.rtl => value.floor(),
595-
}.clamp(0, maxTabIndex);
596-
final int from =
597-
controller.indexIsChanging
598-
? controller.previousIndex
599-
: switch (textDirection) {
600-
TextDirection.ltr => (to - 1),
601-
TextDirection.rtl => (to + 1),
602-
}.clamp(0, maxTabIndex);
603-
final Rect toRect = indicatorRect(size, to);
604-
final Rect fromRect = indicatorRect(size, from);
605-
_currentRect = Rect.lerp(fromRect, toRect, (value - from).abs());
606589

607590
_currentRect = switch (indicatorAnimation) {
608-
TabIndicatorAnimation.linear => _currentRect,
609-
TabIndicatorAnimation.elastic => _applyElasticEffect(fromRect, toRect, _currentRect!),
591+
TabIndicatorAnimation.linear => _applyLinearEffect(size: size, value: value),
592+
TabIndicatorAnimation.elastic => _applyElasticEffect(size: size, value: value),
610593
};
611594

612595
assert(_currentRect != null);
@@ -628,6 +611,17 @@ class _IndicatorPainter extends CustomPainter {
628611
_painter!.paint(canvas, _currentRect!.topLeft, configuration);
629612
}
630613

614+
/// Applies the linear effect to the indicator.
615+
Rect? _applyLinearEffect({required Size size, required double value}) {
616+
final double index = controller.index.toDouble();
617+
final bool ltr = index > value;
618+
final int from = (ltr ? value.floor() : value.ceil()).clamp(0, maxTabIndex);
619+
final int to = (ltr ? from + 1 : from - 1).clamp(0, maxTabIndex);
620+
final Rect fromRect = indicatorRect(size, from);
621+
final Rect toRect = indicatorRect(size, to);
622+
return Rect.lerp(fromRect, toRect, (value - from).abs());
623+
}
624+
631625
// Ease out sine (decelerating).
632626
double decelerateInterpolation(double fraction) {
633627
return math.sin((fraction * math.pi) / 2.0);
@@ -639,20 +633,38 @@ class _IndicatorPainter extends CustomPainter {
639633
}
640634

641635
/// Applies the elastic effect to the indicator.
642-
Rect _applyElasticEffect(Rect fromRect, Rect toRect, Rect currentRect) {
636+
Rect? _applyElasticEffect({required Size size, required double value}) {
637+
final double index = controller.index.toDouble();
638+
double progressLeft = (index - value).abs();
639+
640+
final int to =
641+
progressLeft == 0.0 || !controller.indexIsChanging
642+
? switch (textDirection) {
643+
TextDirection.ltr => value.ceil(),
644+
TextDirection.rtl => value.floor(),
645+
}.clamp(0, maxTabIndex)
646+
: controller.index;
647+
final int from =
648+
progressLeft == 0.0 || !controller.indexIsChanging
649+
? switch (textDirection) {
650+
TextDirection.ltr => (to - 1),
651+
TextDirection.rtl => (to + 1),
652+
}.clamp(0, maxTabIndex)
653+
: controller.previousIndex;
654+
final Rect toRect = indicatorRect(size, to);
655+
final Rect fromRect = indicatorRect(size, from);
656+
final Rect rect = Rect.lerp(fromRect, toRect, (value - from).abs())!;
657+
643658
// If the tab animation is completed, there is no need to stretch the indicator
644659
// This only works for the tab change animation via tab index, not when
645660
// dragging a [TabBarView], but it's still ok, to avoid unnecessary calculations.
646661
if (controller.animation!.isCompleted) {
647-
return currentRect;
662+
return rect;
648663
}
649664

650-
final double index = controller.index.toDouble();
651-
final double value = controller.animation!.value;
652665
final double tabChangeProgress;
653666

654667
if (controller.indexIsChanging) {
655-
double progressLeft = (index - value).abs();
656668
final int tabsDelta = (controller.index - controller.previousIndex).abs();
657669
if (tabsDelta != 0) {
658670
progressLeft /= tabsDelta;
@@ -664,7 +676,7 @@ class _IndicatorPainter extends CustomPainter {
664676

665677
// If the animation has finished, there is no need to apply the stretch effect.
666678
if (tabChangeProgress == 1.0) {
667-
return currentRect;
679+
return rect;
668680
}
669681

670682
final double leftFraction;
@@ -701,7 +713,7 @@ class _IndicatorPainter extends CustomPainter {
701713
};
702714
}
703715

704-
return Rect.fromLTRB(lerpRectLeft, currentRect.top, lerpRectRight, currentRect.bottom);
716+
return Rect.fromLTRB(lerpRectLeft, rect.top, lerpRectRight, rect.bottom);
705717
}
706718

707719
@override

0 commit comments

Comments
 (0)