target_painter.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:fast/utils/global.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:fast/utils/size_fit.dart';
  4. import 'dart:ui' as ui;
  5. import 'dart:math';
  6. class TargetPainter extends CustomPainter {
  7. Color lineColor;
  8. Color completeColor;
  9. double arcAngle;
  10. double beginAngle;
  11. double width;
  12. ui.Image? localImage;
  13. TargetPainter(
  14. {required this.lineColor,
  15. required this.completeColor,
  16. required this.arcAngle,
  17. required this.beginAngle,
  18. required this.width,
  19. required this.localImage});
  20. @override
  21. void paint(Canvas canvas, Size size) {
  22. Paint line = Paint()
  23. ..color = lineColor
  24. ..strokeCap = StrokeCap.round
  25. ..style = PaintingStyle.stroke
  26. ..strokeWidth = width + 8;
  27. Paint complete = Paint()
  28. // ..color = completeColor
  29. ..strokeCap = StrokeCap.round
  30. ..style = PaintingStyle.stroke
  31. ..strokeWidth = width;
  32. Offset center = Offset(size.width / 2, size.height / 2); // 坐标中心
  33. double radius = Global().progressWidth; // 半径
  34. double begin = beginAngle;
  35. if (begin>2*pi){
  36. begin -= 2*pi;
  37. }
  38. if (begin>pi){
  39. begin -= 2*pi;
  40. }
  41. if (arcAngle>2*pi && begin<0){
  42. arcAngle -= 2*pi;
  43. }
  44. if (begin>0 && arcAngle<begin){
  45. arcAngle += 2*pi;
  46. }
  47. if (arcAngle<-pi){
  48. arcAngle += 2*pi;
  49. }
  50. double end = arcAngle-begin;
  51. if (end>2*pi){
  52. end -= 2*pi;
  53. }
  54. // if (begin < 0 && end > 2 * pi + (pi / 2 + begin)) {
  55. // end -= 2 * pi;
  56. // }
  57. complete.color = const Color(0x26C4CCDA);
  58. canvas.drawArc(Rect.fromCircle(center: center, radius: radius), begin, end,
  59. false, complete);
  60. // if (localImage != null) {
  61. // double startLeft = center.dx + radius * cos(begin);
  62. // double startTop = center.dy + radius * sin(begin);
  63. // double? width = localImage?.width.toDouble();
  64. // double? height = localImage?.height.toDouble();
  65. // canvas.drawImageRect(
  66. // localImage!,
  67. // Rect.fromLTWH(0, 0, width!, height!),
  68. // Rect.fromLTWH(startLeft - (width / 6).px, startTop - (height / 6).px,
  69. // (width / 3).px, (height / 3).px),
  70. // line);
  71. // }
  72. }
  73. void setCanvasImageData(Canvas canvas) {}
  74. @override
  75. bool shouldRepaint(CustomPainter oldDelegate) => true;
  76. }