在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

相关推荐
牧羊狼的狼3 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手4 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one4 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
魔云连洲4 小时前
深入解析:Vue与React的异步批处理更新机制
前端·vue.js·react.js
mCell5 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
weixin_437830946 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
超级无敌攻城狮6 小时前
3 分钟学会!波浪文字动画超详细教程,从 0 到 1 实现「思考中 / 加载中」高级效果
前端
excel7 小时前
用 TensorFlow.js Node 实现猫图像识别(教学版逐步分解)
前端
gnip7 小时前
JavaScript事件流
前端·javascript
小菜全8 小时前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json