Last active
June 25, 2019 03:45
-
-
Save duytq94/84192f75853e035d3599e76ef50b3ba0 to your computer and use it in GitHub Desktop.
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
class RadialSeekBarPainter extends CustomPainter { | |
final double trackWidth; | |
final Paint trackPaint; | |
final double progressWidth; | |
final Paint progressPaint; | |
final double progressPercent; | |
final double thumbSize; | |
final Paint thumbPaint; | |
final double thumbPosition; | |
RadialSeekBarPainter({ | |
@required this.trackWidth, | |
@required trackColor, | |
@required this.progressWidth, | |
@required progressColor, | |
@required this.progressPercent, | |
@required this.thumbSize, | |
@required thumbColor, | |
@required this.thumbPosition, | |
}) : trackPaint = new Paint() | |
..color = trackColor | |
..style = PaintingStyle.stroke | |
..strokeWidth = trackWidth, | |
progressPaint = new Paint() | |
..color = progressColor | |
..style = PaintingStyle.stroke | |
..strokeWidth = progressWidth | |
..strokeCap = StrokeCap.round, | |
thumbPaint = new Paint() | |
..color = thumbColor | |
..style = PaintingStyle.fill; | |
@override | |
void paint(Canvas canvas, Size size) { | |
final outerThickness = max(trackWidth, max(progressWidth, thumbSize)); | |
Size constrainedSize = new Size( | |
size.width - outerThickness, | |
size.height - outerThickness, | |
); | |
final center = new Offset(size.width / 2, size.height / 2); | |
final radius = min(constrainedSize.width, constrainedSize.height) / 2; | |
// Paint track. | |
canvas.drawCircle( | |
center, | |
radius, | |
trackPaint, | |
); | |
// Paint progress. | |
final progressAngle = 2 * pi * progressPercent; | |
canvas.drawArc( | |
new Rect.fromCircle( | |
center: center, | |
radius: radius, | |
), | |
-pi / 2, | |
progressAngle, | |
false, | |
progressPaint, | |
); | |
// Paint thumb. | |
final thumbAngle = 2 * pi * thumbPosition - (pi / 2); | |
final thumbX = cos(thumbAngle) * radius; | |
final thumbY = sin(thumbAngle) * radius; | |
final thumbCenter = new Offset(thumbX, thumbY) + center; | |
final thumbRadius = thumbSize / 2.0; | |
canvas.drawCircle( | |
thumbCenter, | |
thumbRadius, | |
thumbPaint, | |
); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) { | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment