Skip to content

Commit f658cc4

Browse files
committed
Use onDragStart event
The previous implementarion use onTapDown to start dragging instead of the events "onHorizontalDragStart" and "onVerticalDragStart" provided by GestureDetector. Using onTapDown might confuse users because it requires a few seconds before actually starting the drag and drop event which is not the expected behavior.
1 parent 63e53a8 commit f658cc4

File tree

2 files changed

+60
-56
lines changed

2 files changed

+60
-56
lines changed

lib/board_item.dart

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,50 @@ class BoardItemState extends State<BoardItem>
6363
}
6464
}
6565

66+
bool get canStartDragging =>
67+
widget.boardList!.widget.boardView!.widget.isNotSelecting &&
68+
widget.draggable;
69+
6670
void _startDrag(Widget item, BuildContext context) {
67-
if (widget.boardList!.widget.boardView != null) {
68-
widget.boardList!.widget.boardView!.onDropItem = _onDropItem;
69-
if (widget.boardList!.mounted) {
70-
widget.boardList!.setState(() {});
71-
}
72-
widget.boardList!.widget.boardView!.draggedItemIndex = widget.index;
73-
widget.boardList!.widget.boardView!.height = context.size!.height;
74-
widget.boardList!.widget.boardView!.draggedListIndex =
75-
widget.boardList!.widget.index;
76-
widget.boardList!.widget.boardView!.startListIndex =
77-
widget.boardList!.widget.index;
78-
widget.boardList!.widget.boardView!.startItemIndex = widget.index;
79-
widget.boardList!.widget.boardView!.draggedItem = item;
80-
if (widget.onStartDragItem != null) {
81-
widget.onStartDragItem!(
82-
widget.boardList!.widget.index, widget.index, this);
83-
}
84-
widget.boardList!.widget.boardView!.run();
85-
if (widget.boardList!.widget.boardView!.mounted) {
86-
widget.boardList!.widget.boardView!.setState(() {});
71+
if (canStartDragging) {
72+
RenderBox object = context.findRenderObject() as RenderBox;
73+
Offset pos = object.localToGlobal(Offset.zero);
74+
RenderBox box = widget.boardList!.context.findRenderObject() as RenderBox;
75+
Offset listPos = box.localToGlobal(Offset.zero);
76+
widget.boardList!.widget.boardView!.leftListX = listPos.dx;
77+
widget.boardList!.widget.boardView!.topListY = listPos.dy;
78+
widget.boardList!.widget.boardView!.topItemY = pos.dy;
79+
widget.boardList!.widget.boardView!.bottomItemY =
80+
pos.dy + object.size.height;
81+
widget.boardList!.widget.boardView!.bottomListY =
82+
listPos.dy + box.size.height;
83+
widget.boardList!.widget.boardView!.rightListX =
84+
listPos.dx + box.size.width;
85+
86+
widget.boardList!.widget.boardView!.initialX = pos.dx;
87+
widget.boardList!.widget.boardView!.initialY = pos.dy;
88+
89+
if (widget.boardList!.widget.boardView != null) {
90+
widget.boardList!.widget.boardView!.onDropItem = _onDropItem;
91+
if (widget.boardList!.mounted) {
92+
widget.boardList!.setState(() {});
93+
}
94+
widget.boardList!.widget.boardView!.draggedItemIndex = widget.index;
95+
widget.boardList!.widget.boardView!.height = context.size!.height;
96+
widget.boardList!.widget.boardView!.draggedListIndex =
97+
widget.boardList!.widget.index;
98+
widget.boardList!.widget.boardView!.startListIndex =
99+
widget.boardList!.widget.index;
100+
widget.boardList!.widget.boardView!.startItemIndex = widget.index;
101+
widget.boardList!.widget.boardView!.draggedItem = item;
102+
if (widget.onStartDragItem != null) {
103+
widget.onStartDragItem!(
104+
widget.boardList!.widget.index, widget.index, this);
105+
}
106+
widget.boardList!.widget.boardView!.run();
107+
if (widget.boardList!.widget.boardView!.mounted) {
108+
widget.boardList!.widget.boardView!.setState(() {});
109+
}
87110
}
88111
}
89112
}
@@ -105,39 +128,14 @@ class BoardItemState extends State<BoardItem>
105128
}
106129
widget.boardList!.itemStates.insert(widget.index!, this);
107130
return GestureDetector(
108-
onTapDown: (otd) {
109-
if (widget.draggable) {
110-
RenderBox object = context.findRenderObject() as RenderBox;
111-
Offset pos = object.localToGlobal(Offset.zero);
112-
RenderBox box =
113-
widget.boardList!.context.findRenderObject() as RenderBox;
114-
Offset listPos = box.localToGlobal(Offset.zero);
115-
widget.boardList!.widget.boardView!.leftListX = listPos.dx;
116-
widget.boardList!.widget.boardView!.topListY = listPos.dy;
117-
widget.boardList!.widget.boardView!.topItemY = pos.dy;
118-
widget.boardList!.widget.boardView!.bottomItemY =
119-
pos.dy + object.size.height;
120-
widget.boardList!.widget.boardView!.bottomListY =
121-
listPos.dy + box.size.height;
122-
widget.boardList!.widget.boardView!.rightListX =
123-
listPos.dx + box.size.width;
124-
125-
widget.boardList!.widget.boardView!.initialX = pos.dx;
126-
widget.boardList!.widget.boardView!.initialY = pos.dy;
127-
}
128-
},
129-
onTapCancel: () {},
130131
onTap: () {
131132
if (widget.onTapItem != null) {
132133
widget.onTapItem!(widget.boardList!.widget.index, widget.index, this);
133134
}
134135
},
135-
onLongPress: () {
136-
if (!widget.boardList!.widget.boardView!.widget.isSelecting &&
137-
widget.draggable) {
138-
_startDrag(widget, context);
139-
}
140-
},
136+
onHorizontalDragStart: (DragStartDetails _) =>
137+
_startDrag(widget, context),
138+
onVerticalDragStart: (DragStartDetails _) => _startDrag(widget, context),
141139
child: widget.item,
142140
);
143141
}

lib/board_view.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class BoardView extends StatefulWidget {
3535
this.bottomPadding,
3636
}) : super(key: key);
3737

38+
bool get isNotSelecting => !isSelecting;
39+
3840
@override
3941
State<StatefulWidget> createState() {
4042
return BoardViewState();
@@ -529,10 +531,12 @@ class BoardViewState extends State<BoardView>
529531
duration: const Duration(milliseconds: 10),
530532
curve: Curves.ease)
531533
.whenComplete(() {
532-
pos -= listStates[draggedListIndex!]
533-
.boardListController
534-
.position
535-
.pixels;
534+
if (draggedListIndex != null) {
535+
pos -= listStates[draggedListIndex!]
536+
.boardListController
537+
.position
538+
.pixels;
539+
}
536540
initialY ??= 0;
537541
// if(widget.boardViewController != null) {
538542
// initialY -= pos;
@@ -589,10 +593,12 @@ class BoardViewState extends State<BoardView>
589593
duration: const Duration(milliseconds: 10),
590594
curve: Curves.ease)
591595
.whenComplete(() {
592-
pos -= listStates[draggedListIndex!]
593-
.boardListController
594-
.position
595-
.pixels;
596+
if (draggedListIndex != null) {
597+
pos -= listStates[draggedListIndex!]
598+
.boardListController
599+
.position
600+
.pixels;
601+
}
596602
initialY ??= 0;
597603
// if(widget.boardViewController != null) {
598604
// initialY -= pos;

0 commit comments

Comments
 (0)