From e4628e977fc9875a3b7fd3f0e2e425e063901d74 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Fri, 3 May 2024 16:00:49 +0300 Subject: [PATCH] Added tODO --- lib/pages/basket.dart | 83 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/lib/pages/basket.dart b/lib/pages/basket.dart index 6f97f8a..bbcd739 100644 --- a/lib/pages/basket.dart +++ b/lib/pages/basket.dart @@ -42,6 +42,7 @@ List> cart = [ } ]; +//TODO: Сделать правильное удаление из корзины по одному class BasketPage extends StatefulWidget { const BasketPage({super.key}); @@ -63,7 +64,6 @@ class _BasketPageState extends State { .toList(); totalPrice = cartItems.fold(0, (sum, item) => sum + int.parse(item['price'])); - debugPrint(totalPrice.toString()); }); }); } @@ -77,6 +77,77 @@ class _BasketPageState extends State { await removeItemFromCart(id); } + Future _deleteItemAlert(String id, String name) async { + return showDialog( + context: context, + barrierDismissible: true, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Удаление из корзины'), + content: SingleChildScrollView( + child: ListBody( + children: [ + Text('Вы действительно хотите убрать "$name" из корзины?'), + ], + ), + ), + actions: [ + TextButton( + child: const Text('Отмена'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + TextButton( + child: const Text('Удалить'), + onPressed: () { + removeItem(id); + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + } + + Future _clearCartAlert() async { + return showDialog( + context: context, + barrierDismissible: true, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Очистка корзины'), + content: const SingleChildScrollView( + child: ListBody( + children: [ + Text('Вы действительно хотите очистить корзину?'), + ], + ), + ), + actions: [ + TextButton( + child: const Text('Отмена'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + TextButton( + child: const Text('Очистить'), + onPressed: () { + clearCart(); + setState(() { + cartItems = []; + }); + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -125,7 +196,8 @@ class _BasketPageState extends State { item['image'], width: 50, ), - onTap: () => removeItem(item['id']), + onTap: () => + _deleteItemAlert(item['id'], item['name']), ); }, ), @@ -153,12 +225,7 @@ class _BasketPageState extends State { ), const SizedBox(height: 10), ElevatedButton( - onPressed: () async { - await clearCart(); - setState(() { - cartItems = []; - }); - }, + onPressed: _clearCartAlert, style: ElevatedButton.styleFrom( backgroundColor: Theme.of(context).primaryColor, shape: const RoundedRectangleBorder(