import 'package:flutter/foundation.dart'; import '../models/cart_item.dart'; import '../models/product.dart'; class CartService extends ChangeNotifier { final List _items = []; List get items => List.unmodifiable(_items); int get totalItems => _items.fold(0, (sum, item) => sum + item.quantity); double get totalPrice => _items.fold(0.0, (sum, item) => sum + item.lineTotal); void addProduct(Product product) { final index = _items.indexWhere((item) => item.product.id == product.id); if (index == -1) { _items.add(CartItem(product: product)); } else { _items[index].quantity += 1; } notifyListeners(); } void removeProduct(String productId) { _items.removeWhere((item) => item.product.id == productId); notifyListeners(); } void updateQuantity(String productId, int quantity) { if (quantity < 1) return; final index = _items.indexWhere((item) => item.product.id == productId); if (index == -1) return; _items[index].quantity = quantity; notifyListeners(); } void clear() { _items.clear(); notifyListeners(); } }