在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

相关推荐
夏梦春蝉1 小时前
ES6从入门到精通:模块化
前端·ecmascript·es6
拓端研究室2 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
工一木子3 小时前
URL时间戳参数深度解析:缓存破坏与前端优化的前世今生
前端·缓存
半点寒12W4 小时前
微信小程序实现路由拦截的方法
前端
某公司摸鱼前端5 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
要加油哦~5 小时前
vue | 插件 | 移动文件的插件 —— move-file-cli 插件 的安装与使用
前端·javascript·vue.js
小林学习编程5 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏5 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083166 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
ai小鬼头7 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github