Skip to content
Snippets Groups Projects
Commit faa193fa authored by DAI Leslie's avatar DAI Leslie
Browse files

addclub avec facilitation d'ajout de président et co-président

parent fec4efc2
Branches
No related tags found
No related merge requests found
import 'package:atlub/Mods/Theme_colors/colors.dart';
import 'package:dropdown_search/dropdown_search.dart';
import 'package:flutter/material.dart';
import 'package:atlub/Services/database.dart';
import 'package:atlub/userprovider.dart';
import 'package:provider/provider.dart';
class AddClub extends StatefulWidget {
final String BDX;
const AddClub({super.key, required this.BDX});
......@@ -20,18 +20,26 @@ class _AddClubState extends State<AddClub> {
String selectedBDXType = '';
String? selectedPrez;
String? selectedCoPrez;
List<Map<String, dynamic>> profileList = []; // Liste des profils triés
@override
void initState() {
super.initState();
selectedBDXType = widget.BDX;
loadProfiles(); // Charger les profils lors de l'initialisation de l'état
}
@override
void dispose() {
clubController.dispose();
descripController.dispose();
super.dispose();
Future<void> loadProfiles() async {
try {
Map<String, Map<String, dynamic>> profilesData = await DatabaseService().getProfiles();
// Extraire les valeurs des profils triés pour DropdownSearch
List<Map<String, dynamic>> sortedProfiles = profilesData.values.toList();
setState(() {
profileList = sortedProfiles;
});
} catch (error) {
print("Erreur lors du chargement des profils : $error");
}
}
@override
......@@ -48,65 +56,74 @@ class _AddClubState extends State<AddClub> {
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Ajouter un club', style: TextStyle(color: AppColor.violetBlue, fontSize: 30, fontWeight: FontWeight.bold),),
const Text(
'Ajouter un club',
style: TextStyle(color: AppColor.violetBlue, fontSize: 30, fontWeight: FontWeight.bold),
),
const SizedBox(height: 30),
MyTextField(label: 'Club', hint: 'Entrez le nom du club', controller: clubController, isRequired: true),
MyTextField(
label: 'Club',
hint: 'Entrez le nom du club',
controller: clubController,
isRequired: true,
),
const SizedBox(height: 20),
FutureBuilder<Map<String, Map<String, dynamic>>>(
future: DatabaseService().getProfiles(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
Map<String, Map<String, dynamic>> profiles = snapshot.data!;
List<DropdownMenuItem<String>> profileItems = profiles.entries.map((entry) {
String profileId = entry.key;
Map<String, dynamic> profileData = entry.value;
String displayName = '${profileData['nom']} ${profileData['prenom']}';
return DropdownMenuItem<String>(
value: profileId,
child: Text(displayName),
);
}).toList();
return Column(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
DropdownButtonFormField<String>(
items: profileItems,
onChanged: (value) {
Text('Président', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
DropdownSearch<Map<String, dynamic>>(
popupProps: PopupProps.menu(
showSelectedItems: false,
showSearchBox: true, // Ajout de la barre de recherche
),
items: profileList,
itemAsString: (profile) => '${profile['nom']} ${profile['prenom']}',
dropdownDecoratorProps: DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
hintText: "Sélectionner le Président",
border: OutlineInputBorder(),
),
),
onChanged: (profile) {
setState(() {
selectedPrez = value;
selectedPrez = profile?['uid'];
});
},
decoration: InputDecoration(
labelText: "Président",
),
const SizedBox(height: 20),
Text('Co-président', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
DropdownSearch<Map<String, dynamic>>(
popupProps: PopupProps.menu(
showSelectedItems: false,
showSearchBox: true, // Ajout de la barre de recherche
),
items: profileList,
itemAsString: (profile) => '${profile['nom']} ${profile['prenom']}',
dropdownDecoratorProps: DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
hintText: "Sélectionner le Co-président",
border: OutlineInputBorder(),
),
validator: (value) => value == null ? 'Président requis' : null,
),
const SizedBox(height: 20),
DropdownButtonFormField<String>(
items: profileItems,
onChanged: (value) {
onChanged: (profile) {
setState(() {
selectedCoPrez = value;
selectedCoPrez = profile?['uid'];
});
},
decoration: InputDecoration(
labelText: "Co-président",
border: OutlineInputBorder(),
),
),
],
);
}
),
const SizedBox(height: 20),
MyTextField(label: 'Description', hint: 'Entrez la description du club', controller: descripController),
MyTextField(
label: 'Description',
hint: 'Entrez la description du club',
controller: descripController,
),
const SizedBox(height: 20),
Container(
margin: const EdgeInsets.only(bottom: 10),
......@@ -124,60 +141,67 @@ class _AddClubState extends State<AddClub> {
},
),
),
SendButton(onTap: () {
const SizedBox(height: 20),
SendButton(onTap: () async {
if (_formKey.currentState!.validate()) {
final club = clubController.text;
final prez = selectedPrez;
final coprez = selectedCoPrez;
final descrip = descripController.text;
// Afficher la valeur de l'ID du président pour le débogage
print('ID du président sélectionné: $prez');
if (prez == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Le Président est obligatoire")),
);
return;
}
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Envoyé")));
FocusScope.of(context).requestFocus(FocusNode());
_formKey.currentState!.reset();
DatabaseService db = DatabaseService();
bool clubOk = true;
bool prezOk = prez != null;
bool coprezOk = coprez != null ? true : false;
// Attendre un court délai avant de vérifier à nouveau si le président est sélectionné
await Future.delayed(Duration(milliseconds: 100));
if (prezOk && coprezOk && clubOk) {
Future<dynamic> idclub = db.addclub(selectedBDXType, club, descrip);
if (selectedPrez == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Le Président est obligatoire")),
);
return;
}
Future<String?> idprez = db.getProfilIdFromName(prez!);
Future<String?> idcoprez = db.getProfilIdFromName(coprez ?? '');
DatabaseService db = DatabaseService();
Future.wait([idprez, idcoprez, idclub]).then((List<dynamic?> results) {
String? idprez = results[0] as String?;
String? idcoprez = results[1] as String?;
String idclub = results[2] as String;
try {
final idclub = await db.addclub(selectedBDXType, club, descrip);
if (idprez != null) {
db.addPresident(idprez, idclub, "current");
} else {
print("L'ID du président est null.");
if (prez != null) {
await db.addPresident(prez, idclub, "current");
}
if (idcoprez != null && coprez!.isNotEmpty) {
db.addPresident(idcoprez, idclub, "co");
} else {
print("L'ID du co-président est null.");
if (coprez != null && coprez.isNotEmpty) {
await db.addPresident(coprez, idclub, "co");
}
}).catchError((error) {
Navigator.pop(context);
} catch (error) {
print("Une erreur s'est produite : $error");
});
}
Navigator.pop(context);
}
}),
],
),
),
),
),
),
);
}
}
......@@ -205,6 +229,7 @@ class MyTextField extends StatelessWidget {
border: OutlineInputBorder(),
),
validator: (value) {
if (isRequired && (value == null || value.isEmpty)) {
return 'Champ requis';
}
......@@ -227,3 +252,4 @@ class SendButton extends StatelessWidget {
);
}
}
......@@ -173,10 +173,10 @@ packages:
dependency: "direct main"
description:
name: dropdown_search
sha256: "4d1106c0a392a2cd9273e13a14055a3faa945f14e9e28bb2832a3a8a70e2e55d"
sha256: "55106e8290acaa97ed15bea1fdad82c3cf0c248dd410e651f5a8ac6870f783ab"
url: "https://pub.dev"
source: hosted
version: "4.0.1"
version: "5.0.6"
equatable:
dependency: transitive
description:
......
......@@ -31,7 +31,7 @@ dependencies:
flutter:
sdk: flutter
salomon_bottom_bar: ^3.3.2
dropdown_search: ^4.0.0
dropdown_search: ^5.0.6
#file_picker: ^4.0.0
#file_picker: ^8.0.0+1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment