progress_painter.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'dart:math';
  2. import 'package:fast/utils/global.dart';
  3. import 'package:fast/utils/size_fit.dart';
  4. import 'package:flutter/material.dart';
  5. import 'dart:ui' as ui;
  6. import '../../constants.dart';
  7. class ProgressPainter extends CustomPainter {
  8. Color lineColor;
  9. Color completeColor;
  10. double arcAngle;
  11. double beginAngle;
  12. double width;
  13. ui.Image? localImage;
  14. ProgressPainter(
  15. {required this.lineColor,
  16. required this.completeColor,
  17. required this.arcAngle,
  18. required this.beginAngle,
  19. required this.width,
  20. required this.localImage});
  21. @override
  22. void paint(Canvas canvas, Size size) {
  23. if (arcAngle==0){
  24. return;
  25. }
  26. Offset center = Offset(size.width / 2, size.height / 2); // 坐标中心
  27. double radius = Global().progressWidth; // 半径
  28. Paint progress = Paint()
  29. ..strokeCap = StrokeCap.round
  30. ..style = PaintingStyle.stroke
  31. ..strokeWidth = width;
  32. var step = 0.171;
  33. var maxEnd = arcAngle;
  34. if (maxEnd>2*pi-step){
  35. maxEnd = 2*pi-2*step;
  36. }
  37. progress.shader = ui.Gradient.sweep(center, [const Color(0x33AAFF00), kThemeColor],[0,1],ui.TileMode.clamp,step,maxEnd+step,null);
  38. canvas.drawArc(Rect.fromCircle(center: center, radius: radius), step, maxEnd,
  39. false, progress);
  40. // Paint line = Paint()
  41. // ..color = lineColor
  42. // ..strokeCap = StrokeCap.round
  43. // ..style = PaintingStyle.stroke
  44. // ..strokeWidth = width + 8;
  45. // if (localImage != null) {
  46. // double startLeft = center.dx + radius * cos(maxEnd+step);
  47. // double startTop = center.dy + radius * sin(maxEnd+step);
  48. // double? width = localImage?.width.toDouble();
  49. // double? height = localImage?.height.toDouble();
  50. // canvas.drawImageRect(
  51. // localImage!,
  52. // Rect.fromLTWH(0, 0, width!, height!),
  53. // Rect.fromLTWH(startLeft - (width / 6).px, startTop - (height / 6).px,
  54. // (width / 3).px, (height / 3).px),
  55. // line);
  56. // }
  57. }
  58. void setCanvasImageData(Canvas canvas) {}
  59. @override
  60. bool shouldRepaint(CustomPainter oldDelegate) => true;
  61. }