From 8e4ca4bd34d1ce58b495d1bb9dd89c9c0c118a32 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 21 Sep 2016 20:45:00 -0700 Subject: [PATCH] Draggable should have an affinity that selects an axis (#5985) This structure for the API is hopefully less confusing that the previous one (which implied that vertical drags would not trigger horizontally draggable widgets). Fixes #1987 --- .../flutter/lib/src/widgets/drag_target.dart | 171 ++++++------------ .../flutter/test/widget/draggable_test.dart | 20 +- 2 files changed, 70 insertions(+), 121 deletions(-) diff --git a/packages/flutter/lib/src/widgets/drag_target.dart b/packages/flutter/lib/src/widgets/drag_target.dart index e78dcbed97..43b9b7106f 100644 --- a/packages/flutter/lib/src/widgets/drag_target.dart +++ b/packages/flutter/lib/src/widgets/drag_target.dart @@ -49,13 +49,23 @@ enum DragAnchor { pointer, } -/// Subclass this widget to customize the gesture used to start a drag. -abstract class DraggableBase extends StatefulWidget { - /// Initializes fields for subclasses. +/// A widget that can be dragged from to a [DragTarget]. +/// +/// When a draggable widget recognizes the start of a drag gesture, it displays +/// a [feedback] widget that tracks the user's finger across the screen. If the +/// user lifts their finger while on top of a [DragTarget], that target is given +/// the opportunity to accept the [data] carried by the draggble. +/// +/// See also: +/// +/// * [DragTarget] +/// * [LongPressDraggable] +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. - DraggableBase({ + Draggable({ Key key, @required this.child, @required this.feedback, @@ -63,6 +73,7 @@ abstract class DraggableBase extends StatefulWidget { this.childWhenDragging, this.feedbackOffset: Offset.zero, this.dragAnchor: DragAnchor.child, + this.affinity, this.maxSimultaneousDrags, this.onDraggableCanceled }) : super(key: key) { @@ -95,6 +106,22 @@ abstract class DraggableBase extends StatefulWidget { /// Where this widget should be anchored during a drag. final DragAnchor dragAnchor; + /// Controls how this widget competes with other gestures to initiate a drag. + /// + /// If affinity is null, this widget initiates a drag as soon as it recognizes + /// a tap down gesture, regardless of any directionality. If affinity is + /// horizontal (or vertical), then this widget will compete with other + /// horizontal (or vertical, respectively) gestures. + /// + /// For example, if this widget is placed in a vertically scrolling region and + /// has horizontal affinity, pointer motion in the vertical direction will + /// result in a scroll and pointer motion in the horizontal direction will + /// result in a drag. Conversely, if the widget has a null or vertical + /// affinity, pointer motion in any direction will result in a drag rather + /// than in a scroll because the draggable widget, being the more specific + /// 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. @@ -103,120 +130,27 @@ abstract class DraggableBase extends StatefulWidget { /// Called when the draggable is dropped without being accepted by a [DragTarget]. final DraggableCanceledCallback onDraggableCanceled; - /// Should return a new MultiDragGestureRecognizer instance - /// constructed with the given arguments. - MultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart); + /// Creates a gesture recognizer that recognizes the start of the drag. + /// + /// Subclasses can override this function to customize when they start + /// recognizing a drag. + @protected + MultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) { + switch (affinity) { + case Axis.horizontal: + return new HorizontalMultiDragGestureRecognizer()..onStart = onStart; + case Axis.vertical: + return new VerticalMultiDragGestureRecognizer()..onStart = onStart; + } + return new ImmediateMultiDragGestureRecognizer()..onStart = onStart; + } @override _DraggableState createState() => new _DraggableState(); } -/// Makes its child draggable starting from tap down. -class Draggable extends DraggableBase { - /// Creates a widget that can be dragged starting from tap down. - /// - /// The [child] and [feedback] arguments must not be null. If - /// [maxSimultaneousDrags] is non-null, it must be positive. - Draggable({ - Key key, - @required Widget child, - @required Widget feedback, - T data, - Widget childWhenDragging, - Offset feedbackOffset: Offset.zero, - DragAnchor dragAnchor: DragAnchor.child, - int maxSimultaneousDrags, - DraggableCanceledCallback onDraggableCanceled - }) : super( - key: key, - child: child, - feedback: feedback, - data: data, - childWhenDragging: childWhenDragging, - feedbackOffset: feedbackOffset, - dragAnchor: dragAnchor, - maxSimultaneousDrags: maxSimultaneousDrags, - onDraggableCanceled: onDraggableCanceled - ); - - @override - ImmediateMultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) { - return new ImmediateMultiDragGestureRecognizer()..onStart = onStart; - } -} - -/// Makes its child draggable. When competing with other gestures, -/// this will only start the drag horizontally. -class HorizontalDraggable extends DraggableBase { - /// Creates a widget that can be dragged. - /// - /// The [child] and [feedback] arguments must not be null. If - /// [maxSimultaneousDrags] is non-null, it must be positive. - HorizontalDraggable({ - Key key, - @required Widget child, - @required Widget feedback, - T data, - Widget childWhenDragging, - Offset feedbackOffset: Offset.zero, - DragAnchor dragAnchor: DragAnchor.child, - int maxSimultaneousDrags, - DraggableCanceledCallback onDraggableCanceled - }) : super( - key: key, - child: child, - feedback: feedback, - data: data, - childWhenDragging: childWhenDragging, - feedbackOffset: feedbackOffset, - dragAnchor: dragAnchor, - maxSimultaneousDrags: maxSimultaneousDrags, - onDraggableCanceled: onDraggableCanceled - ); - - @override - HorizontalMultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) { - return new HorizontalMultiDragGestureRecognizer()..onStart = onStart; - } -} - -/// Makes its child draggable. When competing with other gestures, -/// this will only start the drag vertically. -class VerticalDraggable extends DraggableBase { - /// Creates a widget that can be dragged. - /// - /// The [child] and [feedback] arguments must not be null. If - /// [maxSimultaneousDrags] is non-null, it must be positive. - VerticalDraggable({ - Key key, - @required Widget child, - @required Widget feedback, - T data, - Widget childWhenDragging, - Offset feedbackOffset: Offset.zero, - DragAnchor dragAnchor: DragAnchor.child, - int maxSimultaneousDrags, - DraggableCanceledCallback onDraggableCanceled - }) : super( - key: key, - child: child, - feedback: feedback, - data: data, - childWhenDragging: childWhenDragging, - feedbackOffset: feedbackOffset, - dragAnchor: dragAnchor, - maxSimultaneousDrags: maxSimultaneousDrags, - onDraggableCanceled: onDraggableCanceled - ); - - @override - VerticalMultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) { - return new VerticalMultiDragGestureRecognizer()..onStart = onStart; - } -} - /// Makes its child draggable starting from long press. -class LongPressDraggable extends DraggableBase { +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 @@ -255,7 +189,7 @@ class LongPressDraggable extends DraggableBase { } } -class _DraggableState extends State> { +class _DraggableState extends State> { @override void initState() { super.initState(); @@ -324,6 +258,17 @@ class _DraggableState extends State> { } /// A widget that receives data when a [Draggable] widget is dropped. +/// +/// When a draggable is dragged on top of a drag target, the drag target is +/// asked whether it will accept the data the draggable is carrying. If the user +/// does drop the draggable on top of the drag target (and the drag target has +/// indicated that it will accept the draggable's data), then the drag target is +/// asked to accept the draggable's data. +/// +/// See also: +/// +/// * [Draggable] +/// * [LongPressDraggable] class DragTarget extends StatefulWidget { /// Creates a widget that receives drags. /// diff --git a/packages/flutter/test/widget/draggable_test.dart b/packages/flutter/test/widget/draggable_test.dart index 3e66448a2c..76eb629361 100644 --- a/packages/flutter/test/widget/draggable_test.dart +++ b/packages/flutter/test/widget/draggable_test.dart @@ -315,15 +315,17 @@ void main() { } ), new Container(height: 400.0), - new HorizontalDraggable( + new Draggable( data: 1, child: new Text('H'), - feedback: new Text('Dragging') + feedback: new Text('Dragging'), + affinity: Axis.horizontal, ), - new VerticalDraggable( + new Draggable( data: 2, child: new Text('V'), - feedback: new Text('Dragging') + feedback: new Text('Dragging'), + affinity: Axis.vertical, ), new Container(height: 500.0), new Container(height: 500.0), @@ -420,15 +422,17 @@ void main() { } ), new Container(width: 400.0), - new HorizontalDraggable( + new Draggable( data: 1, child: new Text('H'), - feedback: new Text('Dragging') + feedback: new Text('Dragging'), + affinity: Axis.horizontal, ), - new VerticalDraggable( + new Draggable( data: 2, child: new Text('V'), - feedback: new Text('Dragging') + feedback: new Text('Dragging'), + affinity: Axis.vertical, ), new Container(width: 500.0), new Container(width: 500.0),