Browse Source

commande

master
Stephane 4 months ago
parent
commit
f0fbbaf986
  1. 102
      lib/pages/cart_page.dart

102
lib/pages/cart_page.dart

@ -77,7 +77,10 @@ class _CartPageState extends State<CartPage> {
}
double _calculateTotal() {
return _cartItems.fold(0.0, (sum, item) => sum + (item.prix * item.quantity));
return _cartItems.fold(
0.0,
(sum, item) => sum + (item.prix * item.quantity),
);
}
int _getTotalArticles() {
@ -113,13 +116,13 @@ class _CartPageState extends State<CartPage> {
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(
'Annuler',
style: TextStyle(color: Colors.grey[600]),
),
child: Text('Annuler', style: TextStyle(color: Colors.grey[600])),
),
ElevatedButton(
onPressed: _isValidating ? null : () {
onPressed:
_isValidating
? null
: () {
Navigator.of(context).pop();
_validateOrder();
},
@ -127,13 +130,16 @@ class _CartPageState extends State<CartPage> {
backgroundColor: Colors.green[700],
foregroundColor: Colors.white,
),
child: _isValidating
child:
_isValidating
? SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
valueColor: AlwaysStoppedAnimation<Color>(
Colors.white,
),
),
)
: Text('Valider'),
@ -157,29 +163,36 @@ class _CartPageState extends State<CartPage> {
"reservation_id": 1, // Peut être null si pas de réservation
"serveur": "Serveur par défaut", // Valeur par défaut comme demandé
"commentaires": _getOrderComments(),
"items": _cartItems.map((item) => {
"items":
_cartItems
.map(
(item) => {
"menu_id": item.id,
"quantite": item.quantity,
"commentaires": item.notes.isNotEmpty ? item.notes : null,
}).toList(),
},
)
.toList(),
};
// Appel API pour créer la commande
final response = await http.post(
Uri.parse('https://restaurant.careeracademy.mg/api/commandes'), // Remplacez par votre URL d'API
headers: {
'Content-Type': 'application/json',
},
Uri.parse(
'https://restaurant.careeracademy.mg/api/commandes',
), // Remplacez par votre URL d'API
headers: {'Content-Type': 'application/json'},
body: json.encode(orderData),
);
print(orderData);
print('response body: ${response.body}');
if (response.statusCode == 200 || response.statusCode == 201) {
// Succès
_showSuccessDialog();
} else {
// Erreur
_showErrorDialog('Erreur lors de l\'enregistrement de la commande (${response.statusCode})');
_showErrorDialog(
'Erreur lors de l\'enregistrement de la commande (${response.statusCode})',
);
}
} catch (e) {
_showErrorDialog('Erreur de connexion: $e');
@ -192,7 +205,8 @@ class _CartPageState extends State<CartPage> {
String _getOrderComments() {
// Concaténer toutes les notes des articles pour les commentaires généraux
List<String> allNotes = _cartItems
List<String> allNotes =
_cartItems
.where((item) => item.notes.isNotEmpty)
.map((item) => '${item.nom}: ${item.notes}')
.toList();
@ -213,7 +227,9 @@ class _CartPageState extends State<CartPage> {
Text('Commande validée'),
],
),
content: Text('Votre commande a été envoyée en cuisine avec succès !'),
content: Text(
'Votre commande a été envoyée en cuisine avec succès !',
),
actions: [
ElevatedButton(
onPressed: () {
@ -281,16 +297,14 @@ class _CartPageState extends State<CartPage> {
onPressed: () => Navigator.pop(context),
child: Text(
'Retour au menu',
style: TextStyle(
color: Colors.black,
fontSize: 16,
),
style: TextStyle(color: Colors.black, fontSize: 16),
),
),
SizedBox(width: 16),
],
),
body: _cartItems.isEmpty
body:
_cartItems.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
@ -303,10 +317,7 @@ class _CartPageState extends State<CartPage> {
SizedBox(height: 16),
Text(
'Votre panier est vide',
style: TextStyle(
fontSize: 18,
color: Colors.grey[600],
),
style: TextStyle(fontSize: 18, color: Colors.grey[600]),
),
],
),
@ -320,10 +331,7 @@ class _CartPageState extends State<CartPage> {
color: Colors.white,
child: Text(
'Table ${widget.tableId}${widget.personne} personne${widget.personne > 1 ? 's' : ''}',
style: TextStyle(
fontSize: 16,
color: Colors.grey[600],
),
style: TextStyle(fontSize: 16, color: Colors.grey[600]),
),
),
@ -332,7 +340,8 @@ class _CartPageState extends State<CartPage> {
child: ListView.separated(
padding: EdgeInsets.all(16),
itemCount: _cartItems.length,
separatorBuilder: (context, index) => SizedBox(height: 12),
separatorBuilder:
(context, index) => SizedBox(height: 12),
itemBuilder: (context, index) {
final item = _cartItems[index];
return Container(
@ -352,7 +361,8 @@ class _CartPageState extends State<CartPage> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
@ -394,13 +404,18 @@ class _CartPageState extends State<CartPage> {
],
SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
// Contrôles de quantité
Row(
children: [
IconButton(
onPressed: () => _updateQuantity(index, item.quantity - 1),
onPressed:
() => _updateQuantity(
index,
item.quantity - 1,
),
icon: Icon(Icons.remove),
style: IconButton.styleFrom(
backgroundColor: Colors.grey[200],
@ -417,7 +432,11 @@ class _CartPageState extends State<CartPage> {
),
SizedBox(width: 16),
IconButton(
onPressed: () => _updateQuantity(index, item.quantity + 1),
onPressed:
() => _updateQuantity(
index,
item.quantity + 1,
),
icon: Icon(Icons.add),
style: IconButton.styleFrom(
backgroundColor: Colors.grey[200],
@ -449,9 +468,7 @@ class _CartPageState extends State<CartPage> {
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
border: Border(
top: BorderSide(color: Colors.grey[200]!),
),
border: Border(top: BorderSide(color: Colors.grey[200]!)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
@ -522,7 +539,8 @@ class _CartPageState extends State<CartPage> {
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _cartItems.isNotEmpty && !_isValidating
onPressed:
_cartItems.isNotEmpty && !_isValidating
? _showConfirmationDialog
: null,
style: ElevatedButton.styleFrom(
@ -543,7 +561,9 @@ class _CartPageState extends State<CartPage> {
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
valueColor: AlwaysStoppedAnimation<Color>(
Colors.white,
),
),
),
SizedBox(width: 8),

Loading…
Cancel
Save