positioned_touch_cursor.dart 4.6 KB

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