bdangvnt

RoundedRectangleProgress

Oct 28th, 2020
630
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class RoundedRectangleProgress extends CustomPainter {
  2.   final double currentProgress;
  3.   final BorderRadius borderRadius;
  4.   final Color color;
  5.  
  6.   RoundedRectangleProgress({
  7.     this.color = Colors.blue,
  8.     this.borderRadius = BorderRadius.zero,
  9.     this.currentProgress = 0,
  10.   });
  11.  
  12.   @override
  13.   void paint(Canvas canvas, Size size) {
  14.     final rect = Offset.zero & size;
  15.     //1
  16.     final shapeBounds = Rect.fromLTRB(0, 0, size.width, size.height);
  17.     //2
  18.     final paint = Paint()
  19.       ..strokeWidth = 10
  20.       ..color = currentProgress > 288 ? Colors.redAccent : color
  21.       ..style = PaintingStyle.stroke
  22.       ..strokeCap = StrokeCap.square
  23.       ..shader = SweepGradient(
  24.         colors: [
  25.           if (currentProgress > 288) Colors.redAccent else color,
  26.           Colors.white,
  27.         ],
  28.         stops: const [1.0, 1.0],
  29.         startAngle: 0,
  30.         endAngle: vector.radians(currentProgress),
  31.         transform: GradientRotation(vector.radians(-90)),
  32.       ).createShader(rect);
  33.     //3
  34.     //canvas.drawRect(shapeBounds, paint);
  35.     final borderRect =
  36.         borderRadius.resolve(TextDirection.ltr).toRRect(shapeBounds);
  37.     canvas.drawRRect(borderRect, paint);
  38.   }
  39.  
  40.   @override
  41.   bool shouldRepaint(CustomPainter oldDelegate) {
  42.     return true;
  43.   }
  44. }
RAW Paste Data