Add: CartProvider

This commit is contained in:
2024-05-15 13:49:18 +03:00
parent e52357edf5
commit c54176212a
5 changed files with 89 additions and 54 deletions

View File

@@ -3,9 +3,11 @@ import 'package:gymlink_module_web/components/app_bar.dart';
import 'package:gymlink_module_web/components/basket_item_card.dart';
import 'package:gymlink_module_web/components/heading.dart';
import 'package:gymlink_module_web/pages/order_confirmation.dart';
import 'package:gymlink_module_web/providers/cart.dart';
import 'package:gymlink_module_web/tools/prefs.dart';
import 'package:gymlink_module_web/tools/routes.dart';
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
import 'package:provider/provider.dart';
List<Map<String, dynamic>> cart = [
{
@@ -73,6 +75,10 @@ class _BasketPageState extends State<BasketPage> {
});
}
void _updateCart() {
Provider.of<CartProvider>(context, listen: false).updateCartLength();
}
void removeItem(String id) async {
final item = cartItems.firstWhere((element) => element['id'] == id);
bool toDelete = false;
@@ -136,6 +142,9 @@ class _BasketPageState extends State<BasketPage> {
setState(() {
cartItems.removeWhere((element) => element['id'] == id);
});
if (mounted) {
_updateCart();
}
Navigator.of(context).pop();
},
),
@@ -173,6 +182,9 @@ class _BasketPageState extends State<BasketPage> {
setState(() {
cartItems = [];
});
if (mounted) {
_updateCart();
}
Navigator.of(context).pop();
},
),