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(),
          ),
        ),
      ],
    );
  }
}
相关推荐
m0_7482478016 小时前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
迷雾漫步者17 小时前
Flutter组件————PageView
flutter·跨平台·dart
迷雾漫步者1 天前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig1 天前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜1 天前
flutter自学笔记5- dart 编码规范
flutter·dart
恋猫de小郭1 天前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
sunly_2 天前
Flutter:导航,tab切换,顶部固定,列表分页滚动
开发语言·javascript·flutter
敲代码的小强2 天前
Flutter项目兼容鸿蒙Next系统
flutter·华为·harmonyos
Zh-jie3 天前
flutter 快速实现侧边栏
前端·javascript·flutter
truemi.734 天前
flutter --no-color pub get 超时解决方法
android·flutter