canvas.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import 'dart:ui' as ui;
  2. import 'package:fis_measure/values/colors.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:path_drawing/path_drawing.dart';
  5. extension MeasureCanvasExt on Canvas {
  6. /// 绘制文本
  7. ///
  8. /// [text] 文本内容
  9. ///
  10. /// [offset] 位置
  11. ///
  12. /// [style] 文字样式
  13. void drawText(
  14. String text,
  15. ui.Offset offset, {
  16. TextStyle? style,
  17. }) {
  18. var textPainter = TextPainter(
  19. text: TextSpan(
  20. text: text,
  21. style: style,
  22. ),
  23. textAlign: ui.TextAlign.start,
  24. textDirection: TextDirection.ltr,
  25. );
  26. textPainter.layout();
  27. textPainter.paint(this, offset);
  28. }
  29. /// 绘制文本段落
  30. ///
  31. /// [text] 文本内容
  32. ///
  33. /// [offset] 位置
  34. ///
  35. /// [width] 限制长度
  36. ///
  37. /// [style] 文字样式
  38. ///
  39. /// [maxLines] 最大行数
  40. void drawTextParagraph(
  41. String text,
  42. ui.Offset offset,
  43. double width, {
  44. ui.TextStyle? style,
  45. int maxLines = 1,
  46. }) {
  47. var pb = ui.ParagraphBuilder(
  48. ui.ParagraphStyle(
  49. textAlign: TextAlign.left,
  50. textDirection: ui.TextDirection.ltr,
  51. maxLines: maxLines,
  52. ),
  53. );
  54. if (style != null) {
  55. pb.pushStyle(style);
  56. }
  57. pb.addText(text);
  58. final paragraph = pb.build()..layout(ui.ParagraphConstraints(width: width));
  59. drawParagraph(paragraph, offset);
  60. }
  61. /// 画虚线
  62. ///
  63. /// [p1] 第一个点
  64. ///
  65. /// [p2] 第二个点
  66. ///
  67. /// [dashWidth] 虚线点长度
  68. ///
  69. /// [spaceWidth] 虚线点间隔长度
  70. ///
  71. /// [paint] 画笔
  72. void drawDashLine(
  73. Offset p1,
  74. Offset p2,
  75. double dashWidth,
  76. double spaceWidth,
  77. Paint paint,
  78. ) {
  79. final path = Path()
  80. ..moveTo(p1.dx, p1.dy)
  81. ..lineTo(p2.dx, p2.dy);
  82. drawPath(
  83. dashPath(
  84. path,
  85. dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
  86. ),
  87. paint,
  88. );
  89. }
  90. static final Paint _vertexPaint = Paint()
  91. ..strokeWidth = 1
  92. ..style = PaintingStyle.stroke
  93. ..isAntiAlias = true;
  94. /// 画顶点
  95. ///
  96. /// [offset] 位置
  97. ///
  98. /// [size] 尺寸
  99. ///
  100. /// [active] 是否活动
  101. void drawVertex(
  102. Offset offset,
  103. double size, {
  104. bool active = false,
  105. }) {
  106. final radius = size / 2.0;
  107. double x = offset.dx, y = offset.dy;
  108. save();
  109. final path = Path();
  110. // top_left -> bottom_right
  111. path.moveTo(x - radius, y - radius);
  112. path.lineTo(x + radius, y + radius);
  113. // top_right -> bottom_left
  114. path.moveTo(x + radius, y - radius);
  115. path.lineTo(x - radius, y + radius);
  116. _vertexPaint.color =
  117. active ? MeasureColors.ActiveCaliper : MeasureColors.Primary;
  118. drawPath(path, _vertexPaint);
  119. restore();
  120. }
  121. }