Commit 9447be73 authored by Diego Tori's avatar Diego Tori Committed by Ian Hickson

Added support for passing in velocity and offset into Draggable.onDragCompleted (#22267)

* Added support for passing in velocity and offset into Draggable.onDragCompleted.

* Fixed documentation of DragCompletedCallback.

* Spun off previous onDragCompleted breaking changes into new callback called Draggable.onDragEnd.

* Revert "Fixed documentation of DragCompletedCallback."

This reverts commit 069051f5be0d4ec6a1f4b3f072e535ca87e5d740.

* Revert "Added support for passing in velocity and offset into Draggable.onDragCompleted."

This reverts commit 7ef744aa5645429b7bc92527226203ee8bff68ec.

* DraggableDetails constructor is now declared first as per Flutter code style.

* Draggable.onDragEnd will only call back if its widget is currently mounted to the tree.

* Moved "});" in DraggableDetails constructor to new line, vertically aligned with the constructor name, as per Flutter code style.

* Added space between if statement in drag_target.dart.

* widget.onDragEnd call is now formated as per flutter code style.

* Added more details to DraggableDetails documentation.

* Added brackets to if statement block as per Flutter code style.

* Fixed minor nits in DraggableDetails documentation.

* Made DraggableDetails constructor public. Also added documentation for its constructor.
parent 33f8030b
......@@ -36,6 +36,15 @@ typedef DragTargetBuilder<T> = Widget Function(BuildContext context, List<T> can
/// Used by [Draggable.onDraggableCanceled].
typedef DraggableCanceledCallback = void Function(Velocity velocity, Offset offset);
/// Signature for when the draggable is dropped.
///
/// The velocity and offset at which the pointer was moving when the draggable
/// was dropped is available in the [DraggableDetails]. Also included in the
/// `details` is whether the draggable's [DragTarget] accepted it.
///
/// Used by [Draggable.onDragEnd]
typedef DragEndCallback = void Function(DraggableDetails details);
/// Signature for when a [Draggable] leaves a [DragTarget].
///
/// Used by [DragTarget.onLeave].
......@@ -100,6 +109,7 @@ class Draggable<T> extends StatefulWidget {
this.maxSimultaneousDrags,
this.onDragStarted,
this.onDraggableCanceled,
this.onDragEnd,
this.onDragCompleted,
this.ignoringFeedbackSemantics = true,
}) : assert(child != null),
......@@ -229,6 +239,16 @@ class Draggable<T> extends StatefulWidget {
/// callback is still in the tree.
final VoidCallback onDragCompleted;
/// Called when the draggable is dropped.
///
/// The velocity and offset at which the pointer was moving when it was
/// dropped is available in the [DraggableDetails]. Also included in the
/// `details` is whether the draggable's [DragTarget] accepted it.
///
/// This function will only be called while this widget is still mounted to
/// the tree (i.e. [State.mounted] is true).
final DragEndCallback onDragEnd;
/// Creates a gesture recognizer that recognizes the start of the drag.
///
/// Subclasses can override this function to customize when they start
......@@ -266,6 +286,7 @@ class LongPressDraggable<T> extends Draggable<T> {
int maxSimultaneousDrags,
VoidCallback onDragStarted,
DraggableCanceledCallback onDraggableCanceled,
DragEndCallback onDragEnd,
VoidCallback onDragCompleted,
this.hapticFeedbackOnStart = true,
bool ignoringFeedbackSemantics = true,
......@@ -281,6 +302,7 @@ class LongPressDraggable<T> extends Draggable<T> {
maxSimultaneousDrags: maxSimultaneousDrags,
onDragStarted: onDragStarted,
onDraggableCanceled: onDraggableCanceled,
onDragEnd: onDragEnd,
onDragCompleted: onDragCompleted,
ignoringFeedbackSemantics: ignoringFeedbackSemantics,
);
......@@ -372,6 +394,13 @@ class _DraggableState<T> extends State<Draggable<T>> {
_activeCount -= 1;
_disposeRecognizerIfInactive();
}
if (mounted && widget.onDragEnd != null) {
widget.onDragEnd(DraggableDetails(
wasAccepted: wasAccepted,
velocity: velocity,
offset: offset
));
}
if (wasAccepted && widget.onDragCompleted != null)
widget.onDragCompleted();
if (!wasAccepted && widget.onDraggableCanceled != null)
......@@ -396,6 +425,38 @@ class _DraggableState<T> extends State<Draggable<T>> {
}
}
/// Represents the details when a specific pointer event occurred on
/// the [Draggable].
///
/// This includes the [Velocity] at which the pointer was moving and [Offset]
/// when the draggable event occurred, and whether its [DragTarget] accepted it.
///
/// Also, this is the details object for callbacks that use [DragEndCallback].
class DraggableDetails {
/// Creates details for a [DraggableDetails].
///
/// If [wasAccepted] is not specified, it will default to `false`.
///
/// The [velocity] or [offset] arguments must not be `null`.
DraggableDetails({
this.wasAccepted = false,
@required this.velocity,
@required this.offset
}) : assert(velocity != null),
assert(offset != null);
/// Determines whether the [DragTarget] accepted this draggable.
final bool wasAccepted;
/// The velocity at which the pointer was moving when the specific pointer
/// event occurred on the draggable.
final Velocity velocity;
/// The global position when the specific pointer event occurred on
/// the draggable.
final Offset offset;
}
/// 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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment