Files
impact/lib/features/statistics/statistics_screen.dart

306 lines
9.4 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:fl_chart/fl_chart.dart'; // La librairie pour les courbes
import '../../core/constants/app_constants.dart';
import '../../core/theme/app_theme.dart';
import '../../data/models/session.dart';
import '../../data/repositories/session_repository.dart';
import '../../services/statistics_service.dart';
class StatisticsScreen extends StatefulWidget {
final Session? singleSession;
const StatisticsScreen({super.key, this.singleSession});
@override
State<StatisticsScreen> createState() => _StatisticsScreenState();
}
class _StatisticsScreenState extends State<StatisticsScreen> {
final StatisticsService _statisticsService = StatisticsService();
StatsPeriod _selectedPeriod = StatsPeriod.all;
SessionStatistics? _statistics;
bool _isLoading = true;
List<Session> _allSessions = [];
// Valeurs pour les Dropdowns (Filtres)
String _selectedWeapon = 'Toutes';
String _selectedDistance = 'Toutes';
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _loadStatistics());
}
Future<void> _loadStatistics() async {
if (!mounted) return;
setState(() => _isLoading = true);
try {
final repository = context.read<SessionRepository>();
_allSessions = await repository.getAllSessions();
_calculateStats();
} catch (e) {
debugPrint('Error: $e');
} finally {
if (mounted) setState(() => _isLoading = false);
}
}
void _calculateStats() {
_statistics = _statisticsService.calculateStatistics(
_allSessions,
period: _selectedPeriod,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(
0xFF121212,
), // Fond sombre comme sur le design
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () => Navigator.pop(context),
),
title: const Text(
'Statistiques',
style: TextStyle(color: Colors.white),
),
centerTitle: true,
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: [
// 1. FILTRES (Arme et Distance)
Row(
children: [
Expanded(
child: _buildDropdown('Arme utilisée', _selectedWeapon),
),
const SizedBox(width: 12),
Expanded(
child: _buildDropdown('Distance', _selectedDistance),
),
],
),
const SizedBox(height: 20),
// 2. DONNÉES RAPIDES (Tirs et Sessions)
Row(
children: [
Expanded(
child: _buildQuickStat(
'Nbre de tirs',
'${_statistics?.totalShots ?? 0}',
Icons.gps_fixed,
),
),
const SizedBox(width: 12),
Expanded(
child: _buildQuickStat(
'Nbre de sessions',
'${_statistics?.sessions.length ?? 0}',
Icons.analytics,
),
),
],
),
const SizedBox(height: 25),
// 3. LES GRAPHIQUES
_buildChartSection(
'Score',
'${_statistics?.totalScore ?? 0}',
[8, 12, 10, 15, 14, 18],
),
const SizedBox(height: 20),
_buildChartSection(
'Précision',
'${_statistics?.precision.precisionScore.toStringAsFixed(1)}%',
[60, 75, 70, 85, 80, 95],
),
const SizedBox(height: 20),
_buildChartSection('Groupement moyen', '14.2 mm', [
20,
18,
22,
15,
14,
12,
]), // mm fictif pour l'exemple
const SizedBox(height: 30),
// 4. BOUTON ACCUEIL
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF1A73E8),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
onPressed: () => Navigator.of(
context,
).popUntil((route) => route.isFirst),
child: const Text(
'ACCUEIL',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
),
);
}
// Widget pour les Dropdowns de filtres
Widget _buildDropdown(String label, String value) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
decoration: BoxDecoration(
color: const Color(0xFF1E1E1E),
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.white12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: const TextStyle(color: Colors.white54, fontSize: 10),
),
DropdownButton<String>(
value: value,
isExpanded: true,
underline: Container(),
dropdownColor: const Color(0xFF1E1E1E),
style: const TextStyle(color: Colors.white, fontSize: 14),
items: [value]
.map(
(String val) =>
DropdownMenuItem(value: val, child: Text(val)),
)
.toList(),
onChanged: (newValue) {},
),
],
),
);
}
// Widget pour les petites cartes de stats
Widget _buildQuickStat(String label, String value, IconData icon) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFF1E1E1E),
borderRadius: BorderRadius.circular(12),
),
child: Column(
children: [
Icon(icon, color: const Color(0xFF1A73E8), size: 20),
const SizedBox(height: 8),
Text(
value,
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
Text(
label,
style: const TextStyle(color: Colors.white54, fontSize: 12),
),
],
),
);
}
// Widget pour une section de graphique
Widget _buildChartSection(
String title,
String value,
List<double> dataPoints,
) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFF1E1E1E),
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(color: Colors.white70, fontSize: 14),
),
const SizedBox(height: 10),
Row(
children: [
Text(
value,
style: const TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(width: 20),
Expanded(
child: SizedBox(
height: 60,
child: LineChart(_mainChartData(dataPoints)),
),
),
],
),
],
),
);
}
LineChartData _mainChartData(List<double> points) {
return LineChartData(
gridData: const FlGridData(show: false),
titlesData: const FlTitlesData(show: false),
borderData: FlBorderData(show: false),
lineBarsData: [
LineChartBarData(
spots: points
.asMap()
.entries
.map((e) => FlSpot(e.key.toDouble(), e.value))
.toList(),
isCurved: true,
color: const Color(0xFF4CAF50), // Vert comme sur ton design
barWidth: 3,
isStrokeCapRound: true,
dotData: const FlDotData(show: false),
belowBarData: BarAreaData(
show: true,
color: const Color(0xFF4CAF50).withOpacity(0.2),
),
),
],
);
}
}