注意:无特殊说明,Flutter版本及Dart版本以下:微信
- Flutter版本: 1.12.13+hotfix.5
- Dart版本: 2.7.0
Draggable系列组件可让咱们拖动组件。ide
Draggable组件有2个必须填写的参数,child
参数是子控件,feedback
参数是拖动时跟随移动的组件,用法以下:学习
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, width: 100, alignment: Alignment.center, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(10) ), child: Text('孟',style: TextStyle(color: Colors.white,fontSize: 18),), ), )
效果以下:ui
蓝色的组件是feedback
,若是想在拖动的时候子组件显示其余样式可使用childWhenDragging
参数,用法以下:code
Draggable( childWhenDragging: Container( height: 100, width: 100, alignment: Alignment.center, decoration: BoxDecoration( color: Colors.grey, borderRadius: BorderRadius.circular(10)), child: Text( '孟', style: TextStyle(color: Colors.white, fontSize: 18), ), ), ... )
效果以下:blog
咱们还能够控制拖动的方向,好比只容许垂直方向移动,代码以下:继承
Draggable( axis: Axis.vertical, ... )
Draggable组件为咱们提供了4中拖动过程当中的回调事件,用法以下:事件
Draggable( onDragStarted: (){ print('onDragStarted'); }, onDragEnd: (DraggableDetails details){ print('onDragEnd:$details'); }, onDraggableCanceled: (Velocity velocity, Offset offset){ print('onDraggableCanceled velocity:$velocity,offset:$offset'); }, onDragCompleted: (){ print('onDragCompleted'); }, ... )
说明以下:ci
Draggable有一个data
参数,这个参数是和DragTarget配合使用的,当用户将控件拖动到DragTarget时此数据会传递给DragTarget。get
DragTarget就像他的名字同样,指定一个目的地,Draggable组件能够拖动到此控件,用法以下:
DragTarget( builder: (BuildContext context, List<dynamic> candidateData, List<dynamic> rejectedData) { ... } )
当onWillAccept
返回true时, candidateData
参数的数据是Draggable的data
数据。
当onWillAccept
返回false时, rejectedData
参数的数据是Draggable的data
数据,
DragTarget有3个回调,说明以下:
用法以下:
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, width: 100, alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), border: Border.all(color: Colors.red)), ) : 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), ), ); }, 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, 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, width: 100, alignment: Alignment.center, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(10)), child: DefaultTextStyle.merge( style: TextStyle(color: Colors.white, fontSize: 18), child: Text( '孟', ), ), ), ); }
效果以下:
LongPressDraggable继承自Draggable,所以用法和Draggable彻底同样,惟一的区别就是LongPressDraggable触发拖动的方式是长按,而Draggable触发拖动的方式是按下。
今天的文章对你们是否有帮助?若是有,请在文章底部留言和点赞,以表示对个人支持,大家的留言、点赞和转发关注是我持续更新的动力!
我建立了一个关于Flutter的微信交流群,欢迎您的加入,让咱们一块儿学习,一块儿进步,开始咱们的故事,生活不止眼前的苟且,还有诗和《远方》。
==微信:mqd_zzy==
固然我也很是但愿您关注我我的的公众号,里面有各类福利等着你们哦。