diff --git a/AUTHORS b/AUTHORS index 0e911ef23a..52081df62b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -56,4 +56,5 @@ Michael Lee Katarina Sheremet Nicolas Schneider Mikhail Zotyev +Maria Melnik Ayush Bherwani diff --git a/packages/flutter/lib/src/material/toggleable.dart b/packages/flutter/lib/src/material/toggleable.dart index fb69be46b7..17898dd8d4 100644 --- a/packages/flutter/lib/src/material/toggleable.dart +++ b/packages/flutter/lib/src/material/toggleable.dart @@ -391,6 +391,8 @@ abstract class RenderToggleable extends RenderConstrainedBox { void detach() { _positionController.stop(); _reactionController.stop(); + _reactionHoverFadeController.stop(); + _reactionFocusFadeController.stop(); super.detach(); } diff --git a/packages/flutter/test/material/checkbox_test.dart b/packages/flutter/test/material/checkbox_test.dart index 33b3f6389a..7377ab232a 100644 --- a/packages/flutter/test/material/checkbox_test.dart +++ b/packages/flutter/test/material/checkbox_test.dart @@ -603,6 +603,60 @@ void main() { expect(box.size, equals(const Size(60, 36))); }); + testWidgets('Checkbox stops hover animation when removed from the tree.', (WidgetTester tester) async { + const Key checkboxKey = Key('checkbox'); + bool checkboxVal = true; + + await tester.pumpWidget( + Theme( + data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded), + child: Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: Center( + child: StatefulBuilder( + builder: (_, StateSetter setState) => Checkbox( + key: checkboxKey, + value: checkboxVal, + onChanged: (bool newValue) => setState(() {checkboxVal = newValue;}), + ), + ), + ), + ), + ), + ), + ); + + expect(find.byKey(checkboxKey), findsOneWidget); + final Offset checkboxCenter = tester.getCenter(find.byKey(checkboxKey)); + final TestGesture testGesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await testGesture.moveTo(checkboxCenter); + + await tester.pump(); // start animation + await tester.pump(const Duration(milliseconds: 25)); // hover animation duration is 50 ms. It is half-way. + + await tester.pumpWidget( + Theme( + data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded), + child: Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: Center( + child: Container(), + ), + ), + ), + ), + ); + + // Hover animation should not trigger an exception when the checkbox is removed + // before the hover animation should complete. + expect(tester.takeException(), isNull); + + await testGesture.removePointer(); + }); + + testWidgets('Checkbox changes mouse cursor when hovered', (WidgetTester tester) async { // Test Checkbox() constructor await tester.pumpWidget(