From 0c24a4db532f71e9db4a6a2ae0931020ce6be81d Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Wed, 24 Aug 2022 18:42:45 +0300 Subject: [PATCH] Fix `floatingActionButtonAnimator` not being updated (#110067) --- .../flutter/lib/src/material/scaffold.dart | 8 +-- .../floating_action_button_location_test.dart | 69 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 23d753c899..524f10fd3a 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -1305,15 +1305,15 @@ class _FloatingActionButtonTransitionState extends State<_FloatingActionButtonTr @override void didUpdateWidget(_FloatingActionButtonTransition oldWidget) { super.didUpdateWidget(oldWidget); + if (oldWidget.fabMotionAnimator != widget.fabMotionAnimator || oldWidget.fabMoveAnimation != widget.fabMoveAnimation) { + // Get the right scale and rotation animations to use for this widget. + _updateAnimations(); + } final bool oldChildIsNull = oldWidget.child == null; final bool newChildIsNull = widget.child == null; if (oldChildIsNull == newChildIsNull && oldWidget.child?.key == widget.child?.key) { return; } - if (oldWidget.fabMotionAnimator != widget.fabMotionAnimator || oldWidget.fabMoveAnimation != widget.fabMoveAnimation) { - // Get the right scale and rotation animations to use for this widget. - _updateAnimations(); - } if (_previousController.status == AnimationStatus.dismissed) { final double currentValue = widget.currentController.value; if (currentValue == 0.0 || oldWidget.child == null) { diff --git a/packages/flutter/test/material/floating_action_button_location_test.dart b/packages/flutter/test/material/floating_action_button_location_test.dart index 87274e1d94..a0147b9bbf 100644 --- a/packages/flutter/test/material/floating_action_button_location_test.dart +++ b/packages/flutter/test/material/floating_action_button_location_test.dart @@ -607,6 +607,58 @@ void main() { expect(tester.binding.transientCallbackCount, 0); }); + + testWidgets('Animator can be updated', (WidgetTester tester) async { + FloatingActionButtonAnimator fabAnimator = FloatingActionButtonAnimator.scaling; + FloatingActionButtonLocation fabLocation = FloatingActionButtonLocation.startFloat; + + final Duration animationDuration = kFloatingActionButtonSegue * 2; + + await tester.pumpWidget(_singleFabScaffold( + fabLocation, + animator: fabAnimator, + )); + + expect(find.byType(FloatingActionButton), findsOneWidget); + expect(tester.binding.transientCallbackCount, 0); + expect(tester.getCenter(find.byType(FloatingActionButton)).dx, 44.0); + + fabLocation = FloatingActionButtonLocation.endFloat; + await tester.pumpWidget(_singleFabScaffold( + fabLocation, + animator: fabAnimator, + )); + + expect(tester.getTopLeft(find.byType(FloatingActionButton)).dx, lessThan(16.0)); + + await tester.pump(animationDuration * 0.25); + expect(tester.getTopLeft(find.byType(FloatingActionButton)).dx, greaterThan(16)); + + await tester.pump(animationDuration * 0.25); + expect(tester.getCenter(find.byType(FloatingActionButton)).dx, 756.0); + expect(tester.getTopRight(find.byType(FloatingActionButton)).dx, lessThan(800 - 16)); + + await tester.pump(animationDuration * 0.25); + expect(tester.getTopRight(find.byType(FloatingActionButton)).dx, lessThan(800 - 16)); + + await tester.pump(animationDuration * 0.25); + expect(tester.getTopRight(find.byType(FloatingActionButton)).dx, equals(800 - 16)); + + fabLocation = FloatingActionButtonLocation.startFloat; + fabAnimator = _NoScalingFabAnimator(); + await tester.pumpWidget(_singleFabScaffold( + fabLocation, + animator: fabAnimator, + )); + + await tester.pump(animationDuration * 0.25); + expect(tester.getCenter(find.byType(FloatingActionButton)).dx, 756.0); + expect(tester.getTopRight(find.byType(FloatingActionButton)).dx, equals(800 - 16)); + + await tester.pump(animationDuration * 0.25); + expect(tester.getCenter(find.byType(FloatingActionButton)).dx, 44.0); + expect(tester.getTopLeft(find.byType(FloatingActionButton)).dx, lessThan(16.0)); + }); }); group('Locations account for safe interactive areas', () { @@ -1717,3 +1769,20 @@ class _LinearMovementFabAnimator extends FloatingActionButtonAnimator { return const AlwaysStoppedAnimation(1.0); } } + +class _NoScalingFabAnimator extends FloatingActionButtonAnimator { + @override + Offset getOffset({required Offset begin, required Offset end, required double progress}) { + return progress < 0.5 ? begin : end; + } + + @override + Animation getScaleAnimation({required Animation parent}) { + return const AlwaysStoppedAnimation(1.0); + } + + @override + Animation getRotationAnimation({required Animation parent}) { + return const AlwaysStoppedAnimation(1.0); + } +}