Created with <3 with dartpad.dev.
Created
December 7, 2022 15:29
-
-
Save knaeckeKami/12a2c32d3b124653e770add4939c633e to your computer and use it in GitHub Desktop.
astonishing-spray-1411
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'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(SizedBox(height: 200, child: HalfCircleWidget(values: [1,2,3]))); | |
} | |
class HalfCircleWidget extends StatelessWidget { | |
final List<double> values; | |
HalfCircleWidget({required this.values}); | |
@override | |
Widget build(BuildContext context) { | |
double total = 0; | |
values.forEach((value) => total += value); | |
return Container( | |
child: CustomPaint( | |
painter: HalfCirclePainter(values: values, total: total), | |
size: Size(200,200) | |
), | |
); | |
} | |
} | |
class HalfCirclePainter extends CustomPainter { | |
final List<double> values; | |
final double total; | |
HalfCirclePainter({required this.values, required this.total}); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final center = (Offset.zero & size).center; | |
final radius = min(size.width, size.height) / 4; | |
var startAngle = pi; | |
for (int i = 0; i < values.length; i++) { | |
final value = values[i]; | |
final sweepAngle = (value / total) * pi; | |
final rect = Rect.fromCircle(center: center, radius: radius); | |
final paint = Paint() | |
..color = Colors.blue.withGreen(60 * i) | |
..style = PaintingStyle.stroke | |
..strokeWidth = 90; | |
canvas.drawArc(rect, startAngle, sweepAngle, false, paint); | |
startAngle += sweepAngle; | |
} | |
} | |
@override | |
bool shouldRepaint(HalfCirclePainter oldDelegate) { | |
return oldDelegate.values != values || oldDelegate.total != total; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment