Skip to content

Commit

Permalink
[framework] add gesture settings to draggable (#99567)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored Mar 4, 2022
1 parent 59859df commit 8e6d57b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/flutter/lib/src/widgets/drag_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/services.dart';
import 'basic.dart';
import 'binding.dart';
import 'framework.dart';
import 'media_query.dart';
import 'overlay.dart';

/// Signature for determining whether the given data will be accepted by a [DragTarget].
Expand Down Expand Up @@ -500,6 +501,12 @@ class _DraggableState<T extends Object> extends State<Draggable<T>> {
super.dispose();
}

@override
void didChangeDependencies() {
_recognizer!.gestureSettings = MediaQuery.maybeOf(context)?.gestureSettings;
super.didChangeDependencies();
}

// This gesture recognizer has an unusual lifetime. We want to support the use
// case of removing the Draggable from the tree in the middle of a drag. That
// means we need to keep this recognizer alive after this state object has
Expand Down
77 changes: 72 additions & 5 deletions packages/flutter/test/gestures/gesture_config_regression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class TestResult {
bool dragUpdate = false;
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.testResult}) : super(key: key);
class NestedScrollableCase extends StatefulWidget {
const NestedScrollableCase({Key? key, required this.testResult}) : super(key: key);

final TestResult testResult;

@override
State<MyHomePage> createState() => _MyHomePageState();
State<NestedScrollableCase> createState() => _NestedScrollableCaseState();
}

class _MyHomePageState extends State<MyHomePage> {
class _NestedScrollableCaseState extends State<NestedScrollableCase> {

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -57,6 +57,50 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

class NestedDragableCase extends StatefulWidget {
const NestedDragableCase({Key? key, required this.testResult}) : super(key: key);

final TestResult testResult;

@override
State<NestedDragableCase> createState() => _NestedDragableCaseState();
}

class _NestedDragableCaseState extends State<NestedDragableCase> {

@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverFixedExtentList(
itemExtent: 50.0,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
alignment: Alignment.center,
child: Draggable<Object>(
key: ValueKey<int>(index),
feedback: const Text('Dragging'),
child: Text('List Item $index'),
onDragStarted: () {
widget.testResult.dragStarted = true;
},
onDragUpdate: (DragUpdateDetails details){
widget.testResult.dragUpdate = true;
},
onDragEnd: (_) {},
),
);
},
),
),
],
),
);
}
}

void main() {
testWidgets('Scroll Views get the same ScrollConfiguration as GestureDetectors', (WidgetTester tester) async {
tester.binding.window.viewConfigurationTestValue = const ui.ViewConfiguration(
Expand All @@ -66,7 +110,30 @@ void main() {

await tester.pumpWidget(MaterialApp(
title: 'Scroll Bug',
home: MyHomePage(testResult: result),
home: NestedScrollableCase(testResult: result),
));

// By dragging the scroll view more than the configured touch slop above but less than
// the framework default value, we demonstrate that this causes gesture detectors
// that do not receive the same gesture settings to fire at different times than would
// be expected.
final Offset start = tester.getCenter(find.byKey(const ValueKey<int>(1)));
await tester.timedDragFrom(start, const Offset(0, 5), const Duration(milliseconds: 50));
await tester.pumpAndSettle();

expect(result.dragStarted, true);
expect(result.dragUpdate, true);
});

testWidgets('Scroll Views get the same ScrollConfiguration as Draggables', (WidgetTester tester) async {
tester.binding.window.viewConfigurationTestValue = const ui.ViewConfiguration(
gestureSettings: ui.GestureSettings(physicalTouchSlop: 4),
);
final TestResult result = TestResult();

await tester.pumpWidget(MaterialApp(
title: 'Scroll Bug',
home: NestedDragableCase(testResult: result),
));

// By dragging the scroll view more than the configured touch slop above but less than
Expand Down

0 comments on commit 8e6d57b

Please sign in to comment.