Clearing shopping cart

This commit is contained in:
2024-05-03 13:59:06 +03:00
parent 5c3da0964a
commit 4bbe7fbc0b
2 changed files with 22 additions and 0 deletions

View File

@@ -151,6 +151,23 @@ class _BasketPageState extends State<BasketPage> {
),
child: const Text('Оформить заказ'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
await clearCart();
setState(() {
cartItems = [];
});
},
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).primaryColor,
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(50))),
foregroundColor: Colors.white,
),
child: const Text('Очистить корзину'),
),
],
),
),

View File

@@ -40,3 +40,8 @@ Future<void> removeItemFromCart(String id) async {
}
prefs.setString('cart', jsonEncode(cart));
}
Future<void> clearCart() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('cart', "[]");
}