Skip to content

Commit 3664d93

Browse files
author
yangshangzhi
committed
feat: 增加longdraggable‘
1 parent 06058fc commit 3664d93

File tree

4 files changed

+134
-11
lines changed

4 files changed

+134
-11
lines changed
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
## ****
1+
## **LongPressDraggable**
2+
> 长按能拖动的组件,属性中child和feedback不能为空
3+
4+
### 构造函数
5+
```
6+
LongPressDraggable<T>({
7+
Key key,
8+
@required Widget child,
9+
@required Widget feedback,
10+
T data,
11+
Axis axis,
12+
Widget childWhenDragging,
13+
Offset feedbackOffset: Offset.zero,
14+
DragAnchor dragAnchor: DragAnchor.child,
15+
int maxSimultaneousDrags,
16+
VoidCallback onDragStarted,
17+
DraggableCanceledCallback onDraggableCanceled,
18+
DragEndCallback onDragEnd,
19+
VoidCallback onDragCompleted,
20+
bool hapticFeedbackOnStart: true,
21+
bool ignoringFeedbackSemantics: true
22+
})
23+
```
24+
25+
### 属性介绍
26+
- child: 拖动目标子组件,可配合DragTarget使用
27+
- feedback: 拖动时跟随手势组件/提示内容
28+
- feedbackOffset: 以手势为中心的偏移量,会影响命中的目标
29+
- data: 定义下标,该值会在DragTarget中返回
30+
- axis: Axis.horizontal/Axis.vertical, 默认不限制,赋值后按照限制水平拖动或垂直拖动
31+
- childWhenDragging: 拖动时,可修改被拖动组件的内容。
32+
- onDragStarted: () {}, 拖动开始时回调
33+
- DragEndCallback: (detials) {}, 拖动结束时回调, 返回拖动相关属性
34+
- onDragCompleted: () {}, 完成拖动时回调
35+
- onDraggableCanceled: () {}, 拖动取消
36+
37+

lib/widget/gestures/index.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ const nameSpaces = '/gestures_';
1111
List widgets = [
1212
ItemInfo(
1313
widget: absorbpointer.Index(),
14-
code: 58348, // grid_on
14+
code: 58320, // grid_on
1515
title: absorbpointer.Index.title,
1616
),
1717
ItemInfo(
1818
widget: dismissible.Index(),
19-
code: 58348, // grid_on
19+
code: 58321, // grid_on
2020
title: dismissible.Index.title,
2121
),
2222
ItemInfo(
2323
widget: dragtarget.Index(),
24-
code: 58348, // grid_on
24+
code: 58322, // grid_on
2525
title: dragtarget.Index.title,
2626
),
2727
ItemInfo(
2828
widget: gesturedetector.Index(),
29-
code: 58348, // grid_on
29+
code: 58324, // grid_on
3030
title: gesturedetector.Index.title,
3131
),
3232
ItemInfo(
3333
widget: ignorepointer.Index(),
34-
code: 58348, // grid_on
34+
code: 58325, // grid_on
3535
title: ignorepointer.Index.title,
3636
),
3737
ItemInfo(
3838
widget: longpressdraggable.Index(),
39-
code: 58348, // grid_on
39+
code: 58326, // filter_6
4040
title: longpressdraggable.Index.title,
4141
),
4242
];

lib/widget/gestures/longpressdraggable/demo.dart

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22

33
class Index extends StatefulWidget {
4+
final List _list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].toList();
45
@override
56
State<StatefulWidget> createState() => _IndexState();
67
}
@@ -17,9 +18,95 @@ class _IndexState extends State<Index> {
1718
appBar: AppBar(
1819
title: Text('LongPressDraggable'),
1920
),
20-
body: Center(
21-
child: Text('更新中'),
21+
body: GridView.count(
22+
childAspectRatio: 1.5,
23+
crossAxisCount: 3,
24+
children: _renderWidget(),
25+
padding: EdgeInsets.only(top: 10),
2226
),
2327
);
2428
}
29+
30+
List<Widget> _renderWidget() {
31+
final List list = List<Widget>();
32+
for (int i = 0; i < widget._list.length; i++) {
33+
list.add(_buildDragWidget(i));
34+
}
35+
return list;
36+
}
37+
38+
Widget _buildDragWidget(int index) {
39+
return LongPressDraggable(
40+
data: index,
41+
feedbackOffset: Offset(0, 0),
42+
// axis: Axis.vertical,
43+
child: DragTarget<int>(
44+
onAccept: (data) {
45+
print('accept index $data');
46+
setState(() {
47+
final temp = widget._list[data];
48+
widget._list.remove(temp);
49+
widget._list.insert(index, temp);
50+
});
51+
},
52+
onWillAccept: (data) {
53+
print("current index: $index, on will accept $data");
54+
return true;
55+
},
56+
onLeave: (data) {
57+
print('index $index , leave $data');
58+
},
59+
builder: (context, data, rejects) {
60+
print('builder = $data, rejects= $rejects');
61+
return Container(
62+
margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
63+
alignment: Alignment.center,
64+
decoration: BoxDecoration(
65+
borderRadius: BorderRadius.circular(4),
66+
border: Border.all(
67+
color: Colors.grey,
68+
),
69+
),
70+
child: Text('index = ${widget._list[index]}'),
71+
);
72+
},
73+
),
74+
feedback: SizedBox(
75+
width: 80,
76+
height: 40,
77+
child: Container(
78+
alignment: Alignment.center,
79+
decoration: BoxDecoration(
80+
borderRadius: BorderRadius.circular(4),
81+
border: Border.all(
82+
color: Colors.grey,
83+
),
84+
),
85+
child: Text(
86+
'index=${widget._list[index]}',
87+
style: TextStyle(
88+
fontSize: 12,
89+
),
90+
),
91+
),
92+
),
93+
onDragCompleted: () {
94+
print('onDragCompleted');
95+
},
96+
onDragEnd: (details) {
97+
print(
98+
'onDragEnd ${details.wasAccepted} ${details.velocity} ${details.offset}');
99+
},
100+
onDragStarted: () {
101+
print('onDragStarted');
102+
},
103+
// 拖动时替换原来位置的widget
104+
// childWhenDragging: Center(
105+
// child: Icon(
106+
// Icons.ac_unit,
107+
// color: Colors.redAccent,
108+
// ),
109+
// ),
110+
);
111+
}
25112
}

lib/widget/gestures/longpressdraggable/index.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:efox_flutter/components/widgetComp.dart' as WidgetComp;
3-
// import 'demo.dart' as Demo;
3+
import 'demo.dart' as Demo;
44

55
class Index extends StatefulWidget {
66
static String title = 'LongPressDraggable';
@@ -19,7 +19,7 @@ class _IndexState extends State<Index> {
1919
originCodeUrl: Index.originCodeUrl,
2020
mdUrl: Index.mdUrl,
2121
demoChild: [
22-
// Demo.Index(),
22+
Demo.Index(),
2323
],
2424
);
2525
}

0 commit comments

Comments
 (0)