diff --git a/lib/pages/PlatEdit_screen.dart b/lib/pages/PlatEdit_screen.dart index 78261bc..69cf6c5 100644 --- a/lib/pages/PlatEdit_screen.dart +++ b/lib/pages/PlatEdit_screen.dart @@ -26,16 +26,11 @@ class MenuPlat { class MenuCategory { final int id; final String nom; - final String? description; - MenuCategory({required this.id, required this.nom, this.description}); + MenuCategory({required this.id, required this.nom}); factory MenuCategory.fromJson(Map json) { - return MenuCategory( - id: json['id'], - nom: json['nom'], - description: json['description'], - ); + return MenuCategory(id: json['id'], nom: json['nom']); } @override @@ -227,16 +222,13 @@ class _PlatEditPageState extends State { Expanded( child: InkWell( onTap: () async { - final result = await showDialog< - List - >( + final result = await showDialog( context: context, builder: (context) { - // temp inside a StatefulBuilder so state changes refresh UI - Set temp = - selectedCategories - .map((e) => e.id) - .toSet(); + MenuCategory? temp = + selectedCategories.isNotEmpty + ? selectedCategories.first + : null; return StatefulBuilder( builder: (context, setStateDialog) { @@ -249,18 +241,15 @@ class _PlatEditPageState extends State { mainAxisSize: MainAxisSize.min, children: widget.categories.map((cat) { - return CheckboxListTile( - value: temp.contains( - cat.id, - ), + return RadioListTile< + MenuCategory + >( + value: cat, + groupValue: temp, title: Text(cat.nom), - onChanged: (checked) { + onChanged: (value) { setStateDialog(() { - if (checked == true) { - temp.add(cat.id); - } else { - temp.remove(cat.id); - } + temp = value; }); }, ); @@ -271,15 +260,11 @@ class _PlatEditPageState extends State { actions: [ TextButton( onPressed: () { - final newList = - widget.categories - .where( - (cat) => temp.contains( - cat.id, - ), - ) - .toList(); - Navigator.pop(context, newList); + if (temp != null) { + Navigator.pop(context, temp); + } else { + Navigator.pop(context, null); + } }, child: const Text('OK'), ), @@ -290,37 +275,22 @@ class _PlatEditPageState extends State { }, ); - if (result != null) - setState(() => selectedCategories = result); + if (result != null) { + setState(() => selectedCategories = [result]); + } }, child: InputDecorator( decoration: const InputDecoration( - labelText: "Catégories *", + labelText: "Catégorie *", border: OutlineInputBorder(gapPadding: 2), ), - child: Wrap( - spacing: 6, - runSpacing: -8, - children: - selectedCategories.isEmpty - ? [ - const Text( - "Aucune sélection", - style: TextStyle( - color: Colors.grey, - ), - ), - ] - : selectedCategories - .map( - (c) => Chip( - label: Text(c.nom), - visualDensity: - VisualDensity.compact, - ), - ) - .toList(), - ), + child: + selectedCategories.isEmpty + ? const Text( + "Aucune sélection", + style: TextStyle(color: Colors.grey), + ) + : Text(selectedCategories.first.nom), ), ), ), diff --git a/lib/pages/menus_screen.dart b/lib/pages/menus_screen.dart index 6e1be89..283101e 100644 --- a/lib/pages/menus_screen.dart +++ b/lib/pages/menus_screen.dart @@ -254,21 +254,12 @@ class _PlatsManagementScreenState extends State { ), Row( children: [ - if (p.categories.isNotEmpty) - Wrap( - spacing: 4, - runSpacing: 4, - children: - p.categories - .map( - (c) => CategoryChip( - label: c.nom, - color: Colors.black, - ), - ) - .toList(), - ), - if (p.categories.isEmpty) + if (p.category != null) + CategoryChip( + label: p.category!.nom, + color: Colors.black, + ) + else const CategoryChip( label: "Catégorie", color: Colors.black, @@ -378,8 +369,7 @@ class MenuPlat { final String? commentaire; final double prix; final String? ingredients; - final int disponible = 1; // Default to true - final List categories; // NOW A LIST! + final MenuCategory? category; // single category MenuPlat({ required this.id, @@ -387,7 +377,7 @@ class MenuPlat { this.commentaire, required this.prix, this.ingredients, - required this.categories, + this.category, }); factory MenuPlat.fromJson(Map json) { @@ -404,12 +394,14 @@ class MenuPlat { commentaire: json['commentaire'], prix: parsePrix(json['prix']), ingredients: json['ingredients'], - categories: - (json['categories'] as List? ?? []) - .map((c) => MenuCategory.fromJson(c)) - .toList(), + category: + json['category'] != null + ? MenuCategory.fromJson(json['category']) + : null, ); } + + get disponible => null; } class CategoryChip extends StatelessWidget { @@ -477,8 +469,7 @@ class _EditPlatDialogState extends State { prix = widget.plat.prix; var disponible = widget.plat.disponible; // cat = (widget.plat.categories) as MenuCategory?; - cat = - widget.plat.categories.isNotEmpty ? widget.plat.categories.first : null; + cat = widget.plat.category; } Future submit() async { @@ -542,6 +533,7 @@ class _EditPlatDialogState extends State { isExpanded: true, items: widget.categories + .toSet() .map( (c) => DropdownMenuItem(value: c, child: Text(c.nom)), )