CircleProgressBarPainter.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import 'package:flutter/material.dart';
  2. class CircleProgressBarPainter extends CustomPainter {
  3. var _paintBckGround;
  4. var _paintFore;
  5. final _strokeWidth;
  6. final _backgroundColor;
  7. final _foreColor;
  8. final _startAngle;
  9. final _sweepAngle;
  10. final _endAngle;
  11. CircleProgressBarPainter(this._backgroundColor, this._foreColor,
  12. this._startAngle, this._sweepAngle, this._endAngle, this._strokeWidth) {
  13. _paintBckGround = Paint()
  14. ..color = _backgroundColor
  15. ..isAntiAlias = true
  16. ..strokeCap = StrokeCap.round
  17. ..strokeWidth = _strokeWidth
  18. ..style = PaintingStyle.stroke;
  19. final Gradient gradient = SweepGradient(
  20. endAngle: _endAngle,
  21. colors: [
  22. _foreColor,
  23. _backgroundColor,
  24. ],
  25. );
  26. final Rect arcRect = Rect.fromCircle(center: Offset(0, 0), radius: _endAngle);
  27. _paintFore = Paint()
  28. ..color = _foreColor
  29. ..isAntiAlias = true
  30. ..strokeWidth = _strokeWidth
  31. ..strokeCap = StrokeCap.round
  32. ..shader=gradient.createShader(arcRect)
  33. ..style = PaintingStyle.stroke;
  34. }
  35. @override
  36. void paint(Canvas canvas, Size size) {
  37. var radius = size.width > size.height ? size.width / 2 : size.height / 2;
  38. Rect rect = Rect.fromCircle(center: Offset(radius, radius), radius: radius);
  39. canvas.drawCircle(Offset(radius, radius), radius, _paintBckGround);
  40. canvas.drawArc(rect, _startAngle / 180 * 3.14, _sweepAngle / 180 * 3.14,
  41. false, _paintFore);
  42. }
  43. @override
  44. bool shouldRepaint(CustomPainter oldDelegate) {
  45. return _sweepAngle != _endAngle;
  46. }
  47. }