cursor.dart 4.0 KB

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