From e1f0083c7840a3fe7751c62db958455fe9f6566e Mon Sep 17 00:00:00 2001 From: Nate Wilson Date: Wed, 28 Aug 2024 20:23:19 -0600 Subject: [PATCH] Fix `Drawer` prematurely snapping (#153955) This regression is due to the fact that `animationController.isAnimating` and `animationController.status.isAnimating` sometimes return opposite values. This PR implements & tests the bugfix, and also adds documentation to explain this behavior.
fixes https://github.com/flutter/flutter/issues/153851 cc @azeunkn0wn @huycozy --- .../flutter/lib/src/animation/animation.dart | 4 ++ .../src/animation/animation_controller.dart | 4 ++ packages/flutter/lib/src/material/drawer.dart | 6 +- .../flutter/test/material/drawer_test.dart | 55 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/animation/animation.dart b/packages/flutter/lib/src/animation/animation.dart index e947190f0d..e7ac6d5787 100644 --- a/packages/flutter/lib/src/animation/animation.dart +++ b/packages/flutter/lib/src/animation/animation.dart @@ -243,6 +243,10 @@ abstract class Animation extends Listenable implements ValueListenable { bool get isCompleted => status.isCompleted; /// Whether this animation is running in either direction. + /// + /// By default, this value is equal to `status.isAnimating`, but + /// [AnimationController] overrides this method so that its output + /// depends on whether the controller is actively ticking. bool get isAnimating => status.isAnimating; /// {@macro flutter.animation.AnimationStatus.isForwardOrCompleted} diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index c5b44d2d54..c946b447d6 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -448,6 +448,10 @@ class AnimationController extends Animation /// controller's ticker might get muted, in which case the animation /// controller's callbacks will no longer fire even though time is continuing /// to pass. See [Ticker.muted] and [TickerMode]. + /// + /// If the animation was stopped (e.g. with [stop] or by setting a new [value]), + /// [isAnimating] will return `false` but the [status] will not change, + /// so the value of [AnimationStatus.isAnimating] might still be `true`. @override bool get isAnimating => _ticker != null && _ticker!.isActive; diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index 4d3922530d..0013821c4e 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -501,7 +501,11 @@ class DrawerControllerState extends State with SingleTickerPro if (widget.scrimColor != oldWidget.scrimColor) { _scrimColorTween = _buildScrimColorTween(); } - if (widget.isDrawerOpen != oldWidget.isDrawerOpen && !_controller.isAnimating) { + + if (_controller.status.isAnimating) { + return; // Don't snap the drawer open or shut while the user is dragging. + } + if (widget.isDrawerOpen != oldWidget.isDrawerOpen) { _controller.value = widget.isDrawerOpen ? 1.0 : 0.0; } } diff --git a/packages/flutter/test/material/drawer_test.dart b/packages/flutter/test/material/drawer_test.dart index 1562be4b65..ad8c096aa7 100644 --- a/packages/flutter/test/material/drawer_test.dart +++ b/packages/flutter/test/material/drawer_test.dart @@ -259,6 +259,61 @@ void main() { expect(state.isEndDrawerOpen, equals(false)); }); + testWidgets('Open/close drawer by dragging', (WidgetTester tester) async { + final ThemeData draggable = ThemeData(platform: TargetPlatform.android); + await tester.pumpWidget( + MaterialApp( + theme: draggable, + home: const Scaffold(drawer: Drawer()), + ), + ); + + final TestGesture gesture = await tester.createGesture(); + final Finder finder = find.byType(Drawer); + + double drawerPosition() { + expect(finder, findsOneWidget); + final RenderBox renderBox = tester.renderObject(finder); + return renderBox.localToGlobal(Offset.zero).dx; + } + + // Pointer down (drawer is closed). + await gesture.addPointer(); + await gesture.down(const Offset(2,2)); + await tester.pump(); + expect(finder, findsNothing); + + // Open drawer slightly. + await gesture.moveBy(const Offset(20, 0)); + await tester.pump(); + expect(drawerPosition(), isNegative); + + // Open drawer more than halfway. + await gesture.moveBy(const Offset(200, 0)); + await tester.pump(); + expect(drawerPosition(), isNegative); + + // Drawer is fully open. + await gesture.moveBy(const Offset(200, 0)); + await tester.pump(); + expect(drawerPosition(), 0.0); + + // Drawer is less than halfway closed. + await gesture.moveBy(const Offset(-100.0, 0)); + await tester.pump(); + expect(drawerPosition(), moreOrLessEquals(-100.0)); + + // Drawer is more than halfway closed. + await gesture.moveBy(const Offset(-100.0, 0)); + await tester.pump(); + expect(drawerPosition(), moreOrLessEquals(-200.0)); + + // Drawer is completely closed. + await gesture.moveTo(Offset.zero); + await tester.pump(); + expect(finder, findsNothing); + }); + testWidgets('Scaffold.drawer - null restorationId ', (WidgetTester tester) async { final GlobalKey scaffoldKey = GlobalKey(); await tester.pumpWidget(