在Flutter上怎样实现按钮的拖拽效果

[复制链接]
发表于 2025-9-5 06:30:59 来自手机 | 显示全部楼层 |阅读模式
1、使用 Draggable 和 DragTarget 配合一起使用


   Draggable 界说可拖拽对象和拖动时,拖动对象的样子
  DragTarget 界说拖拽后接收对象,可拿到Draggable携带的数据
  1. import 'package:flutter/material.dart';
  2. class Test extends StatefulWidget {
  3.   const Test({super.key});
  4.   @override
  5.   State<Test> createState() => _TestState();
  6. }
  7. class _TestState extends State<Test> {
  8.   Color curColor = Colors.orange;
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return Column(
  12.       children: [
  13.         Draggable<Color>(
  14.           data: Colors.blue,
  15.           feedback: Container(
  16.             width: 50,
  17.             height: 50,
  18.             color: Colors.blue,
  19.           ),
  20.           child: Container(
  21.             width: 50,
  22.             height: 50,
  23.             color: Colors.blue,
  24.           ),
  25.         ),
  26.         Draggable<Color>(
  27.           data: Colors.yellow,
  28.           feedback: Container(
  29.             width: 50,
  30.             height: 50,
  31.             color: Colors.yellow,
  32.           ),
  33.           child: Container(
  34.             width: 50,
  35.             height: 50,
  36.             color: Colors.yellow,
  37.           ),
  38.         ),
  39.         Draggable<Color>(
  40.           data: Colors.red,
  41.           feedback: Container(
  42.             width: 50,
  43.             height: 50,
  44.             color: Colors.red,
  45.           ),
  46.           child: Container(
  47.             width: 50,
  48.             height: 50,
  49.             color: Colors.red,
  50.           ),
  51.         ),
  52.         DragTarget<Color>(
  53.           onAcceptWithDetails: (DragTargetDetails c) {
  54.             print(c);
  55.             setState(() {
  56.               curColor = c.data as Color;
  57.             });
  58.           },
  59.           builder: (context, _, __) {
  60.             return Container(
  61.               width: 50,
  62.               height: 50,
  63.               color: curColor,
  64.               alignment: Alignment.center,
  65.               child: const Text('拖放到这里'),
  66.             );
  67.           },
  68.         ),
  69.       ],
  70.     );
  71.   }
  72. }
复制代码
特点​:


  • 支持拖拽数据通报(通过data参数)。
  • 提供完备的拖拽生命周期回调(如onDragStarted、onDragEnd

方式二:使用GestureDetector 的onPanUpdate函数来监听 移动

  1. import 'package:flutter/material.dart';
  2. class DraggableButton extends StatefulWidget {
  3.   const DraggableButton({super.key});
  4.   @override
  5.   State<DraggableButton> createState() => _DraggableButtonState();
  6. }
  7. class _DraggableButtonState extends State<DraggableButton> {
  8.   Offset _offset = Offset.zero;
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return SizedBox(
  12.       width: double.infinity,
  13.       height: 400,
  14.       child: Stack(
  15.         children: [
  16.           Positioned(
  17.             left: _offset.dx,
  18.             top: _offset.dy,
  19.             child: GestureDetector(
  20.               onPanUpdate: (details) {
  21.                 setState(() {
  22.                   _offset += details.delta; // 更新偏移量
  23.                 });
  24.               },
  25.               child: FloatingActionButton(
  26.                 onPressed: () {},
  27.                 child: const Icon(Icons.drag_handle),
  28.               ),
  29.             ),
  30.           ),
  31.         ],
  32.       ),
  33.     );
  34.   }
  35. }
复制代码
第三、使用第三方库

flutter_draggable_gridview | Flutter package
draggable_float_widget | Flutter package
draggable_home | Flutter package

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表