128 lines
3.7 KiB
Dart
128 lines
3.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'dart:math' as math;
|
|
|
|
class PieChartWidget extends StatelessWidget {
|
|
final double victoryPercentage;
|
|
final double defeatPercentage;
|
|
final double drawPercentage;
|
|
final double sf;
|
|
|
|
const PieChartWidget({
|
|
super.key,
|
|
required this.victoryPercentage,
|
|
required this.defeatPercentage,
|
|
this.drawPercentage = 0,
|
|
required this.sf,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final double w = constraints.maxWidth.isInfinite ? 100.0 : constraints.maxWidth;
|
|
final double h = constraints.maxHeight.isInfinite ? 100.0 : constraints.maxHeight;
|
|
|
|
final double size = math.min(w, h);
|
|
|
|
return Center(
|
|
child: SizedBox(
|
|
width: size,
|
|
height: size,
|
|
child: CustomPaint(
|
|
painter: _DonutChartPainter(
|
|
victoryPercentage: victoryPercentage,
|
|
defeatPercentage: defeatPercentage,
|
|
drawPercentage: drawPercentage,
|
|
),
|
|
child: Center(
|
|
child: _buildCenterLabels(size),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildCenterLabels(double size) {
|
|
final bool hasGames = victoryPercentage > 0 || defeatPercentage > 0 || drawPercentage > 0;
|
|
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
// 👇 Casa decimal aplicada aqui!
|
|
hasGames ? '${(victoryPercentage * 100).toStringAsFixed(1)}%' : '---',
|
|
style: TextStyle(
|
|
fontSize: size * (hasGames ? 0.20 : 0.15),
|
|
fontWeight: FontWeight.bold,
|
|
color: hasGames ? Colors.white : Colors.white54,
|
|
),
|
|
),
|
|
SizedBox(height: size * 0.02),
|
|
Text(
|
|
hasGames ? 'Vitórias' : 'Sem Jogos',
|
|
style: TextStyle(
|
|
fontSize: size * 0.08,
|
|
color: hasGames ? Colors.white70 : Colors.white38,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _DonutChartPainter extends CustomPainter {
|
|
final double victoryPercentage;
|
|
final double defeatPercentage;
|
|
final double drawPercentage;
|
|
|
|
_DonutChartPainter({
|
|
required this.victoryPercentage,
|
|
required this.defeatPercentage,
|
|
required this.drawPercentage,
|
|
});
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
final center = Offset(size.width / 2, size.height / 2);
|
|
final radius = (size.width / 2) - (size.width * 0.1);
|
|
final strokeWidth = size.width * 0.2;
|
|
|
|
if (victoryPercentage == 0 && defeatPercentage == 0 && drawPercentage == 0) {
|
|
final bgPaint = Paint()
|
|
..color = Colors.white.withOpacity(0.05)
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = strokeWidth;
|
|
canvas.drawCircle(center, radius, bgPaint);
|
|
return;
|
|
}
|
|
|
|
const victoryColor = Colors.green;
|
|
const defeatColor = Colors.red;
|
|
const drawColor = Colors.amber;
|
|
|
|
double startAngle = -math.pi / 2;
|
|
|
|
void drawDonutSector(double percentage, Color color) {
|
|
if (percentage <= 0) return;
|
|
final sweepAngle = 2 * math.pi * percentage;
|
|
final paint = Paint()
|
|
..color = color
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = strokeWidth
|
|
..strokeCap = StrokeCap.butt;
|
|
|
|
canvas.drawArc(Rect.fromCircle(center: center, radius: radius), startAngle, sweepAngle, false, paint);
|
|
startAngle += sweepAngle;
|
|
}
|
|
|
|
drawDonutSector(victoryPercentage, victoryColor);
|
|
drawDonutSector(drawPercentage, drawColor);
|
|
drawDonutSector(defeatPercentage, defeatColor);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
|
|
} |