Allow long press delay duration for LongPressDraggable to be adjustable (#74592)

This commit is contained in:
David Chen 2021-02-02 17:26:02 +08:00 committed by GitHub
parent 11609d1114
commit 98067a6552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 1 deletions

View File

@ -398,6 +398,7 @@ class LongPressDraggable<T extends Object> extends Draggable<T> {
VoidCallback? onDragCompleted, VoidCallback? onDragCompleted,
this.hapticFeedbackOnStart = true, this.hapticFeedbackOnStart = true,
bool ignoringFeedbackSemantics = true, bool ignoringFeedbackSemantics = true,
this.delay = kLongPressTimeout,
}) : super( }) : super(
key: key, key: key,
child: child, child: child,
@ -419,9 +420,14 @@ class LongPressDraggable<T extends Object> extends Draggable<T> {
/// Whether haptic feedback should be triggered on drag start. /// Whether haptic feedback should be triggered on drag start.
final bool hapticFeedbackOnStart; final bool hapticFeedbackOnStart;
/// The duration that a user has to press down before a long press is registered.
///
/// Defaults to [kLongPressTimeout].
final Duration delay;
@override @override
DelayedMultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) { DelayedMultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) {
return DelayedMultiDragGestureRecognizer() return DelayedMultiDragGestureRecognizer(delay: delay)
..onStart = (Offset position) { ..onStart = (Offset position) {
final Drag? result = onStart(position); final Drag? result = onStart(position);
if (result != null && hapticFeedbackOnStart) if (result != null && hapticFeedbackOnStart)

View File

@ -2452,6 +2452,73 @@ void main() {
expect(onDragStartedCalled, isTrue); expect(onDragStartedCalled, isTrue);
}); });
testWidgets('Custom long press delay for LongPressDraggable', (WidgetTester tester) async {
bool onDragStartedCalled = false;
await tester.pumpWidget(MaterialApp(
home: LongPressDraggable<int>(
data: 1,
delay: const Duration(seconds: 2),
child: const Text('Source'),
feedback: const Text('Dragging'),
onDragStarted: () {
onDragStartedCalled = true;
},
),
));
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing);
expect(onDragStartedCalled, isFalse);
final Offset firstLocation = tester.getCenter(find.text('Source'));
await tester.startGesture(firstLocation, pointer: 7);
await tester.pump();
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing);
expect(onDragStartedCalled, isFalse);
// Halfway into the long press duration.
await tester.pump(const Duration(seconds: 1));
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing);
expect(onDragStartedCalled, isFalse);
// Long press draggable should be showing.
await tester.pump(const Duration(seconds: 1));
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsOneWidget);
expect(onDragStartedCalled, isTrue);
});
testWidgets('Default long press delay for LongPressDraggable', (WidgetTester tester) async {
bool onDragStartedCalled = false;
await tester.pumpWidget(MaterialApp(
home: LongPressDraggable<int>(
data: 1,
child: const Text('Source'),
feedback: const Text('Dragging'),
onDragStarted: () {
onDragStartedCalled = true;
},
),
));
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing);
expect(onDragStartedCalled, isFalse);
final Offset firstLocation = tester.getCenter(find.text('Source'));
await tester.startGesture(firstLocation, pointer: 7);
await tester.pump();
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing);
expect(onDragStartedCalled, isFalse);
// Halfway into the long press duration.
await tester.pump(const Duration(milliseconds: 250));
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing);
expect(onDragStartedCalled, isFalse);
// Long press draggable should be showing.
await tester.pump(const Duration(milliseconds: 250));
expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsOneWidget);
expect(onDragStartedCalled, isTrue);
});
testWidgets('long-press draggable calls Haptic Feedback onStart', (WidgetTester tester) async { testWidgets('long-press draggable calls Haptic Feedback onStart', (WidgetTester tester) async {
await _testLongPressDraggableHapticFeedback(tester: tester, hapticFeedbackOnStart: true, expectedHapticFeedbackCount: 1); await _testLongPressDraggableHapticFeedback(tester: tester, hapticFeedbackOnStart: true, expectedHapticFeedbackCount: 1);
}); });