5自己建网站,wordpress延迟加载,十大营销网站,wordpress文章网格文章目录 1. DragTarget 控件的构造函数主要参数#xff1a; 2. DragTarget 的工作原理3. 常见用法示例 1#xff1a;实现一个简单的拖拽目标解释#xff1a;示例 2#xff1a;与 Draggable 结合使用解释#xff1a; 4. DragTarget 的回调详解5. 总结 DragTarget 是 Flutt… 文章目录 1. DragTarget 控件的构造函数主要参数 2. DragTarget 的工作原理3. 常见用法示例 1实现一个简单的拖拽目标解释示例 2与 Draggable 结合使用解释 4. DragTarget 的回调详解5. 总结 DragTarget 是 Flutter 中用于处理拖拽操作的控件它定义了一个区域允许你将其他控件通常是通过
Draggable 控件实现的可拖拽物体拖拽到该区域并在拖拽结束时根据情况更新该区域的内容或执行其他操作。 1. DragTarget 控件的构造函数
DragTargetT({Key? key,required WillAcceptT onWillAccept, // 拖拽对象是否可以接受required void Function(DragTargetDetailsT details) onAccept, // 拖拽对象被接受时执行的回调void Function(DragTargetDetailsT details)? onEnter, // 拖拽对象进入目标区域时执行的回调void Function(DragTargetDetailsT details)? onLeave, // 拖拽对象离开目标区域时执行的回调void Function(DragTargetDetailsT details)? onCancel, // 拖拽对象取消时执行的回调double? elevation, // 控件的阴影高度Color? color, // 控件的背景色TextStyle? textStyle, // 控件的文本样式bool? feedback, // 控件是否启用反馈效果
})主要参数
onWillAccept一个函数决定拖拽的对象是否可以接受。返回 true 或 false。onAccept当拖拽对象被接受时触发的回调函数。接收一个 DragTargetDetails 参数包含了拖拽的数据。onEnter当拖拽对象进入 DragTarget 区域时调用。接收 DragTargetDetails可以用来更新界面如改变颜色。onLeave当拖拽对象离开 DragTarget 区域时调用。onCancel当拖拽对象被取消时触发用来做一些撤销操作。elevation用于设置 DragTarget 区域的阴影效果通常影响视觉层次感。color设置 DragTarget 的背景颜色。textStyle设置文本样式。feedback控制是否显示拖拽反馈。 2. DragTarget 的工作原理 DragTarget 负责接收从其他控件如 Draggable拖拽过来的数据并决定是否处理这些数据。它通过回调函数来响应拖拽事件。 拖拽的过程包括 拖拽对象进入 DragTarget 区域。拖拽对象悬停在目标区域内。拖拽对象离开目标区域。拖拽对象被放置到目标区域内。 onWillAccept 用来判断拖拽对象是否可以被接受onEnter 和 onLeave 用来响应拖拽对象进入或离开区域的动作onAccept 则在拖拽成功放置后触发。 3. 常见用法
示例 1实现一个简单的拖拽目标
import package:flutter/material.dart;void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(appBar: AppBar(title: Text(DragTarget Example)),body: Center(child: DragTargetint(onWillAccept: (data) data ! null, // 允许接收非空的数据onAccept: (data) {ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(Received data: $data)),);},builder: (context, candidateData, rejectedData) {return Container(width: 200,height: 200,color: Colors.blue,child: Center(child: Text(Drag here)),);},),),),);}
}解释
在这个简单的例子中DragTargetint 用来接收类型为 int 的数据。onWillAccept 判断是否接受数据如果数据不为 null 则返回 true。onAccept 被调用时显示一个 SnackBar显示接收到的数据。builder 构建 DragTarget 区域的 UI在这个例子中是一个蓝色的方形区域。 示例 2与 Draggable 结合使用
import package:flutter/material.dart;void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(appBar: AppBar(title: Text(DragTarget with Draggable)),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Draggableint(data: 42, // 传递的数据child: CircleAvatar(radius: 50.0,child: Text(Drag Me),),feedback: Material(color: Colors.transparent,child: CircleAvatar(radius: 50.0,backgroundColor: Colors.blue,child: Text(Dragging),),),childWhenDragging: CircleAvatar(radius: 50.0,backgroundColor: Colors.grey,child: Text(Gone),),),SizedBox(height: 50),DragTargetint(onWillAccept: (data) data ! null, // 允许接收数据onAccept: (data) {ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(Dropped: $data)),);},builder: (context, candidateData, rejectedData) {return Container(width: 200,height: 200,color: Colors.orange,child: Center(child: Text(Drop Here)),);},),],),),),);}
}解释
在此示例中Draggable 控件创建了一个可拖拽的圆形用户可以将它拖拽到下方的 DragTarget 区域。Draggable 的 data 参数传递了一个整数42。当拖拽对象被放置到 DragTarget 区域时onAccept 回调被触发显示一个 SnackBar告知接收到的数据。 4. DragTarget 的回调详解 onWillAccept此回调决定是否接受拖拽的对象。它接收拖拽的数据作为参数返回一个布尔值表示是否接受该数据。如果返回 true表示可以放置数据否则数据将被拒绝。 onEnter此回调在拖拽对象进入 DragTarget 区域时触发可以用来改变目标区域的样式例如改变颜色提示用户目标区域准备好接受拖拽对象。 onLeave此回调在拖拽对象离开 DragTarget 区域时触发可以用来恢复区域的样式。 onAccept此回调在拖拽对象成功放置在目标区域时触发可以用于处理拖拽成功后需要执行的逻辑例如更新界面、保存数据等。 onCancel当拖拽操作被取消时例如拖拽对象超出了目标区域并没有被放置会调用该回调。 5. 总结
DragTarget 是 Flutter 中处理拖拽操作的控件提供了多种回调函数来控制拖拽行为。它与 Draggable 控件配合使用实现复杂的拖拽交互。DragTarget 支持灵活的 UI 设计可以根据拖拽状态动态更新 UI提供良好的用户体验。 结束语 Flutter是一个由Google开发的开源UI工具包它可以让您在不同平台上创建高质量、美观的应用程序而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧从UI设计到性能优化欢饮关注一起讨论学习共同进入Flutter的精彩世界
文章转载自: http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.ydnxm.cn.gov.cn.ydnxm.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.jjnry.cn.gov.cn.jjnry.cn http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.nqmdc.cn.gov.cn.nqmdc.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn