runable
This commit is contained in:
54
lib/features/home/presentation/providers/cart_provider.dart
Normal file
54
lib/features/home/presentation/providers/cart_provider.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import '../../domain/entities/cart_item.dart';
|
||||
|
||||
part 'cart_provider.g.dart';
|
||||
|
||||
/// Provider for shopping cart
|
||||
@riverpod
|
||||
class Cart extends _$Cart {
|
||||
@override
|
||||
Future<List<CartItem>> build() async {
|
||||
// TODO: Implement with repository
|
||||
return [];
|
||||
}
|
||||
|
||||
Future<void> addItem(CartItem item) async {
|
||||
// TODO: Implement add to cart
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
final currentItems = state.value ?? [];
|
||||
return [...currentItems, item];
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> removeItem(String productId) async {
|
||||
// TODO: Implement remove from cart
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
final currentItems = state.value ?? [];
|
||||
return currentItems.where((item) => item.productId != productId).toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateQuantity(String productId, int quantity) async {
|
||||
// TODO: Implement update quantity
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
final currentItems = state.value ?? [];
|
||||
return currentItems.map((item) {
|
||||
if (item.productId == productId) {
|
||||
return item.copyWith(quantity: quantity);
|
||||
}
|
||||
return item;
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> clearCart() async {
|
||||
// TODO: Implement clear cart
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
return [];
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user