cursor.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import 'package:fis_ui/define.dart';
  2. import 'package:flutter/material.dart';
  3. enum MeasureCursorType {
  4. cursor01,
  5. cursor02,
  6. cursor03,
  7. cursor04,
  8. cursor05,
  9. }
  10. class MeasureCursor extends StatelessWidget implements FWidget {
  11. const MeasureCursor({
  12. Key? key,
  13. required this.type,
  14. required this.size,
  15. this.color,
  16. }) : super(key: key);
  17. final double size;
  18. final MeasureCursorType type;
  19. final Color? color;
  20. @override
  21. Widget build(BuildContext context) {
  22. return SizedBox(
  23. width: size,
  24. height: size,
  25. child: RepaintBoundary(
  26. child: CustomPaint(painter: _CursorPainter(type, color: color)),
  27. ),
  28. );
  29. }
  30. }
  31. class _CursorPainter extends CustomPainter {
  32. const _CursorPainter(this.type, {this.color});
  33. final MeasureCursorType type;
  34. final Color? color;
  35. @override
  36. void paint(Canvas canvas, Size size) {
  37. final double width = size.width;
  38. final double offset = width / 2;
  39. // position center point
  40. canvas.translate(offset, offset);
  41. _CursorPathGeometry(
  42. cursorType: type,
  43. offset: offset,
  44. color: color,
  45. ).draw(canvas);
  46. }
  47. @override
  48. bool shouldRepaint(covariant _CursorPainter oldDelegate) {
  49. return oldDelegate.color != color || oldDelegate.type != type;
  50. }
  51. }
  52. class _CursorPathGeometry {
  53. _CursorPathGeometry({
  54. required double offset,
  55. required this.cursorType,
  56. this.color,
  57. }) {
  58. _offset = offset;
  59. _paint = Paint()
  60. ..color = color ?? const Color.fromARGB(255, 255, 255, 0)
  61. ..strokeWidth = 2
  62. ..style = PaintingStyle.stroke;
  63. _assemblePathArray();
  64. }
  65. // ignore: constant_identifier_names
  66. static const double LINE_RATIO = 0.8;
  67. late final Paint _paint;
  68. late double _offset;
  69. final MeasureCursorType cursorType;
  70. final Color? color;
  71. final List<Path> _pathArray = <Path>[];
  72. List<Path> get pathArray => _pathArray;
  73. void draw(Canvas canvas) {
  74. canvas.save();
  75. for (final Path path in _pathArray) {
  76. canvas.drawPath(path, _paint);
  77. }
  78. canvas.restore();
  79. }
  80. void _assemblePathArray() {
  81. switch (cursorType) {
  82. case MeasureCursorType.cursor01:
  83. _pathArray.add(_buildHorizontalLine());
  84. _pathArray.add(_buildVerticalLine());
  85. break;
  86. case MeasureCursorType.cursor02:
  87. _pathArray.add(_buildLeftBias());
  88. _pathArray.add(_buildRightBias());
  89. break;
  90. case MeasureCursorType.cursor03:
  91. _pathArray.add(_buildHorizontalLine());
  92. _pathArray.add(_buildLeftBias());
  93. _pathArray.add(_buildRightBias());
  94. break;
  95. case MeasureCursorType.cursor04:
  96. _pathArray.add(_buildVerticalLine());
  97. _pathArray.add(_buildLeftBias());
  98. _pathArray.add(_buildRightBias());
  99. break;
  100. case MeasureCursorType.cursor05:
  101. _pathArray.add(_buildLeftSide());
  102. _pathArray.add(_buildRightSide());
  103. _pathArray.add(_buildTopSide());
  104. _pathArray.add(_buildBottomSide());
  105. _offset *= LINE_RATIO;
  106. _pathArray.add(_buildHorizontalLine());
  107. _pathArray.add(_buildVerticalLine());
  108. break;
  109. }
  110. }
  111. Path _buildHorizontalLine() {
  112. return Path()
  113. ..moveTo(-_offset, 0)
  114. ..lineTo(_offset, 0);
  115. }
  116. Path _buildVerticalLine() {
  117. return Path()
  118. ..moveTo(0, -_offset)
  119. ..lineTo(0, _offset);
  120. }
  121. Path _buildLeftBias() {
  122. return Path()
  123. ..moveTo(-_offset, -_offset)
  124. ..lineTo(_offset, _offset);
  125. }
  126. Path _buildRightBias() {
  127. return Path()
  128. ..moveTo(_offset, -_offset)
  129. ..lineTo(-_offset, _offset);
  130. }
  131. Path _buildLeftSide() {
  132. return Path()
  133. ..moveTo(-_offset, -_offset * LINE_RATIO)
  134. ..lineTo(-_offset, _offset * LINE_RATIO);
  135. }
  136. Path _buildRightSide() {
  137. return Path()
  138. ..moveTo(_offset, -_offset * LINE_RATIO)
  139. ..lineTo(_offset, _offset * LINE_RATIO);
  140. }
  141. Path _buildTopSide() {
  142. return Path()
  143. ..moveTo(-_offset * LINE_RATIO, -_offset)
  144. ..lineTo(_offset * LINE_RATIO, -_offset);
  145. }
  146. Path _buildBottomSide() {
  147. return Path()
  148. ..moveTo(-_offset * LINE_RATIO, _offset)
  149. ..lineTo(_offset * LINE_RATIO, _offset);
  150. }
  151. }