progress.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'package:flutter/material.dart';
  2. import 'dart:math' as math;
  3. import 'theme.dart';
  4. class Progress extends StatefulWidget {
  5. final double value;
  6. const Progress({
  7. Key key,
  8. @required this.value,
  9. }) : super(key: key);
  10. @override
  11. ProgressState createState() => ProgressState();
  12. }
  13. class ProgressState extends State<Progress> {
  14. /// value of progress, should be 0.0~1.0.
  15. double _value = 0;
  16. @override
  17. void initState() {
  18. super.initState();
  19. }
  20. @override
  21. void dispose() {
  22. super.dispose();
  23. }
  24. void updateProgress(double value) {
  25. setState(() {
  26. _value = value;
  27. });
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return SizedBox(
  32. width: EasyLoadingTheme.indicatorSize,
  33. height: EasyLoadingTheme.indicatorSize,
  34. child: _CircleProgress(
  35. value: _value,
  36. color: EasyLoadingTheme.progressColor,
  37. width: EasyLoadingTheme.progressWidth,
  38. ),
  39. );
  40. }
  41. }
  42. class _CircleProgress extends ProgressIndicator {
  43. final double value;
  44. final double width;
  45. final Color color;
  46. _CircleProgress({
  47. @required this.value,
  48. @required this.width,
  49. @required this.color,
  50. });
  51. @override
  52. __CircleProgressState createState() => __CircleProgressState();
  53. }
  54. class __CircleProgressState extends State<_CircleProgress> {
  55. @override
  56. void initState() {
  57. super.initState();
  58. }
  59. @override
  60. void dispose() {
  61. super.dispose();
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return CustomPaint(
  66. painter: _CirclePainter(
  67. color: widget.color,
  68. value: widget.value,
  69. width: widget.width,
  70. ),
  71. );
  72. }
  73. }
  74. class _CirclePainter extends CustomPainter {
  75. final Color color;
  76. final double value;
  77. final double width;
  78. _CirclePainter({
  79. @required this.color,
  80. @required this.value,
  81. @required this.width,
  82. });
  83. @override
  84. void paint(Canvas canvas, Size size) {
  85. final paint = Paint()
  86. ..color = color
  87. ..strokeWidth = width
  88. ..style = PaintingStyle.stroke
  89. ..strokeCap = StrokeCap.round;
  90. canvas.drawArc(
  91. Offset.zero & size,
  92. -math.pi / 2,
  93. math.pi * 2 * value,
  94. false,
  95. paint,
  96. );
  97. }
  98. @override
  99. bool shouldRepaint(_CirclePainter oldDelegate) => value != oldDelegate.value;
  100. }