Flutter自定义可拖动组件

c 复制代码
 WidgetsBinding.instance.addPostFrameCallback((_) async {
      Overlay.of(context)?.insert(_entry());
    });
c 复制代码
  OverlayEntry _entry() {
    return OverlayEntry(builder: (ctx) {
      return DraggableComponentOrderStatuWidget(
          Offset(ctx.width - 100, ctx.height / 3));
    });
  }
c 复制代码
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:upower/common/style/common_style.dart';

class DraggableComponentOrderStatuWidget extends StatefulWidget {
  DraggableComponentOrderStatuWidget(this.offsetParam, {super.key});

  Offset offsetParam;

  @override
  State<DraggableComponentOrderStatuWidget> createState() =>
      _DraggableComponentOrderStatuWidgetState();
}

class _DraggableComponentOrderStatuWidgetState
    extends State<DraggableComponentOrderStatuWidget> {
  Offset? _offset;

  Widget _getContentWidget() {
    return Container(
      alignment: Alignment.center,
      width: 100,
      height: 30,
      decoration: const BoxDecoration(
        color: ColorStyle.primaryC2,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(15), bottomLeft: Radius.circular(15)),
      ),
      child: Text(
        "租赁中",
        style: TextStyle(
            fontSize: 12.sp,
            color: Colors.white,
            decoration: TextDecoration.none),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _offset = widget.offsetParam;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          right: 0,
          top: _offset?.dy,
          child: Draggable(
            feedback: _getContentWidget(),
            childWhenDragging: Text(""), // 设置为null以隐藏原始小部件
            onDraggableCanceled: (velocity, offset) {
              setState(() {
                _offset = offset;
              });
            },
            child: _getContentWidget(),
          ),
        ),
      ],
    );
  }
}
相关推荐
君蓦3 小时前
Flutter 本地存储与数据库的使用和优化
flutter
problc12 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
lqj_本人21 小时前
鸿蒙next选择 Flutter 开发跨平台应用的原因
flutter·华为·harmonyos
lqj_本人1 天前
Flutter&鸿蒙next 状态管理框架对比分析
flutter·华为·harmonyos
起司锅仔1 天前
Flutter启动流程(2)
flutter
hello world smile1 天前
最全的Flutter中pubspec.yaml及其yaml 语法的使用说明
android·前端·javascript·flutter·dart·yaml·pubspec.yaml
lqj_本人1 天前
Flutter 的 Widget 概述与常用 Widgets 与鸿蒙 Next 的对比
flutter·harmonyos
iFlyCai1 天前
极简实现酷炫动效:Flutter隐式动画指南第二篇之一些酷炫的隐式动画效果
flutter
lqj_本人1 天前
Flutter&鸿蒙next 中使用 MobX 进行状态管理
flutter·华为·harmonyos