historico de jogos
This commit is contained in:
@@ -5,61 +5,70 @@ class PieChartWidget extends StatelessWidget {
|
||||
final double victoryPercentage;
|
||||
final double defeatPercentage;
|
||||
final double drawPercentage;
|
||||
final double size;
|
||||
final double sf; // <-- Fator de Escala
|
||||
final double sf;
|
||||
|
||||
const PieChartWidget({
|
||||
super.key,
|
||||
required this.victoryPercentage,
|
||||
required this.defeatPercentage,
|
||||
this.drawPercentage = 0,
|
||||
this.size = 140,
|
||||
required this.sf, // <-- Obrigatório agora
|
||||
required this.sf,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Aplica a escala ao tamanho total do quadrado do gráfico
|
||||
final double scaledSize = size * sf;
|
||||
|
||||
return SizedBox(
|
||||
width: scaledSize,
|
||||
height: scaledSize,
|
||||
child: CustomPaint(
|
||||
painter: _PieChartPainter(
|
||||
victoryPercentage: victoryPercentage,
|
||||
defeatPercentage: defeatPercentage,
|
||||
drawPercentage: drawPercentage,
|
||||
sf: sf, // <-- Passar para desenhar a borda
|
||||
),
|
||||
child: _buildCenterLabels(scaledSize),
|
||||
),
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
// 👇 MAGIA ANTI-DESAPARECIMENTO 👇
|
||||
// Vê o espaço real. Se por algum motivo for infinito, assume 100 para não sumir.
|
||||
final double w = constraints.maxWidth.isInfinite ? 100.0 : constraints.maxWidth;
|
||||
final double h = constraints.maxHeight.isInfinite ? 100.0 : constraints.maxHeight;
|
||||
|
||||
// Pega no menor valor para garantir que o círculo não é cortado
|
||||
final double size = math.min(w, h);
|
||||
|
||||
return Center(
|
||||
child: SizedBox(
|
||||
width: size,
|
||||
height: size,
|
||||
child: CustomPaint(
|
||||
painter: _PieChartPainter(
|
||||
victoryPercentage: victoryPercentage,
|
||||
defeatPercentage: defeatPercentage,
|
||||
drawPercentage: drawPercentage,
|
||||
),
|
||||
child: Center(
|
||||
child: _buildCenterLabels(size),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCenterLabels(double scaledSize) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
'${(victoryPercentage * 100).toStringAsFixed(1)}%',
|
||||
style: TextStyle(
|
||||
fontSize: scaledSize * 0.144, // Mantém-se proporcional
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
Widget _buildCenterLabels(double size) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
'${(victoryPercentage * 100).toStringAsFixed(1)}%',
|
||||
style: TextStyle(
|
||||
fontSize: size * 0.18, // O texto cresce ou encolhe com o círculo
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
SizedBox(height: 4 * sf),
|
||||
Text(
|
||||
'Vitórias',
|
||||
style: TextStyle(
|
||||
fontSize: scaledSize * 0.1, // Mantém-se proporcional
|
||||
color: Colors.white.withOpacity(0.8),
|
||||
),
|
||||
),
|
||||
SizedBox(height: size * 0.02),
|
||||
Text(
|
||||
'Vitórias',
|
||||
style: TextStyle(
|
||||
fontSize: size * 0.10,
|
||||
color: Colors.white.withOpacity(0.8),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -68,78 +77,63 @@ class _PieChartPainter extends CustomPainter {
|
||||
final double victoryPercentage;
|
||||
final double defeatPercentage;
|
||||
final double drawPercentage;
|
||||
final double sf; // <-- Escala no pintor
|
||||
|
||||
_PieChartPainter({
|
||||
required this.victoryPercentage,
|
||||
required this.defeatPercentage,
|
||||
required this.drawPercentage,
|
||||
required this.sf,
|
||||
});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final center = Offset(size.width / 2, size.height / 2);
|
||||
// Aplica a escala à margem para não cortar a linha da borda num ecrã pequeno
|
||||
final radius = size.width / 2 - (5 * sf);
|
||||
// Margem de 5% para a linha de fora não ser cortada
|
||||
final radius = (size.width / 2) - (size.width * 0.05);
|
||||
|
||||
// Cores
|
||||
const victoryColor = Colors.green;
|
||||
const defeatColor = Colors.red;
|
||||
const drawColor = Colors.yellow;
|
||||
const borderColor = Colors.white30;
|
||||
|
||||
double startAngle = 0;
|
||||
double startAngle = -math.pi / 2;
|
||||
|
||||
// Vitórias (verde)
|
||||
if (victoryPercentage > 0) {
|
||||
final sweepAngle = 2 * math.pi * victoryPercentage;
|
||||
_drawSector(canvas, center, radius, startAngle, sweepAngle, victoryColor);
|
||||
_drawSector(canvas, center, radius, startAngle, sweepAngle, victoryColor, size.width);
|
||||
startAngle += sweepAngle;
|
||||
}
|
||||
|
||||
// Empates (amarelo)
|
||||
if (drawPercentage > 0) {
|
||||
final sweepAngle = 2 * math.pi * drawPercentage;
|
||||
_drawSector(canvas, center, radius, startAngle, sweepAngle, drawColor);
|
||||
_drawSector(canvas, center, radius, startAngle, sweepAngle, drawColor, size.width);
|
||||
startAngle += sweepAngle;
|
||||
}
|
||||
|
||||
// Derrotas (vermelho)
|
||||
if (defeatPercentage > 0) {
|
||||
final sweepAngle = 2 * math.pi * defeatPercentage;
|
||||
_drawSector(canvas, center, radius, startAngle, sweepAngle, defeatColor);
|
||||
_drawSector(canvas, center, radius, startAngle, sweepAngle, defeatColor, size.width);
|
||||
}
|
||||
|
||||
// Borda
|
||||
final borderPaint = Paint()
|
||||
..color = borderColor
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2 * sf; // <-- Escala na grossura da linha
|
||||
..strokeWidth = size.width * 0.02;
|
||||
|
||||
canvas.drawCircle(center, radius, borderPaint);
|
||||
}
|
||||
|
||||
void _drawSector(Canvas canvas, Offset center, double radius,
|
||||
double startAngle, double sweepAngle, Color color) {
|
||||
void _drawSector(Canvas canvas, Offset center, double radius, double startAngle, double sweepAngle, Color color, double totalWidth) {
|
||||
final paint = Paint()
|
||||
..color = color
|
||||
..style = PaintingStyle.fill;
|
||||
|
||||
canvas.drawArc(
|
||||
Rect.fromCircle(center: center, radius: radius),
|
||||
startAngle,
|
||||
sweepAngle,
|
||||
true,
|
||||
paint,
|
||||
);
|
||||
canvas.drawArc(Rect.fromCircle(center: center, radius: radius), startAngle, sweepAngle, true, paint);
|
||||
|
||||
// Linha divisória
|
||||
if (sweepAngle < 2 * math.pi) {
|
||||
final linePaint = Paint()
|
||||
..color = Colors.white.withOpacity(0.5)
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.5 * sf; // <-- Escala na grossura da linha
|
||||
..strokeWidth = totalWidth * 0.015;
|
||||
|
||||
final lineX = center.dx + radius * math.cos(startAngle);
|
||||
final lineY = center.dy + radius * math.sin(startAngle);
|
||||
|
||||
Reference in New Issue
Block a user