Compare commits

...
+101 -24
View File
@@ -27,8 +27,14 @@ class _SessionSetupScreenState extends State<SessionSetupScreen> {
DateTime _selectedDate = DateTime.now(); DateTime _selectedDate = DateTime.now();
/// Valeur sentinelle de l'item "Ajouter une nouvelle arme" du menu deroulant.
static const String _addWeaponValue = '__add_weapon__';
final GlobalKey<FormFieldState<String>> _weaponFieldKey =
GlobalKey<FormFieldState<String>>();
static const List<int> _presetDistances = [10, 15, 25, 50, 100, 200]; static const List<int> _presetDistances = [10, 15, 25, 50, 100, 200];
static const List<int> _presetShots = [3, 5, 10, 15, 20, 30]; static const List<int> _presetShots = [3, 5, 10, 15, 20, 30, 50];
@override @override
void initState() { void initState() {
@@ -44,8 +50,21 @@ class _SessionSetupScreenState extends State<SessionSetupScreen> {
if (mounted) { if (mounted) {
setState(() { setState(() {
_availableWeapons = weapons; _availableWeapons = weapons;
if (_availableWeapons.isNotEmpty && _selectedWeapon == null) {
_selectedWeapon = _availableWeapons.first; // Les armes rechargees sont de nouvelles instances (Weapon n'a pas
// d'operator ==) et l'arme selectionnee a pu etre supprimee : on
// re-resout la selection par identifiant.
final previousId = _selectedWeapon?.id;
_selectedWeapon = null;
for (final weapon in weapons) {
if (weapon.id == previousId) {
_selectedWeapon = weapon;
break;
}
}
if (_selectedWeapon == null && weapons.isNotEmpty) {
_selectedWeapon = weapons.first;
_updateSettingsForWeapon(_selectedWeapon!); _updateSettingsForWeapon(_selectedWeapon!);
} }
_isLoadingWeapons = false; _isLoadingWeapons = false;
@@ -53,6 +72,35 @@ class _SessionSetupScreenState extends State<SessionSetupScreen> {
} }
} }
Future<void> _openArmory() async {
final knownIds = _availableWeapons.map((w) => w.id).toSet();
await Navigator.push(
context,
MaterialPageRoute(builder: (_) => const WeaponListScreen()),
);
if (!mounted) return;
await _loadWeapons();
if (!mounted) return;
// Selectionne automatiquement l'arme qui vient d'etre ajoutee.
Weapon? added;
for (final weapon in _availableWeapons) {
if (!knownIds.contains(weapon.id)) {
added = weapon;
break;
}
}
if (added != null) {
final newWeapon = added;
setState(() {
_selectedWeapon = newWeapon;
_updateSettingsForWeapon(newWeapon);
});
}
}
void _updateSettingsForWeapon(Weapon weapon) { void _updateSettingsForWeapon(Weapon weapon) {
_shotsPerTarget = weapon.magazineCapacity > 0 ? weapon.magazineCapacity : 5; _shotsPerTarget = weapon.magazineCapacity > 0 ? weapon.magazineCapacity : 5;
_distance = (weapon.type == WeaponType.handgun) ? 25 : 50; _distance = (weapon.type == WeaponType.handgun) ? 25 : 50;
@@ -177,12 +225,7 @@ class _SessionSetupScreenState extends State<SessionSetupScreen> {
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
ElevatedButton.icon( ElevatedButton.icon(
onPressed: () { onPressed: _openArmory,
Navigator.push(
context,
MaterialPageRoute(builder: (_) => const WeaponListScreen()),
).then((_) => _loadWeapons());
},
icon: const Icon(Icons.shield), icon: const Icon(Icons.shield),
label: const Text('Aller à l\'armurerie'), label: const Text('Aller à l\'armurerie'),
), ),
@@ -213,26 +256,60 @@ class _SessionSetupScreenState extends State<SessionSetupScreen> {
], ],
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
DropdownButtonFormField<Weapon>( DropdownButtonFormField<String>(
initialValue: _selectedWeapon, key: _weaponFieldKey,
initialValue: _selectedWeapon?.id,
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: 'Arme utilisée', labelText: 'Arme utilisée',
prefixIcon: Icon(Icons.shield_outlined), prefixIcon: Icon(Icons.shield_outlined),
), ),
items: _availableWeapons items: [
.map( ..._availableWeapons.map(
(w) => DropdownMenuItem( (w) => DropdownMenuItem(
value: w, value: w.id,
child: Text('${w.displayName} (${w.caliber})'), child: Text('${w.displayName} (${w.caliber})'),
), ),
) ),
.toList(), DropdownMenuItem(
value: _addWeaponValue,
child: Row(
children: [
Icon(
Icons.add_circle_outline,
size: 18,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
Text(
'Ajouter une nouvelle arme',
style: TextStyle(
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.primary,
),
),
],
),
),
],
onChanged: (value) { onChanged: (value) {
if (value != null) { if (value == null) return;
setState(() {
_selectedWeapon = value; if (value == _addWeaponValue) {
_updateSettingsForWeapon(value); // L'item d'action n'est pas une arme : on restaure aussitot
}); // la selection precedente avant d'ouvrir l'armurerie.
_weaponFieldKey.currentState?.didChange(_selectedWeapon?.id);
_openArmory();
return;
}
for (final weapon in _availableWeapons) {
if (weapon.id == value) {
setState(() {
_selectedWeapon = weapon;
_updateSettingsForWeapon(weapon);
});
break;
}
} }
}, },
), ),