utils.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'dart:ui' as ui;
  2. import 'package:flutter/material.dart';
  3. import 'package:path_drawing/path_drawing.dart';
  4. extension MeasureCanvasExt on Canvas {
  5. /// 画虚线
  6. ///
  7. /// [p1] 第一个点
  8. ///
  9. /// [p2] 第二个点
  10. ///
  11. /// [dashWidth] 虚线点长度
  12. ///
  13. /// [spaceWidth] 虚线点间隔长度
  14. ///
  15. /// [paint] 画笔
  16. void drawDashLine(
  17. Offset p1,
  18. Offset p2,
  19. double dashWidth,
  20. double spaceWidth,
  21. Paint paint,
  22. ) {
  23. final path = Path()
  24. ..moveTo(p1.dx, p1.dy)
  25. ..lineTo(p2.dx, p2.dy);
  26. drawPath(
  27. dashPath(
  28. path,
  29. dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
  30. ),
  31. paint,
  32. );
  33. }
  34. static final Paint _vertexPaint = Paint()
  35. ..strokeWidth = 1
  36. ..style = PaintingStyle.stroke
  37. ..isAntiAlias = true;
  38. /// 画顶点
  39. ///
  40. /// [offset] 位置
  41. ///
  42. /// [size] 尺寸
  43. ///
  44. /// [active] 是否活动
  45. void drawVertex(
  46. Offset offset,
  47. double size, {
  48. bool active = false,
  49. }) {
  50. final radius = size / 2.0;
  51. double x = offset.dx, y = offset.dy;
  52. save();
  53. final path = Path();
  54. // top_left -> bottom_right
  55. path.moveTo(x - radius, y - radius);
  56. path.lineTo(x + radius, y + radius);
  57. // top_right -> bottom_left
  58. path.moveTo(x + radius, y - radius);
  59. path.lineTo(x - radius, y + radius);
  60. _vertexPaint.color = active ? Colors.green : Colors.yellow;
  61. drawPath(path, _vertexPaint);
  62. restore();
  63. }
  64. }