diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index 47791c416e..0fed2b4784 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -275,6 +275,7 @@ class RefreshIndicatorState extends State with TickerProviderS late Future _pendingRefreshFuture; bool? _isIndicatorAtTop; double? _dragOffset; + late Color _effectiveValueColor = widget.color ?? Theme.of(context).colorScheme.primary; static final Animatable _threeQuarterTween = Tween(begin: 0.0, end: 0.75); static final Animatable _kDragSizeFactorLimitTween = Tween(begin: 0.0, end: _kDragSizeFactorLimit); @@ -293,15 +294,7 @@ class RefreshIndicatorState extends State with TickerProviderS @override void didChangeDependencies() { - final ThemeData theme = Theme.of(context); - _valueColor = _positionController.drive( - ColorTween( - begin: (widget.color ?? theme.colorScheme.primary).withOpacity(0.0), - end: (widget.color ?? theme.colorScheme.primary).withOpacity(1.0), - ).chain(CurveTween( - curve: const Interval(0.0, 1.0 / _kDragSizeFactorLimit), - )), - ); + _setupColorTween(); super.didChangeDependencies(); } @@ -309,15 +302,7 @@ class RefreshIndicatorState extends State with TickerProviderS void didUpdateWidget(covariant RefreshIndicator oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.color != widget.color) { - final ThemeData theme = Theme.of(context); - _valueColor = _positionController.drive( - ColorTween( - begin: (widget.color ?? theme.colorScheme.primary).withOpacity(0.0), - end: (widget.color ?? theme.colorScheme.primary).withOpacity(1.0), - ).chain(CurveTween( - curve: const Interval(0.0, 1.0 / _kDragSizeFactorLimit), - )), - ); + _setupColorTween(); } } @@ -328,6 +313,28 @@ class RefreshIndicatorState extends State with TickerProviderS super.dispose(); } + void _setupColorTween() { + // Reset the current value color. + _effectiveValueColor = widget.color ?? Theme.of(context).colorScheme.primary; + final Color color = _effectiveValueColor; + if (color.alpha == 0x00) { + // Set an always stopped animation instead of a driven tween. + _valueColor = AlwaysStoppedAnimation(color); + } else { + // Respect the alpha of the given color. + _valueColor = _positionController.drive( + ColorTween( + begin: color.withAlpha(0), + end: color.withAlpha(color.alpha), + ).chain( + CurveTween( + curve: const Interval(0.0, 1.0 / _kDragSizeFactorLimit), + ), + ), + ); + } + } + bool _shouldStart(ScrollNotification notification) { // If the notification.dragDetails is null, this scroll is not triggered by // user dragging. It may be a result of ScrollController.jumpTo or ballistic scroll. @@ -448,7 +455,7 @@ class RefreshIndicatorState extends State with TickerProviderS newValue = math.max(newValue, 1.0 / _kDragSizeFactorLimit); } _positionController.value = clampDouble(newValue, 0.0, 1.0); // this triggers various rebuilds - if (_mode == _RefreshIndicatorMode.drag && _valueColor.value!.alpha == 0xFF) { + if (_mode == _RefreshIndicatorMode.drag && _valueColor.value!.alpha == _effectiveValueColor.alpha) { _mode = _RefreshIndicatorMode.armed; } } diff --git a/packages/flutter/test/material/refresh_indicator_test.dart b/packages/flutter/test/material/refresh_indicator_test.dart index ede579db2e..e3c1bd8933 100644 --- a/packages/flutter/test/material/refresh_indicator_test.dart +++ b/packages/flutter/test/material/refresh_indicator_test.dart @@ -1070,4 +1070,67 @@ void main() { expect(refreshCalled, true); expect(stretchAccepted, false); }); + + testWidgetsWithLeakTracking('RefreshIndicator manipulates value color opacity correctly', (WidgetTester tester) async { + final List colors = [ + Colors.black, + Colors.black54, + Colors.white, + Colors.white54, + Colors.transparent, + ]; + const List positions = [50.0, 100.0, 150.0]; + + Future testColor(Color color) async { + final AnimationController positionController = AnimationController(vsync: const TestVSync()); + // Correspond to [_setupColorTween]. + final Animation valueColorAnimation = positionController.drive( + ColorTween( + begin: color.withAlpha(0), + end: color.withAlpha(color.alpha), + ).chain( + CurveTween( + // Correspond to [_kDragSizeFactorLimit]. + curve: const Interval(0.0, 1.0 / 1.5), + ), + ), + ); + await tester.pumpWidget( + MaterialApp( + home: RefreshIndicator( + onRefresh: refresh, + color: color, + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: const [Text('X')], + ), + ), + ), + ); + + RefreshProgressIndicator getIndicator() { + return tester.widget( + find.byType(RefreshProgressIndicator), + ); + } + + // Correspond to [_kDragContainerExtentPercentage]. + final double maxPosition = tester.view.physicalSize.height / tester.view.devicePixelRatio * 0.25; + for (final double position in positions) { + await tester.fling(find.text('X'), Offset(0.0, position), 1.0); + await tester.pump(); + positionController.value = position / maxPosition; + expect( + getIndicator().valueColor!.value!.alpha, + valueColorAnimation.value!.alpha, + ); + // Wait until the fling finishes before starting the next fling. + await tester.pumpAndSettle(); + } + } + + for (final Color color in colors) { + await testColor(color); + } + }); }