diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 50ec52a285..809eb49d80 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -459,7 +459,7 @@ class _IndicatorPainter extends CustomPainter { final TabController controller; final Decoration indicator; - final TabBarIndicatorSize? indicatorSize; + final TabBarIndicatorSize indicatorSize; final EdgeInsetsGeometry indicatorPadding; final List tabKeys; final List labelPaddings; @@ -554,6 +554,13 @@ class _IndicatorPainter extends CustomPainter { final Rect fromRect = indicatorRect(size, from); final Rect toRect = indicatorRect(size, to); _currentRect = Rect.lerp(fromRect, toRect, (value - from).abs()); + + _currentRect = switch (indicatorSize) { + TabBarIndicatorSize.label => _applyStretchEffect(_currentRect!), + // Do nothing. + TabBarIndicatorSize.tab => _currentRect, + }; + assert(_currentRect != null); final ImageConfiguration configuration = ImageConfiguration( @@ -569,6 +576,70 @@ class _IndicatorPainter extends CustomPainter { _painter!.paint(canvas, _currentRect!.topLeft, configuration); } + /// Applies the stretch effect to the indicator. + Rect _applyStretchEffect(Rect rect) { + // If the tab animation is completed, there is no need to stretch the indicator + // This only works for the tab change animation via tab index, not when + // dragging a [TabBarView], but it's still ok, to avoid unnecessary calculations. + if (controller.animation!.status == AnimationStatus.completed) { + return rect; + } + + final double index = controller.index.toDouble(); + final double value = controller.animation!.value; + + // The progress of the animation from 0 to 1. + late double tabChangeProgress; + + // If we are changing tabs via index, we want to map the progress between 0 and 1. + if (controller.indexIsChanging) { + double progressLeft = (index - value).abs(); + final int tabsDelta = (controller.index - controller.previousIndex).abs(); + if (tabsDelta != 0) { + progressLeft /= tabsDelta; + } + tabChangeProgress = 1 - clampDouble(progressLeft, 0.0, 1.0); + } else { + // Otherwise, the progress is how close we are to the current tab. + tabChangeProgress = (index - value).abs(); + } + + // If the animation has finished, there is no need to apply the stretch effect. + if (tabChangeProgress == 1.0) { + return rect; + } + + // The maximum amount of extra width to add to the indicator. + final double stretchSize = rect.width; + + final double inflationPerSide = stretchSize * _stretchAnimation.transform(tabChangeProgress) / 2; + final Rect stretchedRect = _inflateRectHorizontally(rect, inflationPerSide); + return stretchedRect; + } + + /// The animatable that stretches the indicator horizontally when changing tabs. + /// Value range is from 0 to 1, so we can multiply it by an stretch factor. + /// + /// Animation starts with no stretch, then quickly goes to the max stretch amount + /// and then goes back to no stretch. + late final Animatable _stretchAnimation = TweenSequence( + >[ + TweenSequenceItem( + tween: Tween(begin: 0.0, end: 1.0), + weight: 20, + ), + TweenSequenceItem( + tween: Tween(begin: 1.0, end: 0.0), + weight: 80, + ), + ], + ); + + /// Same as [Rect.inflate], but only inflates in the horizontal direction. + Rect _inflateRectHorizontally(Rect r, double delta) { + return Rect.fromLTRB(r.left - delta, r.top, r.right + delta, r.bottom); + } + @override bool shouldRepaint(_IndicatorPainter old) { return _needsPaint @@ -1338,7 +1409,7 @@ class _TabBarState extends State { _indicatorPainter = !_controllerIsValid ? null : _IndicatorPainter( controller: _controller!, indicator: _getIndicator(indicatorSize), - indicatorSize: widget.indicatorSize ?? tabBarTheme.indicatorSize ?? _defaults.indicatorSize!, + indicatorSize: indicatorSize, indicatorPadding: widget.indicatorPadding, tabKeys: _tabKeys, old: _indicatorPainter, diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 9ae0c2676d..8cee1e5bfc 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -2557,6 +2557,66 @@ void main() { expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB #19')).dx, moreOrLessEquals(tabRight)); }); + testWidgets('Material3 - Indicator stretch animation', (WidgetTester tester) async { + const double indicatorWidth = 50.0; + final List tabs = List.generate(4, (int index) { + return Tab( + key: ValueKey(index), + child: const SizedBox(width: indicatorWidth)); + }); + + final TabController controller = _tabController( + vsync: const TestVSync(), + length: tabs.length, + ); + + await tester.pumpWidget( + MaterialApp( + home: boilerplate( + child: Container( + alignment: Alignment.topLeft, + child: TabBar( + controller: controller, + tabs: tabs, + ), + ), + ), + ), + ); + + final RenderBox tabBarBox = tester.firstRenderObject(find.byType(TabBar)); + expect(tabBarBox.size.height, 48.0); + final double tabWidth = tabBarBox.size.width / tabs.length; + + void expectIndicatorAttrs(RenderBox tabBarBox, {required double offset, required double width}) { + const double indicatorWeight = 3.0; + final double centerX = offset * tabWidth + tabWidth / 2; + final RRect rrect = RRect.fromLTRBAndCorners( + centerX - width / 2, + tabBarBox.size.height - indicatorWeight, + centerX + width / 2, + tabBarBox.size.height, + topLeft: const Radius.circular(3.0), + topRight: const Radius.circular(3.0), + ); + + expect(tabBarBox, paints..rrect(rrect: rrect)); + } + + // Idle at tab 0. + expectIndicatorAttrs(tabBarBox, offset: 0.0, width: indicatorWidth); + + // Peak stretch at 20%. + controller.offset = 0.2; + await tester.pump(); + expectIndicatorAttrs(tabBarBox, offset: 0.2, width: indicatorWidth * 2); + + // Idle at tab 1. + controller.offset = 1; + await tester.pump(); + expectIndicatorAttrs(tabBarBox, offset: 1, width: indicatorWidth); + }); + testWidgets('TabBar with indicatorWeight, indicatorPadding (LTR)', (WidgetTester tester) async { const Color indicatorColor = Color(0xFF00FF00); const double indicatorWeight = 8.0;