注意:無特殊說明,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==
固然我也很是但願您關注我我的的公衆號,裏面有各類福利等着你們哦。