positioned_cursor.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import 'package:fis_common/event/event_type.dart';
  2. import 'package:fis_measure/values/colors.dart';
  3. import 'package:fis_measure/view/cursor.dart';
  4. import 'package:fis_measure/view/gesture/cross_position_indicator.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:get/get.dart';
  7. abstract class IMouseState {
  8. Offset? get mousePosition;
  9. set mousePosition(Offset? value);
  10. Offset? get cursorPosition;
  11. MeasureCursorType get cursorType;
  12. set cursorType(MeasureCursorType value);
  13. double get cursorSize;
  14. set cursorSize(double value);
  15. /// 卡尺缩放比
  16. double get cursorScaleRatio;
  17. set cursorScaleRatio(double value);
  18. late FEventHandler<Offset?> mousePositionChanged;
  19. late FEventHandler<MeasureCursorType> cursorTypeChanged;
  20. late FEventHandler<double> cursorSizeChanged;
  21. late FEventHandler<double> cursorScaleRatioChanged;
  22. late FEventHandler<CrossIndicatorStyle> crossIndicatorStyleChanged;
  23. }
  24. class MouseState implements IMouseState {
  25. Offset? _mousePosition;
  26. double _cursorSize = 16;
  27. double _cursorScaleRatio = 1;
  28. MeasureCursorType _cursorType = MeasureCursorType.cursor01;
  29. @override
  30. var mousePositionChanged = FEventHandler<Offset?>();
  31. @override
  32. var cursorSizeChanged = FEventHandler<double>();
  33. @override
  34. var cursorScaleRatioChanged = FEventHandler<double>();
  35. @override
  36. var cursorTypeChanged = FEventHandler<MeasureCursorType>();
  37. @override
  38. var crossIndicatorStyleChanged = FEventHandler<CrossIndicatorStyle>();
  39. @override
  40. Offset? get mousePosition => _mousePosition;
  41. @override
  42. set mousePosition(Offset? value) {
  43. if (value != _mousePosition) {
  44. _mousePosition = value;
  45. mousePositionChanged.emit(this, value);
  46. }
  47. }
  48. @override
  49. double get cursorSize => _cursorSize;
  50. @override
  51. set cursorSize(double value) {
  52. if (value != _cursorSize) {
  53. _cursorSize = value;
  54. cursorSizeChanged.emit(this, value);
  55. }
  56. }
  57. @override
  58. double get cursorScaleRatio => _cursorScaleRatio;
  59. @override
  60. set cursorScaleRatio(double value) {
  61. if (value != _cursorScaleRatio) {
  62. _cursorScaleRatio = value;
  63. cursorScaleRatioChanged.emit(this, value);
  64. }
  65. }
  66. @override
  67. MeasureCursorType get cursorType => _cursorType;
  68. @override
  69. set cursorType(MeasureCursorType value) {
  70. if (value != _cursorType) {
  71. _cursorType = value;
  72. cursorTypeChanged.emit(this, value);
  73. }
  74. }
  75. @override
  76. Offset? get cursorPosition {
  77. if (mousePosition == null) return null;
  78. double dx = mousePosition!.dx, dy = mousePosition!.dy;
  79. final offset = cursorScaleRatio * cursorSize / 2;
  80. dx -= offset;
  81. dy -= offset;
  82. return Offset(dx, dy);
  83. }
  84. }
  85. class PositionedCursor extends StatefulWidget {
  86. const PositionedCursor({Key? key}) : super(key: key);
  87. @override
  88. State<StatefulWidget> createState() => _PositionedCursorState();
  89. }
  90. class _PositionedCursorState extends State<PositionedCursor> {
  91. final mouseState = Get.find<IMouseState>();
  92. @override
  93. void initState() {
  94. mouseState.cursorSizeChanged.addListener(onCursorSizeChanged);
  95. mouseState.cursorScaleRatioChanged.addListener(onCursorScaleRatioChanged);
  96. mouseState.cursorTypeChanged.addListener(cursorTypeChanged);
  97. mouseState.mousePositionChanged.addListener(mousePositionChanged);
  98. super.initState();
  99. }
  100. @override
  101. void dispose() {
  102. mouseState.cursorSizeChanged.removeListener(onCursorSizeChanged);
  103. mouseState.cursorScaleRatioChanged
  104. .removeListener(onCursorScaleRatioChanged);
  105. mouseState.cursorTypeChanged.removeListener(cursorTypeChanged);
  106. mouseState.mousePositionChanged.removeListener(mousePositionChanged);
  107. super.dispose();
  108. }
  109. @override
  110. Widget build(BuildContext context) {
  111. final position = mouseState.cursorPosition;
  112. if (position == null) return Container();
  113. return Positioned(
  114. left: position.dx,
  115. top: position.dy,
  116. child: MeasureCursor(
  117. type: mouseState.cursorType,
  118. size: mouseState.cursorSize * mouseState.cursorScaleRatio,
  119. color: MeasureColors.Primary,
  120. ),
  121. );
  122. }
  123. void onCursorSizeChanged(Object sender, dynamic e) {
  124. onUpdate();
  125. }
  126. void onCursorScaleRatioChanged(Object sender, dynamic e) {
  127. onUpdate();
  128. }
  129. void cursorTypeChanged(Object sender, dynamic e) {
  130. onUpdate();
  131. }
  132. void mousePositionChanged(Object sender, dynamic e) {
  133. onUpdate();
  134. }
  135. void onUpdate() {
  136. setState(() {});
  137. }
  138. }