Draggable(
child: Container(
height: 100,width: 100,alignment: Alignment.center,decoration: BoxDecoration(
color: Colors.red,borderRadius: BorderRadius.circular(10)
),child: Text('孟',style: TextStyle(color: Colors.white,fontSize: 18),),feedback: Container(
height: 100,decoration: BoxDecoration(
color: Colors.blue,)
Draggable(
childWhenDragging: Container(
height: 100,decoration: BoxDecoration(
color: Colors.grey,borderRadius: BorderRadius.circular(10)),child: Text(
'孟',...
)
Draggable(
axis: Axis.vertical,...
)
Draggable(
onDragStarted: (){
print('onDragStarted');
},onDragEnd: (DraggableDetails details){
print('onDragEnd:$details');
},onDraggableCanceled: (Velocity velocity,Offset offset){
print('onDraggableCanceled velocity:$velocity,offset:$offset');
},onDragCompleted: (){
print('onDragCompleted');
},...
)
- onDragStarted:开始拖动时回调。
- onDragEnd:拖动结束时回调。
- onDraggableCanceled:未拖动到DragTarget控件上时回调。
- onDragCompleted:拖动到DragTarget控件上时回调。
DragTarget(
builder: (BuildContext context,List<dynamic> candidateData,List<dynamic> rejectedData) {
...
}
)
- onWillAccept:拖到该控件上时调用,需要返回true或者false,返回true,松手后会回调onAccept,否则回调onLeave。
- onAccept:onWillAccept返回true时,用户松手后调用。
- onLeave:onWillAccept返回false时,用户松手后调用。
var _dragData;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
_buildDraggable(),SizedBox(
height: 200,DragTarget<Color>(
builder: (BuildContext context,List<Color> candidateData,List<dynamic> rejectedData) {
print('candidateData:$candidateData,rejectedData:$rejectedData');
return _dragData == null
? Container(
height: 100,decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),border: Border.all(color: Colors.red)),)
: Container(
height: 100,decoration: BoxDecoration(
color: Colors.red,child: Text(
'孟',);
},onWillAccept: (Color color) {
print('onWillAccept:$color');
return true;
},onAccept: (Color color) {
setState(() {
_dragData = color;
});
print('onAccept:$color');
},onLeave: (Color color) {
print('onLeave:$color');
},],);
}
_buildDraggable() {
return Draggable(
data: Color(0x000000FF),child: Container(
height: 100,decoration: BoxDecoration(
color: Colors.red,child: Text(
'孟',feedback: Container(
height: 100,decoration: BoxDecoration(
color: Colors.blue,child: DefaultTextStyle.merge(
style: TextStyle(color: Colors.white,child: Text(
'孟',);
}