Created
October 10, 2024 07:25
-
-
Save matifdeveloper/9904c7787be1f45d06d798260ae83ac4 to your computer and use it in GitHub Desktop.
Custom Flutter Circular Progress Indicator
This file contains 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
import 'package:flutter/material.dart'; | |
import 'dart:math'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class CircularProgressIndicator extends StatelessWidget { | |
final double percentage; | |
final double size; | |
const CircularProgressIndicator({ | |
Key? key, | |
required this.percentage, | |
this.size = 200, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: size, | |
height: size, | |
child: CustomPaint( | |
painter: CircularProgressPainter(percentage: percentage), | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
'${percentage.toInt()}%', | |
style: TextStyle( | |
fontSize: size / 5, | |
fontWeight: FontWeight.bold, | |
color: Colors.blue, | |
), | |
), | |
Text( | |
'Clean', | |
style: TextStyle( | |
fontSize: size / 10, | |
color: Colors.blue, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class CircularProgressPainter extends CustomPainter { | |
final double percentage; | |
CircularProgressPainter({required this.percentage}); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final center = Offset(size.width / 2, size.height / 2); | |
final radius = min(size.width, size.height) / 2; | |
// Draw background circle | |
final backgroundPaint = Paint() | |
..color = Colors.grey.shade200 | |
..style = PaintingStyle.stroke | |
..strokeWidth = radius * 0.2; | |
canvas.drawCircle(center, radius * 0.9, backgroundPaint); | |
// Draw progress arc | |
final progressPaint = Paint() | |
..color = Colors.blue | |
..style = PaintingStyle.stroke | |
..strokeWidth = radius * 0.2 | |
..strokeCap = StrokeCap.round; | |
final progressAngle = 2 * pi * (percentage / 100); | |
canvas.drawArc( | |
Rect.fromCircle(center: center, radius: radius * 0.9), | |
-pi / 2, | |
progressAngle, | |
false, | |
progressPaint, | |
); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) => true; | |
} | |
// Usage example | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: CircularProgressIndicator(percentage: 65), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment