Commit ea3c3f53 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Add Draggable.onDragStarted (#6343)

Fixes #6084
parent 034d2fcd
...@@ -75,6 +75,7 @@ class Draggable<T> extends StatefulWidget { ...@@ -75,6 +75,7 @@ class Draggable<T> extends StatefulWidget {
this.dragAnchor: DragAnchor.child, this.dragAnchor: DragAnchor.child,
this.affinity, this.affinity,
this.maxSimultaneousDrags, this.maxSimultaneousDrags,
this.onDragStarted,
this.onDraggableCanceled this.onDraggableCanceled
}) : super(key: key) { }) : super(key: key) {
assert(child != null); assert(child != null);
...@@ -127,6 +128,9 @@ class Draggable<T> extends StatefulWidget { ...@@ -127,6 +128,9 @@ class Draggable<T> extends StatefulWidget {
/// dragged at a time. /// dragged at a time.
final int maxSimultaneousDrags; final int maxSimultaneousDrags;
/// Called when the draggable starts being dragged.
final VoidCallback onDragStarted;
/// Called when the draggable is dropped without being accepted by a [DragTarget]. /// Called when the draggable is dropped without being accepted by a [DragTarget].
final DraggableCanceledCallback onDraggableCanceled; final DraggableCanceledCallback onDraggableCanceled;
...@@ -164,6 +168,7 @@ class LongPressDraggable<T> extends Draggable<T> { ...@@ -164,6 +168,7 @@ class LongPressDraggable<T> extends Draggable<T> {
Offset feedbackOffset: Offset.zero, Offset feedbackOffset: Offset.zero,
DragAnchor dragAnchor: DragAnchor.child, DragAnchor dragAnchor: DragAnchor.child,
int maxSimultaneousDrags, int maxSimultaneousDrags,
VoidCallback onDragStarted,
DraggableCanceledCallback onDraggableCanceled DraggableCanceledCallback onDraggableCanceled
}) : super( }) : super(
key: key, key: key,
...@@ -174,6 +179,7 @@ class LongPressDraggable<T> extends Draggable<T> { ...@@ -174,6 +179,7 @@ class LongPressDraggable<T> extends Draggable<T> {
feedbackOffset: feedbackOffset, feedbackOffset: feedbackOffset,
dragAnchor: dragAnchor, dragAnchor: dragAnchor,
maxSimultaneousDrags: maxSimultaneousDrags, maxSimultaneousDrags: maxSimultaneousDrags,
onDragStarted: onDragStarted,
onDraggableCanceled: onDraggableCanceled onDraggableCanceled: onDraggableCanceled
); );
...@@ -227,7 +233,7 @@ class _DraggableState<T> extends State<Draggable<T>> { ...@@ -227,7 +233,7 @@ class _DraggableState<T> extends State<Draggable<T>> {
setState(() { setState(() {
_activeCount += 1; _activeCount += 1;
}); });
return new _DragAvatar<T>( final _DragAvatar<T> avatar = new _DragAvatar<T>(
overlay: Overlay.of(context, debugRequiredFor: config), overlay: Overlay.of(context, debugRequiredFor: config),
data: config.data, data: config.data,
initialPosition: position, initialPosition: position,
...@@ -242,6 +248,9 @@ class _DraggableState<T> extends State<Draggable<T>> { ...@@ -242,6 +248,9 @@ class _DraggableState<T> extends State<Draggable<T>> {
}); });
} }
); );
if (config.onDragStarted != null)
config.onDragStarted();
return avatar;
} }
@override @override
......
...@@ -8,6 +8,7 @@ import 'package:flutter/material.dart'; ...@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
void main() { void main() {
testWidgets('Drag and drop - control test', (WidgetTester tester) async { testWidgets('Drag and drop - control test', (WidgetTester tester) async {
List<int> accepted = <int>[]; List<int> accepted = <int>[];
int dragStartedCount = 0;
await tester.pumpWidget(new MaterialApp( await tester.pumpWidget(new MaterialApp(
home: new Column( home: new Column(
...@@ -15,7 +16,10 @@ void main() { ...@@ -15,7 +16,10 @@ void main() {
new Draggable<int>( new Draggable<int>(
data: 1, data: 1,
child: new Text('Source'), child: new Text('Source'),
feedback: new Text('Dragging') feedback: new Text('Dragging'),
onDragStarted: () {
++dragStartedCount;
},
), ),
new DragTarget<int>( new DragTarget<int>(
builder: (BuildContext context, List<int> data, List<dynamic> rejects) { builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
...@@ -33,6 +37,7 @@ void main() { ...@@ -33,6 +37,7 @@ void main() {
expect(find.text('Source'), findsOneWidget); expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing); expect(find.text('Dragging'), findsNothing);
expect(find.text('Target'), findsOneWidget); expect(find.text('Target'), findsOneWidget);
expect(dragStartedCount, 0);
Point firstLocation = tester.getCenter(find.text('Source')); Point firstLocation = tester.getCenter(find.text('Source'));
TestGesture gesture = await tester.startGesture(firstLocation, pointer: 7); TestGesture gesture = await tester.startGesture(firstLocation, pointer: 7);
...@@ -42,6 +47,7 @@ void main() { ...@@ -42,6 +47,7 @@ void main() {
expect(find.text('Source'), findsOneWidget); expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsOneWidget); expect(find.text('Dragging'), findsOneWidget);
expect(find.text('Target'), findsOneWidget); expect(find.text('Target'), findsOneWidget);
expect(dragStartedCount, 1);
Point secondLocation = tester.getCenter(find.text('Target')); Point secondLocation = tester.getCenter(find.text('Target'));
await gesture.moveTo(secondLocation); await gesture.moveTo(secondLocation);
...@@ -51,6 +57,7 @@ void main() { ...@@ -51,6 +57,7 @@ void main() {
expect(find.text('Source'), findsOneWidget); expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsOneWidget); expect(find.text('Dragging'), findsOneWidget);
expect(find.text('Target'), findsOneWidget); expect(find.text('Target'), findsOneWidget);
expect(dragStartedCount, 1);
await gesture.up(); await gesture.up();
await tester.pump(); await tester.pump();
...@@ -59,6 +66,7 @@ void main() { ...@@ -59,6 +66,7 @@ void main() {
expect(find.text('Source'), findsOneWidget); expect(find.text('Source'), findsOneWidget);
expect(find.text('Dragging'), findsNothing); expect(find.text('Dragging'), findsNothing);
expect(find.text('Target'), findsOneWidget); expect(find.text('Target'), findsOneWidget);
expect(dragStartedCount, 1);
}); });
testWidgets('Drag and drop - dragging over button', (WidgetTester tester) async { testWidgets('Drag and drop - dragging over button', (WidgetTester tester) async {
......
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