在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

相关推荐
Zestia13 分钟前
页面点击跳转源代码?——element-jumper插件实现
前端·javascript
前端小白199513 分钟前
面试取经:工程化篇-webpack性能优化之优化loader性能
前端·面试·前端工程化
PineappleCoder13 分钟前
大小写 + 标点全搞定!JS 如何精准统计单词频率?
前端·javascript·算法
zhangbao90s14 分钟前
Web组件:使用Shadow DOM
前端
hhy前端之旅15 分钟前
语义版本控制:掌握版本管理的艺术
前端
coding随想15 分钟前
深入浅出DOM操作的隐藏利器:Range(范围)对象——掌控文档的“手术刀”
前端
前端小白199515 分钟前
面试取经:工程化篇-webpack性能优化之减少模块解析
前端·面试·前端工程化
一枚前端小能手16 分钟前
🏗️ 项目越来越大维护不动了,微前端架构了解一下
前端
KasukabeTsumugi16 分钟前
TypeScript:联合类型可以转化为元组类型吗?数组如何用联合类型逐项约束?
javascript
文艺理科生24 分钟前
Nuxt.js入门指南-Vue生态下的高效渲染技术
前端·vue.js·nuxt.js