在Flutter上如何实现按钮的拖拽效果

1、使用 DraggableDragTarget 配合一起使用

Draggable 定义可拖拽对象和拖动时,拖动对象的样子

DragTarget 定义拖拽后接收对象,可拿到Draggable携带的数据

Dart 复制代码
import 'package:flutter/material.dart';

class Test extends StatefulWidget {
  const Test({super.key});

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {
  Color curColor = Colors.orange;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Draggable<Color>(
          data: Colors.blue,
          feedback: Container(
            width: 50,
            height: 50,
            color: Colors.blue,
          ),
          child: Container(
            width: 50,
            height: 50,
            color: Colors.blue,
          ),
        ),
        Draggable<Color>(
          data: Colors.yellow,
          feedback: Container(
            width: 50,
            height: 50,
            color: Colors.yellow,
          ),
          child: Container(
            width: 50,
            height: 50,
            color: Colors.yellow,
          ),
        ),
        Draggable<Color>(
          data: Colors.red,
          feedback: Container(
            width: 50,
            height: 50,
            color: Colors.red,
          ),
          child: Container(
            width: 50,
            height: 50,
            color: Colors.red,
          ),
        ),
        DragTarget<Color>(
          onAcceptWithDetails: (DragTargetDetails c) {
            print(c);
            setState(() {
              curColor = c.data as Color;
            });
          },
          builder: (context, _, __) {
            return Container(
              width: 50,
              height: 50,
              color: curColor,
              alignment: Alignment.center,
              child: const Text('拖放到这里'),
            );
          },
        ),
      ],
    );
  }
}

特点​:

  • 支持拖拽数据传递(通过data参数)。
  • 提供完整的拖拽生命周期回调(如onDragStartedonDragEnd

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

Dart 复制代码
import 'package:flutter/material.dart';

class DraggableButton extends StatefulWidget {
  const DraggableButton({super.key});

  @override
  State<DraggableButton> createState() => _DraggableButtonState();
}

class _DraggableButtonState extends State<DraggableButton> {
  Offset _offset = Offset.zero;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: 400,
      child: Stack(
        children: [
          Positioned(
            left: _offset.dx,
            top: _offset.dy,
            child: GestureDetector(
              onPanUpdate: (details) {
                setState(() {
                  _offset += details.delta; // 更新偏移量
                });
              },
              child: FloatingActionButton(
                onPressed: () {},
                child: const Icon(Icons.drag_handle),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

第三、使用第三方库

flutter_draggable_gridview | Flutter package

draggable_float_widget | Flutter package

draggable_home | Flutter package

相关推荐
Jackson__10 分钟前
聊一下HTTP 与 HTTPS 的区别,以及HTTPS 的加密方式
前端·面试
一抓掉一大把23 分钟前
MiniExcel模板填充Excel导出
开发语言·javascript·ecmascript
好运yoo33 分钟前
npm install的原理
前端·npm
Jiaberrr38 分钟前
uniapp 安卓 APP 后台持续运行(保活)的尝试办法
android·前端·javascript·uni-app·app·保活
不老刘38 分钟前
uniapp+vue3实现CK通信协议(基于jjc-tcpTools)
前端·javascript·uni-app
蓝婷儿1 小时前
第二章支线八 ·CSS终式:Tailwind与原子风暴
前端·css
vanora11111 小时前
Vue在线预览excel、word、ppt等格式数据。
前端·javascript·vue.js
树上有只程序猿1 小时前
低代码不是炫技,而是回归需求的必然答案
前端
比特森林探险记1 小时前
Go 中 map 的双值检测写法详解
java·前端·golang
溪饱鱼1 小时前
React源码阅读-fiber核心构建原理
前端·javascript·react.js