From b7534760049f6100289303eaba6f93d46d4729b2 Mon Sep 17 00:00:00 2001 From: hangyu Date: Tue, 12 Jul 2022 23:06:21 +0800 Subject: [PATCH] Add a snapAnimationDuration param in DraggableScrollableSheet (#107396) * Add a snapAnimationDuration param in DraggableScrollableSheet * snapAnimationDuration.inMilliseconds > 0 * Update draggable_scrollable_sheet.dart --- .../widgets/draggable_scrollable_sheet.dart | 22 +++- .../draggable_scrollable_sheet_test.dart | 102 ++++++++++-------- 2 files changed, 76 insertions(+), 48 deletions(-) diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index a721548dc6..9eac1f3dd4 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -289,6 +289,7 @@ class DraggableScrollableSheet extends StatefulWidget { this.expand = true, this.snap = false, this.snapSizes, + this.snapAnimationDuration, this.controller, required this.builder, }) : assert(initialChildSize != null), @@ -298,6 +299,7 @@ class DraggableScrollableSheet extends StatefulWidget { assert(maxChildSize <= 1.0), assert(minChildSize <= initialChildSize), assert(initialChildSize <= maxChildSize), + assert(snapAnimationDuration == null || snapAnimationDuration > Duration.zero), assert(expand != null), assert(builder != null); @@ -365,6 +367,12 @@ class DraggableScrollableSheet extends StatefulWidget { /// being built or since the last call to [DraggableScrollableActuator.reset]. final List? snapSizes; + /// Defines a duration for the snap animations. + /// + /// If it's not set, then the animation duration is the distance to the snap + /// target divided by the velocity of the widget. + final Duration? snapAnimationDuration; + /// A controller that can be used to programmatically control this sheet. final DraggableScrollableController? controller; @@ -466,6 +474,7 @@ class _DraggableSheetExtent { required this.snapSizes, required this.initialSize, required this.onSizeChanged, + this.snapAnimationDuration, ValueNotifier? currentSize, bool? hasDragged, }) : assert(minSize != null), @@ -486,6 +495,7 @@ class _DraggableSheetExtent { final double maxSize; final bool snap; final List snapSizes; + final Duration? snapAnimationDuration; final double initialSize; final ValueNotifier _currentSize; final VoidCallback onSizeChanged; @@ -570,12 +580,14 @@ class _DraggableSheetExtent { required List snapSizes, required double initialSize, required VoidCallback onSizeChanged, + Duration? snapAnimationDuration, }) { return _DraggableSheetExtent( minSize: minSize, maxSize: maxSize, snap: snap, snapSizes: snapSizes, + snapAnimationDuration: snapAnimationDuration, initialSize: initialSize, onSizeChanged: onSizeChanged, // Use the possibly updated initialSize if the user hasn't dragged yet. @@ -599,6 +611,7 @@ class _DraggableScrollableSheetState extends State { maxSize: widget.maxChildSize, snap: widget.snap, snapSizes: _impliedSnapSizes(), + snapAnimationDuration: widget.snapAnimationDuration, initialSize: widget.initialChildSize, onSizeChanged: _setExtent, ); @@ -679,6 +692,7 @@ class _DraggableScrollableSheetState extends State { maxSize: widget.maxChildSize, snap: widget.snap, snapSizes: _impliedSnapSizes(), + snapAnimationDuration: widget.snapAnimationDuration, initialSize: widget.initialChildSize, onSizeChanged: _setExtent, ); @@ -902,6 +916,7 @@ class _DraggableScrollableSheetScrollPosition extends ScrollPositionWithSingleCo position: extent.currentPixels, initialVelocity: velocity, pixelSnapSize: extent.pixelSnapSizes, + snapAnimationDuration: extent.snapAnimationDuration, tolerance: physics.tolerance, ); } else { @@ -1065,12 +1080,17 @@ class _SnappingSimulation extends Simulation { required this.position, required double initialVelocity, required List pixelSnapSize, + Duration? snapAnimationDuration, super.tolerance, }) { _pixelSnapSize = _getSnapSize(initialVelocity, pixelSnapSize); + + if (snapAnimationDuration != null && snapAnimationDuration.inMilliseconds > 0) { + velocity = (_pixelSnapSize - position) * 1000 / snapAnimationDuration.inMilliseconds; + } // Check the direction of the target instead of the sign of the velocity because // we may snap in the opposite direction of velocity if velocity is very low. - if (_pixelSnapSize < position) { + else if (_pixelSnapSize < position) { velocity = math.min(-minimumSpeed, initialVelocity); } else { velocity = math.max(minimumSpeed, initialVelocity); diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index 8781f1f99b..47f82e90ba 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -15,6 +15,7 @@ void main() { double minChildSize = .25, bool snap = false, List? snapSizes, + Duration? snapAnimationDuration, double? itemExtent, Key? containerKey, Key? stackKey, @@ -40,6 +41,7 @@ void main() { initialChildSize: initialChildSize, snap: snap, snapSizes: snapSizes, + snapAnimationDuration: snapAnimationDuration, builder: (BuildContext context, ScrollController scrollController) { return NotificationListener( onNotification: onScrollNotification, @@ -485,58 +487,64 @@ void main() { }); } - testWidgets('Zero velocity drag snaps to nearest snap target', (WidgetTester tester) async { - const Key stackKey = ValueKey('stack'); - const Key containerKey = ValueKey('container'); - await tester.pumpWidget(boilerplateWidget(null, - snap: true, - stackKey: stackKey, - containerKey: containerKey, - snapSizes: [.25, .5, .75, 1.0], - )); - await tester.pumpAndSettle(); - final double screenHeight = tester.getSize(find.byKey(stackKey)).height; + for (final Duration? snapAnimationDuration in [null, const Duration(seconds: 2)]) { + testWidgets( + 'Zero velocity drag snaps to nearest snap target with ' + 'snapAnimationDuration: $snapAnimationDuration', + (WidgetTester tester) async { + const Key stackKey = ValueKey('stack'); + const Key containerKey = ValueKey('container'); + await tester.pumpWidget(boilerplateWidget(null, + snap: true, + stackKey: stackKey, + containerKey: containerKey, + snapSizes: [.25, .5, .75, 1.0], + snapAnimationDuration: snapAnimationDuration + )); + await tester.pumpAndSettle(); + final double screenHeight = tester.getSize(find.byKey(stackKey)).height; - // We are dragging up, but we'll snap down because we're closer to .75 than 1. - await tester.drag(find.text('Item 1'), Offset(0, -.35 * screenHeight)); - await tester.pumpAndSettle(); - expect( - tester.getSize(find.byKey(containerKey)).height / screenHeight, - closeTo(.75, precisionErrorTolerance), - ); + // We are dragging up, but we'll snap down because we're closer to .75 than 1. + await tester.drag(find.text('Item 1'), Offset(0, -.35 * screenHeight)); + await tester.pumpAndSettle(); + expect( + tester.getSize(find.byKey(containerKey)).height / screenHeight, + closeTo(.75, precisionErrorTolerance), + ); - // Drag up and snap up. - await tester.drag(find.text('Item 1'), Offset(0, -.2 * screenHeight)); - await tester.pumpAndSettle(); - expect( - tester.getSize(find.byKey(containerKey)).height / screenHeight, - closeTo(1.0, precisionErrorTolerance), - ); + // Drag up and snap up. + await tester.drag(find.text('Item 1'), Offset(0, -.2 * screenHeight)); + await tester.pumpAndSettle(); + expect( + tester.getSize(find.byKey(containerKey)).height / screenHeight, + closeTo(1.0, precisionErrorTolerance), + ); - // Drag down and snap up. - await tester.drag(find.text('Item 1'), Offset(0, .1 * screenHeight)); - await tester.pumpAndSettle(); - expect( - tester.getSize(find.byKey(containerKey)).height / screenHeight, - closeTo(1.0, precisionErrorTolerance), - ); + // Drag down and snap up. + await tester.drag(find.text('Item 1'), Offset(0, .1 * screenHeight)); + await tester.pumpAndSettle(); + expect( + tester.getSize(find.byKey(containerKey)).height / screenHeight, + closeTo(1.0, precisionErrorTolerance), + ); - // Drag down and snap down. - await tester.drag(find.text('Item 1'), Offset(0, .45 * screenHeight)); - await tester.pumpAndSettle(); - expect( - tester.getSize(find.byKey(containerKey)).height / screenHeight, - closeTo(.5, precisionErrorTolerance), - ); + // Drag down and snap down. + await tester.drag(find.text('Item 1'), Offset(0, .45 * screenHeight)); + await tester.pumpAndSettle(); + expect( + tester.getSize(find.byKey(containerKey)).height / screenHeight, + closeTo(.5, precisionErrorTolerance), + ); - // Fling up with negligible velocity and snap down. - await tester.fling(find.text('Item 1'), Offset(0, .1 * screenHeight), 1); - await tester.pumpAndSettle(); - expect( - tester.getSize(find.byKey(containerKey)).height / screenHeight, - closeTo(.5, precisionErrorTolerance), - ); - }, variant: TargetPlatformVariant.all()); + // Fling up with negligible velocity and snap down. + await tester.fling(find.text('Item 1'), Offset(0, .1 * screenHeight), 1); + await tester.pumpAndSettle(); + expect( + tester.getSize(find.byKey(containerKey)).height / screenHeight, + closeTo(.5, precisionErrorTolerance), + ); + }, variant: TargetPlatformVariant.all()); + } for (final List? snapSizes in ?>[null, []]) { testWidgets('Setting snapSizes to $snapSizes resolves to min and max', (WidgetTester tester) async {