Browse Source

correction gestion menus

master
Stephane 4 months ago
parent
commit
63c64f3f4b
  1. 84
      lib/pages/PlatEdit_screen.dart
  2. 36
      lib/pages/menus_screen.dart

84
lib/pages/PlatEdit_screen.dart

@ -26,16 +26,11 @@ class MenuPlat {
class MenuCategory { class MenuCategory {
final int id; final int id;
final String nom; 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<String, dynamic> json) { factory MenuCategory.fromJson(Map<String, dynamic> json) {
return MenuCategory( return MenuCategory(id: json['id'], nom: json['nom']);
id: json['id'],
nom: json['nom'],
description: json['description'],
);
} }
@override @override
@ -227,16 +222,13 @@ class _PlatEditPageState extends State<PlatEditPage> {
Expanded( Expanded(
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
final result = await showDialog< final result = await showDialog<MenuCategory?>(
List<MenuCategory>
>(
context: context, context: context,
builder: (context) { builder: (context) {
// temp inside a StatefulBuilder so state changes refresh UI MenuCategory? temp =
Set<int> temp = selectedCategories.isNotEmpty
selectedCategories ? selectedCategories.first
.map((e) => e.id) : null;
.toSet();
return StatefulBuilder( return StatefulBuilder(
builder: (context, setStateDialog) { builder: (context, setStateDialog) {
@ -249,18 +241,15 @@ class _PlatEditPageState extends State<PlatEditPage> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: children:
widget.categories.map((cat) { widget.categories.map((cat) {
return CheckboxListTile( return RadioListTile<
value: temp.contains( MenuCategory
cat.id, >(
), value: cat,
groupValue: temp,
title: Text(cat.nom), title: Text(cat.nom),
onChanged: (checked) { onChanged: (value) {
setStateDialog(() { setStateDialog(() {
if (checked == true) { temp = value;
temp.add(cat.id);
} else {
temp.remove(cat.id);
}
}); });
}, },
); );
@ -271,15 +260,11 @@ class _PlatEditPageState extends State<PlatEditPage> {
actions: [ actions: [
TextButton( TextButton(
onPressed: () { onPressed: () {
final newList = if (temp != null) {
widget.categories Navigator.pop(context, temp);
.where( } else {
(cat) => temp.contains( Navigator.pop(context, null);
cat.id, }
),
)
.toList();
Navigator.pop(context, newList);
}, },
child: const Text('OK'), child: const Text('OK'),
), ),
@ -290,37 +275,22 @@ class _PlatEditPageState extends State<PlatEditPage> {
}, },
); );
if (result != null) if (result != null) {
setState(() => selectedCategories = result); setState(() => selectedCategories = [result]);
}
}, },
child: InputDecorator( child: InputDecorator(
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: "Catégories *", labelText: "Catégorie *",
border: OutlineInputBorder(gapPadding: 2), border: OutlineInputBorder(gapPadding: 2),
), ),
child: Wrap( child:
spacing: 6,
runSpacing: -8,
children:
selectedCategories.isEmpty selectedCategories.isEmpty
? [ ? const Text(
const Text(
"Aucune sélection", "Aucune sélection",
style: TextStyle( style: TextStyle(color: Colors.grey),
color: Colors.grey,
),
),
]
: selectedCategories
.map(
(c) => Chip(
label: Text(c.nom),
visualDensity:
VisualDensity.compact,
),
) )
.toList(), : Text(selectedCategories.first.nom),
),
), ),
), ),
), ),

36
lib/pages/menus_screen.dart

@ -254,21 +254,12 @@ class _PlatsManagementScreenState extends State<PlatsManagementScreen> {
), ),
Row( Row(
children: [ children: [
if (p.categories.isNotEmpty) if (p.category != null)
Wrap( CategoryChip(
spacing: 4, label: p.category!.nom,
runSpacing: 4,
children:
p.categories
.map(
(c) => CategoryChip(
label: c.nom,
color: Colors.black, color: Colors.black,
),
) )
.toList(), else
),
if (p.categories.isEmpty)
const CategoryChip( const CategoryChip(
label: "Catégorie", label: "Catégorie",
color: Colors.black, color: Colors.black,
@ -378,8 +369,7 @@ class MenuPlat {
final String? commentaire; final String? commentaire;
final double prix; final double prix;
final String? ingredients; final String? ingredients;
final int disponible = 1; // Default to true final MenuCategory? category; // single category
final List<MenuCategory> categories; // NOW A LIST!
MenuPlat({ MenuPlat({
required this.id, required this.id,
@ -387,7 +377,7 @@ class MenuPlat {
this.commentaire, this.commentaire,
required this.prix, required this.prix,
this.ingredients, this.ingredients,
required this.categories, this.category,
}); });
factory MenuPlat.fromJson(Map<String, dynamic> json) { factory MenuPlat.fromJson(Map<String, dynamic> json) {
@ -404,12 +394,14 @@ class MenuPlat {
commentaire: json['commentaire'], commentaire: json['commentaire'],
prix: parsePrix(json['prix']), prix: parsePrix(json['prix']),
ingredients: json['ingredients'], ingredients: json['ingredients'],
categories: category:
(json['categories'] as List? ?? []) json['category'] != null
.map((c) => MenuCategory.fromJson(c)) ? MenuCategory.fromJson(json['category'])
.toList(), : null,
); );
} }
get disponible => null;
} }
class CategoryChip extends StatelessWidget { class CategoryChip extends StatelessWidget {
@ -477,8 +469,7 @@ class _EditPlatDialogState extends State<EditPlatDialog> {
prix = widget.plat.prix; prix = widget.plat.prix;
var disponible = widget.plat.disponible; var disponible = widget.plat.disponible;
// cat = (widget.plat.categories) as MenuCategory?; // cat = (widget.plat.categories) as MenuCategory?;
cat = cat = widget.plat.category;
widget.plat.categories.isNotEmpty ? widget.plat.categories.first : null;
} }
Future<void> submit() async { Future<void> submit() async {
@ -542,6 +533,7 @@ class _EditPlatDialogState extends State<EditPlatDialog> {
isExpanded: true, isExpanded: true,
items: items:
widget.categories widget.categories
.toSet()
.map( .map(
(c) => DropdownMenuItem(value: c, child: Text(c.nom)), (c) => DropdownMenuItem(value: c, child: Text(c.nom)),
) )

Loading…
Cancel
Save