cursor.dart 3.8 KB

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