canvas.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. static String? _fontFamily;
  7. /// 设置字体
  8. static void setFontFamily(String? val) {
  9. _fontFamily = val;
  10. }
  11. /// 绘制文本
  12. ///
  13. /// [text] 文本内容
  14. ///
  15. /// [offset] 位置
  16. ///
  17. /// [style] 文字样式
  18. void drawText(
  19. String text,
  20. ui.Offset offset, {
  21. TextStyle? style,
  22. }) {
  23. var displayStyle = style;
  24. if (displayStyle != null) {
  25. if (displayStyle.fontFamily == null) {
  26. displayStyle = displayStyle.copyWith(fontFamily: _fontFamily);
  27. }
  28. }
  29. var textPainter = TextPainter(
  30. text: TextSpan(
  31. text: text,
  32. style: displayStyle,
  33. ),
  34. textAlign: ui.TextAlign.start,
  35. textDirection: TextDirection.ltr,
  36. );
  37. textPainter.layout();
  38. textPainter.paint(this, offset);
  39. }
  40. /// 绘制文本段落
  41. ///
  42. /// [text] 文本内容
  43. ///
  44. /// [offset] 位置
  45. ///
  46. /// [width] 限制长度
  47. ///
  48. /// [style] 文字样式
  49. ///
  50. /// [maxLines] 最大行数
  51. void drawTextParagraph(
  52. String text,
  53. ui.Offset offset,
  54. double width, {
  55. ui.TextStyle? style,
  56. int maxLines = 1,
  57. }) {
  58. var pb = ui.ParagraphBuilder(
  59. ui.ParagraphStyle(
  60. textAlign: TextAlign.left,
  61. textDirection: ui.TextDirection.ltr,
  62. maxLines: maxLines,
  63. ),
  64. );
  65. if (style != null) {
  66. pb.pushStyle(style);
  67. }
  68. pb.addText(text);
  69. final paragraph = pb.build()..layout(ui.ParagraphConstraints(width: width));
  70. drawParagraph(paragraph, offset);
  71. }
  72. /// 画虚线
  73. ///
  74. /// [p1] 第一个点
  75. ///
  76. /// [p2] 第二个点
  77. ///
  78. /// [dashWidth] 虚线点长度
  79. ///
  80. /// [spaceWidth] 虚线点间隔长度
  81. ///
  82. /// [paint] 画笔
  83. void drawDashLine(
  84. Offset p1,
  85. Offset p2,
  86. double dashWidth,
  87. double spaceWidth,
  88. Paint paint,
  89. ) {
  90. final path = Path()
  91. ..moveTo(p1.dx, p1.dy)
  92. ..lineTo(p2.dx, p2.dy);
  93. drawPath(
  94. dashPath(
  95. path,
  96. dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
  97. ),
  98. paint,
  99. );
  100. }
  101. /// 画虚线框
  102. ///
  103. /// [p1] 起始顶点
  104. ///
  105. /// [p2] 结束顶点
  106. ///
  107. /// [dashWidth] 虚线点长度
  108. ///
  109. /// [spaceWidth] 虚线点间隔长度
  110. ///
  111. /// [paint] 画笔
  112. void drawDashRect(
  113. Offset p1,
  114. Offset p2,
  115. double dashWidth,
  116. double spaceWidth,
  117. Paint paint,
  118. ) {
  119. final path = Path()
  120. ..moveTo(p1.dx, p1.dy)
  121. ..lineTo(p2.dx, p1.dy)
  122. ..lineTo(p2.dx, p2.dy)
  123. ..lineTo(p1.dx, p2.dy)
  124. ..close();
  125. drawPath(
  126. dashPath(
  127. path,
  128. dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
  129. ),
  130. paint,
  131. );
  132. }
  133. /// 画点集连线
  134. /// [points] 点集
  135. /// [paint] 画笔
  136. void drawPointsLine(List<Offset> points, Paint paint) {
  137. final path = Path();
  138. for (var i = 0; i < points.length; i++) {
  139. if (i == 0) {
  140. path.moveTo(points[i].dx, points[i].dy);
  141. } else {
  142. path.lineTo(points[i].dx, points[i].dy);
  143. }
  144. }
  145. drawPath(path, paint);
  146. }
  147. static final Paint _vertexPaint = Paint()
  148. ..strokeWidth = 1
  149. ..style = PaintingStyle.stroke
  150. ..isAntiAlias = true;
  151. /// 画顶点
  152. ///
  153. /// [offset] 位置
  154. ///
  155. /// [size] 尺寸
  156. ///
  157. /// [active] 是否活动
  158. void drawVertex(
  159. Offset offset,
  160. double size, {
  161. bool active = false,
  162. }) {
  163. final radius = size / 2.0;
  164. double x = offset.dx, y = offset.dy;
  165. save();
  166. final path = Path();
  167. // top_left -> bottom_right
  168. path.moveTo(x - radius, y - radius);
  169. path.lineTo(x + radius, y + radius);
  170. // top_right -> bottom_left
  171. path.moveTo(x + radius, y - radius);
  172. path.lineTo(x - radius, y + radius);
  173. _vertexPaint.color =
  174. active ? MeasureColors.ActiveCaliper : MeasureColors.Primary;
  175. drawPath(path, _vertexPaint);
  176. restore();
  177. }
  178. }