Skip to content

Commit 2c1e4b1

Browse files
auto-submit[bot]auto-submit[bot]
andauthored
Reverts "Fix Tab linear and elastic animation blink (#162315)" (#162387)
<!-- start_original_pr_link --> Reverts: flutter/flutter#162315 <!-- end_original_pr_link --> <!-- start_initiating_author --> Initiated by: TahaTesser <!-- end_initiating_author --> <!-- start_revert_reason --> Reason for reverting: Red tree due to unapproved golden images. <!-- end_revert_reason --> <!-- start_original_pr_author --> Original PR Author: TahaTesser <!-- end_original_pr_author --> <!-- start_reviewers --> Reviewed By: {justinmc} <!-- end_reviewers --> <!-- start_revert_body --> This change reverts the following previous change: 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 <!-- end_revert_body --> Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
1 parent b007899 commit 2c1e4b1

2 files changed

Lines changed: 98 additions & 432 deletions

File tree

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

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,27 @@ 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());
589606

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

595612
assert(_currentRect != null);
@@ -611,17 +628,6 @@ class _IndicatorPainter extends CustomPainter {
611628
_painter!.paint(canvas, _currentRect!.topLeft, configuration);
612629
}
613630

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-
625631
// Ease out sine (decelerating).
626632
double decelerateInterpolation(double fraction) {
627633
return math.sin((fraction * math.pi) / 2.0);
@@ -633,38 +639,20 @@ class _IndicatorPainter extends CustomPainter {
633639
}
634640

635641
/// Applies the elastic effect to the indicator.
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-
642+
Rect _applyElasticEffect(Rect fromRect, Rect toRect, Rect currentRect) {
658643
// If the tab animation is completed, there is no need to stretch the indicator
659644
// This only works for the tab change animation via tab index, not when
660645
// dragging a [TabBarView], but it's still ok, to avoid unnecessary calculations.
661646
if (controller.animation!.isCompleted) {
662-
return rect;
647+
return currentRect;
663648
}
664649

650+
final double index = controller.index.toDouble();
651+
final double value = controller.animation!.value;
665652
final double tabChangeProgress;
666653

667654
if (controller.indexIsChanging) {
655+
double progressLeft = (index - value).abs();
668656
final int tabsDelta = (controller.index - controller.previousIndex).abs();
669657
if (tabsDelta != 0) {
670658
progressLeft /= tabsDelta;
@@ -676,7 +664,7 @@ class _IndicatorPainter extends CustomPainter {
676664

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

682670
final double leftFraction;
@@ -713,7 +701,7 @@ class _IndicatorPainter extends CustomPainter {
713701
};
714702
}
715703

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

719707
@override

0 commit comments

Comments
 (0)