label_drag_target_pannel.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'package:fis_measure/interfaces/enums/annotation.dart';
  2. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  3. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  4. import 'package:fis_measure/view/gesture/mouse_gesture.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:get/get.dart';
  7. import '../positioned_cursor.dart';
  8. /// 文本注释拖拽目标面板
  9. class AnnotationLabelDragTargetPanel extends StatefulWidget {
  10. const AnnotationLabelDragTargetPanel({Key? key}) : super(key: key);
  11. @override
  12. State<StatefulWidget> createState() => _PanelState();
  13. }
  14. class _PanelState extends State<AnnotationLabelDragTargetPanel> {
  15. late final application = Get.find<IApplication>();
  16. final mouseState = Get.find<IMouseState>();
  17. CursorDisplayType displayType = CursorDisplayType.none;
  18. @override
  19. void initState() {
  20. super.initState();
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return DragTarget<String>(
  25. builder: (context, candidateData, rejectedData) {
  26. return GestureDetector(
  27. onPanDown: (details) {
  28. application.createPointInfo(
  29. details.localPosition,
  30. PointInfoType.mouseDown,
  31. );
  32. application.switchAnnotation(AnnotationType.input);
  33. },
  34. child: MouseRegion(
  35. cursor: SystemMouseCursors.none,
  36. onHover: (event) {
  37. mouseState.mousePosition = event.localPosition;
  38. },
  39. onEnter: (e) {
  40. setState(() {
  41. displayType = CursorDisplayType.normal;
  42. });
  43. },
  44. onExit: (e) {
  45. setState(() {
  46. displayType = CursorDisplayType.none;
  47. });
  48. },
  49. child: Stack(
  50. children: [
  51. _buildCursor(),
  52. ],
  53. ),
  54. ),
  55. );
  56. },
  57. onMove: (details) {
  58. _notifyPosition(context, details.offset, PointInfoType.mouseMove);
  59. },
  60. onAcceptWithDetails: (details) {
  61. _notifyPosition(context, details.offset, PointInfoType.mouseUp);
  62. },
  63. );
  64. }
  65. void _notifyPosition(
  66. BuildContext context,
  67. Offset offset,
  68. PointInfoType type,
  69. ) {
  70. final localOffset = _findLocalPosition(context, offset);
  71. mouseState.mousePosition = localOffset;
  72. application.createPointInfo(localOffset, type);
  73. }
  74. Offset _findLocalPosition(BuildContext context, Offset globalPosition) {
  75. final renderBox = context.findRenderObject() as RenderBox?;
  76. if (renderBox == null) return globalPosition;
  77. return renderBox.globalToLocal(globalPosition);
  78. }
  79. Widget _buildCursor() {
  80. switch (displayType) {
  81. case CursorDisplayType.none:
  82. return Container();
  83. case CursorDisplayType.normal:
  84. return const PositionedCursor();
  85. default:
  86. return const PositionedCursor();
  87. }
  88. }
  89. @override
  90. void dispose() {
  91. super.dispose();
  92. }
  93. }