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 {
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<String, dynamic> 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<PlatEditPage> {
Expanded(
child: InkWell(
onTap: () async {
final result = await showDialog<
List<MenuCategory>
>(
final result = await showDialog<MenuCategory?>(
context: context,
builder: (context) {
// temp inside a StatefulBuilder so state changes refresh UI
Set<int> 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<PlatEditPage> {
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<PlatEditPage> {
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<PlatEditPage> {
},
);
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:
child:
selectedCategories.isEmpty
? [
const Text(
? const Text(
"Aucune sélection",
style: TextStyle(
color: Colors.grey,
),
),
]
: selectedCategories
.map(
(c) => Chip(
label: Text(c.nom),
visualDensity:
VisualDensity.compact,
),
style: TextStyle(color: Colors.grey),
)
.toList(),
),
: Text(selectedCategories.first.nom),
),
),
),

36
lib/pages/menus_screen.dart

@ -254,21 +254,12 @@ class _PlatsManagementScreenState extends State<PlatsManagementScreen> {
),
Row(
children: [
if (p.categories.isNotEmpty)
Wrap(
spacing: 4,
runSpacing: 4,
children:
p.categories
.map(
(c) => CategoryChip(
label: c.nom,
if (p.category != null)
CategoryChip(
label: p.category!.nom,
color: Colors.black,
),
)
.toList(),
),
if (p.categories.isEmpty)
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<MenuCategory> 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<String, dynamic> 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<EditPlatDialog> {
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<void> submit() async {
@ -542,6 +533,7 @@ class _EditPlatDialogState extends State<EditPlatDialog> {
isExpanded: true,
items:
widget.categories
.toSet()
.map(
(c) => DropdownMenuItem(value: c, child: Text(c.nom)),
)

Loading…
Cancel
Save