- getAllSessions : élimine le N+1 (une requête par session + par analyse) au profit de 3 requêtes groupées (sessions, analyses IN, shots IN), découpées en paquets de 500 pour rester sous la limite SQLite. Assemblage en mémoire via maps — accueil/historique/stats ne ralentiront plus au fil des mois - flutter analyze : 28 -> 0 problème. APIs dépréciées migrées (withOpacity -> withValues, scale -> scaleByDouble, DropdownButtonFormField.value -> initialValue, dialogBackgroundColor -> dialogTheme, activeColor -> activeThumbColor), use_build_context_synchronously corrigés dans l'armurerie (repository capturé avant await + gardes mounted), print -> debugPrint, identifiants TopLeft/... -> lowerCamelCase, blocs if, champ final - _showShotDetails dédupliqué : bottom sheet extraite dans le widget partagé shot_details_sheet.dart, utilisée par l'écran d'analyse et l'éditeur d'impacts - CLAUDE.md : sections Features réécrites (retrait détection auto/références, ajout capture/plotting manuel) ; description pubspec renseignée Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
282 lines
10 KiB
Dart
282 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../core/constants/app_constants.dart';
|
|
import '../../core/theme/app_theme.dart';
|
|
import '../../data/models/weapon.dart';
|
|
import '../../data/repositories/session_repository.dart';
|
|
import 'weapon_detail_screen.dart';
|
|
|
|
class WeaponListScreen extends StatefulWidget {
|
|
const WeaponListScreen({super.key});
|
|
|
|
@override
|
|
State<WeaponListScreen> createState() => _WeaponListScreenState();
|
|
}
|
|
|
|
class _WeaponListScreenState extends State<WeaponListScreen> {
|
|
List<Weapon> _weapons = [];
|
|
bool _isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadWeapons();
|
|
}
|
|
|
|
Future<void> _loadWeapons() async {
|
|
final repository = context.read<SessionRepository>();
|
|
final weapons = await repository.getWeapons();
|
|
if (mounted) {
|
|
setState(() {
|
|
_weapons = weapons;
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Mon Armurerie'),
|
|
),
|
|
body: _isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: _weapons.isEmpty
|
|
? _buildEmptyState()
|
|
: _buildWeaponList(),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _showAddWeaponDialog,
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildEmptyState() {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.shield, size: 64, color: Colors.grey[400]),
|
|
const SizedBox(height: 16),
|
|
const Text('Aucune arme enregistrée'),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: _showAddWeaponDialog,
|
|
child: const Text('Ajouter ma première arme'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildWeaponList() {
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(AppConstants.defaultPadding),
|
|
itemCount: _weapons.length,
|
|
itemBuilder: (context, index) {
|
|
final weapon = _weapons[index];
|
|
final accessories = _accessoryChips(weapon);
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
child: InkWell(
|
|
borderRadius: BorderRadius.circular(12),
|
|
onTap: () async {
|
|
await Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => WeaponDetailScreen(weapon: weapon)),
|
|
);
|
|
_loadWeapons(); // Reload in case it was edited or maintenance was added
|
|
},
|
|
onLongPress: () => _confirmDelete(weapon),
|
|
child: Padding(
|
|
// La hauteur du cadre s'adapte automatiquement à la liste d'accessoires.
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
CircleAvatar(
|
|
backgroundColor: AppTheme.primaryColor.withValues(alpha: 0.1),
|
|
child: Icon(
|
|
weapon.type == WeaponType.handgun ? Icons.shield : Icons.ads_click,
|
|
color: AppTheme.primaryColor,
|
|
),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(weapon.displayName, style: const TextStyle(fontWeight: FontWeight.bold)),
|
|
if (accessories.isNotEmpty) ...[
|
|
const SizedBox(height: 6),
|
|
Wrap(
|
|
spacing: 6,
|
|
runSpacing: 6,
|
|
children: accessories,
|
|
),
|
|
const SizedBox(height: 6),
|
|
] else
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
'${weapon.type.displayName} • ${weapon.caliber}',
|
|
style: const TextStyle(fontSize: 13, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text('${weapon.magazineCount} chargeurs', style: const TextStyle(fontSize: 12)),
|
|
Text('${weapon.magazineCapacity} coups', style: const TextStyle(fontSize: 12, color: Colors.grey)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
// Construit la liste des "puces" d'accessoires renseignés pour une arme.
|
|
// Seuls les accessoires effectivement définis sont affichés ; la card
|
|
// s'agrandit donc en fonction du nombre d'accessoires.
|
|
List<Widget> _accessoryChips(Weapon weapon) {
|
|
final items = <(IconData, String)>[];
|
|
if (weapon.optic != null && weapon.optic!.isNotEmpty) {
|
|
items.add((Icons.center_focus_strong, weapon.optic!));
|
|
}
|
|
if (weapon.silencer != null && weapon.silencer!.isNotEmpty) {
|
|
items.add((Icons.volume_off, weapon.silencer!));
|
|
}
|
|
if (weapon.trigger != null && weapon.trigger!.isNotEmpty) {
|
|
items.add((Icons.touch_app, weapon.trigger!));
|
|
}
|
|
|
|
return items.map((item) {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor.withValues(alpha: 0.08),
|
|
borderRadius: BorderRadius.circular(20),
|
|
border: Border.all(color: AppTheme.primaryColor.withValues(alpha: 0.25)),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(item.$1, size: 13, color: AppTheme.primaryColor),
|
|
const SizedBox(width: 4),
|
|
Text(item.$2, style: const TextStyle(fontSize: 12)),
|
|
],
|
|
),
|
|
);
|
|
}).toList();
|
|
}
|
|
|
|
void _showAddWeaponDialog() async {
|
|
// Capturé AVANT l'await : plus aucun usage du context après le dialogue.
|
|
final repository = context.read<SessionRepository>();
|
|
final nameController = TextEditingController();
|
|
final caliberController = TextEditingController();
|
|
final magCountController = TextEditingController(text: '2');
|
|
final magCapController = TextEditingController(text: '15');
|
|
WeaponType selectedType = WeaponType.handgun;
|
|
|
|
final result = await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => StatefulBuilder(
|
|
builder: (context, setState) => AlertDialog(
|
|
title: const Text('Ajouter une arme'),
|
|
content: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: nameController,
|
|
decoration: const InputDecoration(labelText: 'Nom de l\'arme', hintText: 'ex: Glock 17'),
|
|
),
|
|
DropdownButtonFormField<WeaponType>(
|
|
initialValue: selectedType,
|
|
decoration: const InputDecoration(labelText: 'Type'),
|
|
items: WeaponType.values.map((t) => DropdownMenuItem(value: t, child: Text(t.displayName))).toList(),
|
|
onChanged: (v) => setState(() => selectedType = v!),
|
|
),
|
|
TextField(
|
|
controller: caliberController,
|
|
decoration: const InputDecoration(labelText: 'Calibre', hintText: 'ex: 9mm, .22LR'),
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: magCountController,
|
|
decoration: const InputDecoration(labelText: 'Chargeurs'),
|
|
keyboardType: TextInputType.number,
|
|
),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: TextField(
|
|
controller: magCapController,
|
|
decoration: const InputDecoration(labelText: 'Capacité'),
|
|
keyboardType: TextInputType.number,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Annuler')),
|
|
ElevatedButton(onPressed: () => Navigator.pop(context, true), child: const Text('Ajouter')),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
|
|
if (result == true && nameController.text.isNotEmpty) {
|
|
await repository.addWeapon(
|
|
name: nameController.text,
|
|
type: selectedType,
|
|
caliber: caliberController.text,
|
|
magazineCount: int.tryParse(magCountController.text) ?? 1,
|
|
magazineCapacity: int.tryParse(magCapController.text) ?? 10,
|
|
);
|
|
if (!mounted) return;
|
|
_loadWeapons();
|
|
}
|
|
}
|
|
|
|
void _confirmDelete(Weapon weapon) async {
|
|
// Capturé AVANT l'await : plus aucun usage du context après le dialogue.
|
|
final repository = context.read<SessionRepository>();
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Supprimer'),
|
|
content: Text('Voulez-vous supprimer ${weapon.name} de votre armurerie ?'),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Annuler')),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: const Text('Supprimer', style: TextStyle(color: AppTheme.errorColor)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (confirmed == true) {
|
|
await repository.deleteWeapon(weapon.id);
|
|
if (!mounted) return;
|
|
_loadWeapons();
|
|
}
|
|
}
|
|
}
|