Created
July 17, 2023 07:40
-
-
Save CoderNamedHendrick/9bb42104e2b7d5018dbc62883bee67ce to your computer and use it in GitHub Desktop.
Round Linear progress indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoundLinearProgress extends StatelessWidget { | |
const RoundLinearProgress({super.key, double progress = 0}) | |
: _progress = progress, | |
assert( | |
progress >= 0 && progress <= 100, | |
'indicator progress must be between 0 and 100', | |
); | |
final double _progress; | |
@override | |
Widget build(BuildContext context) { | |
return TweenAnimationBuilder( | |
curve: Curves.easeInOut, | |
duration: const Duration(milliseconds: 1300), | |
tween: Tween<double>(begin: 0, end: _progress), | |
builder: (_, progressValue, __) { | |
return CustomPaint( | |
painter: ProgressPainter(progressValue), | |
size: const Size(double.infinity, 8), | |
); | |
}, | |
); | |
} | |
} | |
class ProgressPainter extends CustomPainter { | |
final double progress; | |
const ProgressPainter([this.progress = 0]); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final paint = Paint() | |
..strokeCap = StrokeCap.round | |
..strokeWidth = size.height | |
..style = PaintingStyle.stroke | |
..color = Colors.blueAccent.withOpacity(0.3); | |
final foregroundPaint = Paint() | |
..strokeCap = StrokeCap.round | |
..strokeWidth = size.height | |
..style = PaintingStyle.stroke | |
..color = Colors.blue; | |
final bgPath = Path() | |
..moveTo(0, size.height) | |
..lineTo(size.width, size.height); | |
final fgPath = Path() | |
..moveTo(0, size.height) | |
..lineTo((progress / 100) * size.width, size.height); | |
canvas.drawPath(bgPath, paint); | |
canvas.drawPath(fgPath, foregroundPaint); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) { | |
if (oldDelegate is! ProgressPainter) return false; | |
if (oldDelegate.progress == progress) return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment