Refactoring + Version améliorée
This commit is contained in:
119
lib/presentation/widgets/category_field.dart
Normal file
119
lib/presentation/widgets/category_field.dart
Normal file
@@ -0,0 +1,119 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' as rootBundle;
|
||||
|
||||
class CategoryField extends StatefulWidget {
|
||||
final FormFieldSetter<String> onSaved;
|
||||
|
||||
const CategoryField({Key? key, required this.onSaved}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CategoryFieldState createState() => _CategoryFieldState();
|
||||
}
|
||||
|
||||
class _CategoryFieldState extends State<CategoryField> {
|
||||
String? _selectedCategory;
|
||||
Map<String, List<String>> _categoryMap = {}; // Map pour stocker les catégories et sous-catégories
|
||||
List<DropdownMenuItem<String>> _dropdownItems = []; // Liste pour stocker les éléments de menu déroulant
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadCategories(); // Charger les catégories à partir du JSON
|
||||
}
|
||||
|
||||
// Charger les catégories depuis le fichier JSON
|
||||
Future<void> _loadCategories() async {
|
||||
try {
|
||||
final String jsonString = await rootBundle.rootBundle.loadString('lib/assets/json/event_categories.json');
|
||||
final Map<String, dynamic> jsonMap = json.decode(jsonString);
|
||||
|
||||
final Map<String, List<String>> categoryMap = {};
|
||||
jsonMap['categories'].forEach((key, value) {
|
||||
categoryMap[key] = List<String>.from(value);
|
||||
});
|
||||
|
||||
setState(() {
|
||||
_categoryMap = categoryMap;
|
||||
_dropdownItems = _buildDropdownItems();
|
||||
});
|
||||
|
||||
// Ajouter un log pour vérifier si les catégories sont bien chargées
|
||||
print("Catégories chargées: $_categoryMap");
|
||||
} catch (e) {
|
||||
print("Erreur lors du chargement des catégories : $e");
|
||||
}
|
||||
}
|
||||
|
||||
// Construire les éléments du menu déroulant avec catégorisation
|
||||
List<DropdownMenuItem<String>> _buildDropdownItems() {
|
||||
List<DropdownMenuItem<String>> items = [];
|
||||
|
||||
_categoryMap.forEach((category, subcategories) {
|
||||
items.add(
|
||||
DropdownMenuItem<String>(
|
||||
enabled: false,
|
||||
child: Text(
|
||||
category,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white70,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
for (String subcategory in subcategories) {
|
||||
items.add(
|
||||
DropdownMenuItem<String>(
|
||||
value: subcategory,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0),
|
||||
child: Text(
|
||||
subcategory,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Ajouter un log pour vérifier si les éléments sont bien créés
|
||||
print("Éléments créés pour le menu déroulant: ${items.length}");
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _dropdownItems.isEmpty
|
||||
? CircularProgressIndicator() // Affiche un indicateur de chargement si les éléments ne sont pas encore prêts
|
||||
: DropdownButtonFormField<String>(
|
||||
value: _selectedCategory,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Catégorie',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withOpacity(0.1),
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
prefixIcon: const Icon(Icons.category, color: Colors.white70),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
dropdownColor: const Color(0xFF2C2C3E),
|
||||
iconEnabledColor: Colors.white70,
|
||||
items: _dropdownItems,
|
||||
onChanged: (String? newValue) {
|
||||
setState(() {
|
||||
_selectedCategory = newValue;
|
||||
});
|
||||
},
|
||||
onSaved: widget.onSaved,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user