Flutter Widget 之 Draggable 与 DragTarget

280 阅读1分钟

Draggable 顾名思义 可拖拽的 。DragTarget 则是拖拽目标。两个widget联合使用 可以实现 将一个View 拖到另一个View中的效果,二者都是容器组件。

Draggable<T> // 范型T 代表数据类型
const Draggable({
  Key key,
  @required this.child, // 未拖动情况下的视图
  @required this.feedback, // 拖出的视图
 this.childWhenDragging, // 拖出后留在原地的视图

 this.data, // 拖拽的数据
 this.axis, // Axis.vertical Axis.horizontal 默认是任意方向  
 this.feedbackOffset = Offset.zero, // 偏移量  this.dragAnchor = DragAnchor.child, // DragAnchor.pointer // 
  this.affinity,
  this.maxSimultaneousDrags,
  this.onDragStarted, // 拖拽开始
  this.onDraggableCanceled, // 拖拽取消回调
  this.onDragEnd, // 拖拽结束
  this.onDragCompleted,    // 拖拽完成回调
  this.ignoringFeedbackSemantics = true,
})DragTarget<T>
const DragTarget({
  Key key,  @required this.builder, // 视图构建器
  this.onWillAccept, // 已经悬停在上方 手指未抬起的回调
  this.onAccept, // 已经悬停在上方,手指释放
  this.onLeave, // 拖动离开上方的回调
})// builder的类型
typedef DragTargetBuilder<T> = Widget Function(
    BuildContext context, // 上下文
    List<T> candidateData, // 接受候选数据
    List<dynamic> rejectedData // 明确拒绝的数据
);