From 126283337772b602b8a062722a23c926c17d9043 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sat, 15 Oct 2016 18:25:25 -0700 Subject: [PATCH] Support zero for Draggable.maxSimultaneousDrags (#6342) This patch also makes it possible for a single DragTarget to handle multiple draggables with the same data without asserting. Fixes #6086 --- .../flutter/lib/src/widgets/drag_target.dart | 61 +++++----- .../flutter/test/widget/draggable_test.dart | 112 ++++++++++++++++++ 2 files changed, 145 insertions(+), 28 deletions(-) diff --git a/packages/flutter/lib/src/widgets/drag_target.dart b/packages/flutter/lib/src/widgets/drag_target.dart index 13973bb6d8..6f7b21ff78 100644 --- a/packages/flutter/lib/src/widgets/drag_target.dart +++ b/packages/flutter/lib/src/widgets/drag_target.dart @@ -64,7 +64,7 @@ class Draggable extends StatefulWidget { /// Creates a widget that can be dragged to a [DragTarget]. /// /// The [child] and [feedback] arguments must not be null. If - /// [maxSimultaneousDrags] is non-null, it must be positive. + /// [maxSimultaneousDrags] is non-null, it must be non-negative. Draggable({ Key key, @required this.child, @@ -80,7 +80,7 @@ class Draggable extends StatefulWidget { }) : super(key: key) { assert(child != null); assert(feedback != null); - assert(maxSimultaneousDrags == null || maxSimultaneousDrags > 0); + assert(maxSimultaneousDrags == null || maxSimultaneousDrags >= 0); } /// The data that will be dropped by this draggable. @@ -123,9 +123,11 @@ class Draggable extends StatefulWidget { /// widget, will out-compete the [Scrollable] for vertical gestures. final Axis affinity; - /// How many simultaneous drags to support. When null, no limit is applied. - /// Set this to 1 if you want to only allow the drag source to have one item - /// dragged at a time. + /// How many simultaneous drags to support. + /// + /// When null, no limit is applied. Set this to 1 if you want to only allow + /// the drag source to have one item dragged at a time. Set this to 0 if you + /// want to prevent the draggable from actually being dragged. final int maxSimultaneousDrags; /// Called when the draggable starts being dragged. @@ -158,7 +160,7 @@ class LongPressDraggable extends Draggable { /// Creates a widget that can be dragged starting from long press. /// /// The [child] and [feedback] arguments must not be null. If - /// [maxSimultaneousDrags] is non-null, it must be positive. + /// [maxSimultaneousDrags] is non-null, it must be non-negative. LongPressDraggable({ Key key, @required Widget child, @@ -306,42 +308,46 @@ class DragTarget extends StatefulWidget { _DragTargetState createState() => new _DragTargetState(); } -class _DragTargetState extends State> { - final List _candidateData = new List(); - final List _rejectedData = new List(); +List/**/ _mapAvatarsToData/**/(List/*<_DragAvatar>*/ avatars) { + return avatars.map/**/((_DragAvatar/**/ avatar) => avatar.data).toList(); +} - bool didEnter(dynamic data) { - assert(!_candidateData.contains(data)); - assert(!_rejectedData.contains(data)); - if (data is T && (config.onWillAccept == null || config.onWillAccept(data))) { +class _DragTargetState extends State> { + final List<_DragAvatar> _candidateAvatars = new List<_DragAvatar>(); + final List<_DragAvatar> _rejectedAvatars = new List<_DragAvatar>(); + + bool didEnter(_DragAvatar avatar) { + assert(!_candidateAvatars.contains(avatar)); + assert(!_rejectedAvatars.contains(avatar)); + if (avatar.data is T && (config.onWillAccept == null || config.onWillAccept(avatar.data))) { setState(() { - _candidateData.add(data); + _candidateAvatars.add(avatar); }); return true; } - _rejectedData.add(data); + _rejectedAvatars.add(avatar); return false; } - void didLeave(dynamic data) { - assert(_candidateData.contains(data) || _rejectedData.contains(data)); + void didLeave(_DragAvatar avatar) { + assert(_candidateAvatars.contains(avatar) || _rejectedAvatars.contains(avatar)); if (!mounted) return; setState(() { - _candidateData.remove(data); - _rejectedData.remove(data); + _candidateAvatars.remove(avatar); + _rejectedAvatars.remove(avatar); }); } - void didDrop(dynamic data) { - assert(_candidateData.contains(data)); + void didDrop(_DragAvatar avatar) { + assert(_candidateAvatars.contains(avatar)); if (!mounted) return; setState(() { - _candidateData.remove(data); + _candidateAvatars.remove(avatar); }); if (config.onAccept != null) - config.onAccept(data); + config.onAccept(avatar.data); } @override @@ -350,12 +356,11 @@ class _DragTargetState extends State> { return new MetaData( metaData: this, behavior: HitTestBehavior.translucent, - child: config.builder(context, _candidateData, _rejectedData) + child: config.builder(context, _mapAvatarsToData/**/(_candidateAvatars), _mapAvatarsToData(_rejectedAvatars)) ); } } - enum _DragEndKind { dropped, canceled } typedef void _OnDragEnd(Velocity velocity, Offset offset, bool wasAccepted); @@ -443,7 +448,7 @@ class _DragAvatar extends Drag { // Enter new targets. _DragTargetState newTarget = targets.firstWhere((_DragTargetState target) { _enteredTargets.add(target); - return target.didEnter(data); + return target.didEnter(this); }, orElse: () => null ); @@ -465,14 +470,14 @@ class _DragAvatar extends Drag { void _leaveAllEntered() { for (int i = 0; i < _enteredTargets.length; i += 1) - _enteredTargets[i].didLeave(data); + _enteredTargets[i].didLeave(this); _enteredTargets.clear(); } void finishDrag(_DragEndKind endKind, [Velocity velocity]) { bool wasAccepted = false; if (endKind == _DragEndKind.dropped && _activeTarget != null) { - _activeTarget.didDrop(data); + _activeTarget.didDrop(this); wasAccepted = true; _enteredTargets.remove(_activeTarget); } diff --git a/packages/flutter/test/widget/draggable_test.dart b/packages/flutter/test/widget/draggable_test.dart index c67d3cd7ef..c8a8e2fe67 100644 --- a/packages/flutter/test/widget/draggable_test.dart +++ b/packages/flutter/test/widget/draggable_test.dart @@ -884,6 +884,118 @@ void main() { } }); + testWidgets('Drag and drop - maxSimultaneousDrags', (WidgetTester tester) async { + List accepted = []; + + Widget build(int maxSimultaneousDrags) { + return new MaterialApp( + home: new Column( + children: [ + new Draggable( + data: 1, + maxSimultaneousDrags: maxSimultaneousDrags, + child: new Text('Source'), + feedback: new Text('Dragging') + ), + new DragTarget( + builder: (BuildContext context, List data, List rejects) { + return new Container(height: 100.0, child: new Text('Target')); + }, + onAccept: (int data) { + accepted.add(data); + } + ), + ] + ) + ); + } + + await tester.pumpWidget(build(0)); + + Point firstLocation = tester.getCenter(find.text('Source')); + Point secondLocation = tester.getCenter(find.text('Target')); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNothing); + expect(find.text('Target'), findsOneWidget); + + TestGesture gesture = await tester.startGesture(firstLocation, pointer: 7); + await tester.pump(); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNothing); + expect(find.text('Target'), findsOneWidget); + + await gesture.up(); + + await tester.pumpWidget(build(2)); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNothing); + expect(find.text('Target'), findsOneWidget); + + TestGesture gesture1 = await tester.startGesture(firstLocation, pointer: 8); + await tester.pump(); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsOneWidget); + expect(find.text('Target'), findsOneWidget); + + TestGesture gesture2 = await tester.startGesture(firstLocation, pointer: 9); + await tester.pump(); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNWidgets(2)); + expect(find.text('Target'), findsOneWidget); + + TestGesture gesture3 = await tester.startGesture(firstLocation, pointer: 10); + await tester.pump(); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNWidgets(2)); + expect(find.text('Target'), findsOneWidget); + + await gesture1.moveTo(secondLocation); + await gesture2.moveTo(secondLocation); + await gesture3.moveTo(secondLocation); + await tester.pump(); + + expect(accepted, isEmpty); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNWidgets(2)); + expect(find.text('Target'), findsOneWidget); + + await gesture1.up(); + await tester.pump(); + + expect(accepted, equals([1])); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsOneWidget); + expect(find.text('Target'), findsOneWidget); + + await gesture2.up(); + await tester.pump(); + + expect(accepted, equals([1, 1])); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNothing); + expect(find.text('Target'), findsOneWidget); + + await gesture3.up(); + await tester.pump(); + + expect(accepted, equals([1, 1])); + expect(find.text('Source'), findsOneWidget); + expect(find.text('Dragging'), findsNothing); + expect(find.text('Target'), findsOneWidget); + }); + testWidgets('Draggable disposes recognizer', (WidgetTester tester) async { bool didTap = false; await tester.pumpWidget(new Overlay(