add cart
This commit is contained in:
177
lib/features/cart/presentation/providers/cart_provider.dart
Normal file
177
lib/features/cart/presentation/providers/cart_provider.dart
Normal file
@@ -0,0 +1,177 @@
|
||||
/// Cart Provider
|
||||
///
|
||||
/// State management for shopping cart using Riverpod.
|
||||
library;
|
||||
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:worker/features/cart/presentation/providers/cart_state.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
|
||||
part 'cart_provider.g.dart';
|
||||
|
||||
/// Cart Notifier
|
||||
///
|
||||
/// Manages cart state including:
|
||||
/// - Adding/removing items
|
||||
/// - Updating quantities
|
||||
/// - Warehouse selection
|
||||
/// - Discount code application
|
||||
/// - Cart summary calculations
|
||||
@riverpod
|
||||
class Cart extends _$Cart {
|
||||
@override
|
||||
CartState build() {
|
||||
final initialState = CartState.initial();
|
||||
// Initialize with Diamond tier discount (15%)
|
||||
// TODO: Get actual tier from user profile
|
||||
return initialState.copyWith(
|
||||
memberTier: 'Diamond',
|
||||
memberDiscountPercent: 15.0,
|
||||
);
|
||||
}
|
||||
|
||||
/// Add product to cart
|
||||
void addToCart(Product product, {double quantity = 1.0}) {
|
||||
final existingItemIndex = state.items.indexWhere(
|
||||
(item) => item.product.productId == product.productId,
|
||||
);
|
||||
|
||||
if (existingItemIndex >= 0) {
|
||||
// Update quantity if item already exists
|
||||
updateQuantity(
|
||||
product.productId,
|
||||
state.items[existingItemIndex].quantity + quantity,
|
||||
);
|
||||
} else {
|
||||
// Add new item
|
||||
final newItem = CartItemData(
|
||||
product: product,
|
||||
quantity: quantity,
|
||||
);
|
||||
|
||||
state = state.copyWith(
|
||||
items: [...state.items, newItem],
|
||||
);
|
||||
_recalculateTotal();
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove product from cart
|
||||
void removeFromCart(String productId) {
|
||||
state = state.copyWith(
|
||||
items: state.items.where((item) => item.product.productId != productId).toList(),
|
||||
);
|
||||
_recalculateTotal();
|
||||
}
|
||||
|
||||
/// Update item quantity
|
||||
void updateQuantity(String productId, double newQuantity) {
|
||||
if (newQuantity <= 0) {
|
||||
removeFromCart(productId);
|
||||
return;
|
||||
}
|
||||
|
||||
final updatedItems = state.items.map((item) {
|
||||
if (item.product.productId == productId) {
|
||||
return item.copyWith(quantity: newQuantity);
|
||||
}
|
||||
return item;
|
||||
}).toList();
|
||||
|
||||
state = state.copyWith(items: updatedItems);
|
||||
_recalculateTotal();
|
||||
}
|
||||
|
||||
/// Increment quantity
|
||||
void incrementQuantity(String productId) {
|
||||
final item = state.items.firstWhere(
|
||||
(item) => item.product.productId == productId,
|
||||
);
|
||||
updateQuantity(productId, item.quantity + 1);
|
||||
}
|
||||
|
||||
/// Decrement quantity
|
||||
void decrementQuantity(String productId) {
|
||||
final item = state.items.firstWhere(
|
||||
(item) => item.product.productId == productId,
|
||||
);
|
||||
updateQuantity(productId, item.quantity - 1);
|
||||
}
|
||||
|
||||
/// Clear entire cart
|
||||
void clearCart() {
|
||||
state = CartState.initial();
|
||||
}
|
||||
|
||||
/// Select warehouse
|
||||
void selectWarehouse(String warehouse) {
|
||||
state = state.copyWith(selectedWarehouse: warehouse);
|
||||
}
|
||||
|
||||
/// Apply discount code
|
||||
void applyDiscountCode(String code) {
|
||||
// TODO: Validate with backend
|
||||
// For now, simulate discount application
|
||||
if (code.isNotEmpty) {
|
||||
state = state.copyWith(
|
||||
discountCode: code,
|
||||
discountCodeApplied: true,
|
||||
);
|
||||
_recalculateTotal();
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove discount code
|
||||
void removeDiscountCode() {
|
||||
state = state.copyWith(
|
||||
discountCode: null,
|
||||
discountCodeApplied: false,
|
||||
);
|
||||
_recalculateTotal();
|
||||
}
|
||||
|
||||
/// Recalculate cart totals
|
||||
void _recalculateTotal() {
|
||||
// Calculate subtotal
|
||||
final subtotal = state.items.fold<double>(
|
||||
0.0,
|
||||
(sum, item) => sum + (item.product.basePrice * item.quantity),
|
||||
);
|
||||
|
||||
// Calculate member tier discount
|
||||
final memberDiscount = subtotal * (state.memberDiscountPercent / 100);
|
||||
|
||||
// Calculate shipping (free for now)
|
||||
const shippingFee = 0.0;
|
||||
|
||||
// Calculate total
|
||||
final total = subtotal - memberDiscount + shippingFee;
|
||||
|
||||
state = state.copyWith(
|
||||
subtotal: subtotal,
|
||||
memberDiscount: memberDiscount,
|
||||
shippingFee: shippingFee,
|
||||
total: total,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get total quantity of all items
|
||||
double get totalQuantity {
|
||||
return state.items.fold<double>(
|
||||
0.0,
|
||||
(sum, item) => sum + item.quantity,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cart item count provider
|
||||
@riverpod
|
||||
int cartItemCount(Ref ref) {
|
||||
return ref.watch(cartProvider).items.length;
|
||||
}
|
||||
|
||||
/// Cart total provider
|
||||
@riverpod
|
||||
double cartTotal(Ref ref) {
|
||||
return ref.watch(cartProvider).total;
|
||||
}
|
||||
186
lib/features/cart/presentation/providers/cart_provider.g.dart
Normal file
186
lib/features/cart/presentation/providers/cart_provider.g.dart
Normal file
@@ -0,0 +1,186 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'cart_provider.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
/// Cart Notifier
|
||||
///
|
||||
/// Manages cart state including:
|
||||
/// - Adding/removing items
|
||||
/// - Updating quantities
|
||||
/// - Warehouse selection
|
||||
/// - Discount code application
|
||||
/// - Cart summary calculations
|
||||
|
||||
@ProviderFor(Cart)
|
||||
const cartProvider = CartProvider._();
|
||||
|
||||
/// Cart Notifier
|
||||
///
|
||||
/// Manages cart state including:
|
||||
/// - Adding/removing items
|
||||
/// - Updating quantities
|
||||
/// - Warehouse selection
|
||||
/// - Discount code application
|
||||
/// - Cart summary calculations
|
||||
final class CartProvider extends $NotifierProvider<Cart, CartState> {
|
||||
/// Cart Notifier
|
||||
///
|
||||
/// Manages cart state including:
|
||||
/// - Adding/removing items
|
||||
/// - Updating quantities
|
||||
/// - Warehouse selection
|
||||
/// - Discount code application
|
||||
/// - Cart summary calculations
|
||||
const CartProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'cartProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$cartHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
Cart create() => Cart();
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(CartState value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<CartState>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$cartHash() => r'fa4c957f9cd7e54000e035b0934ad2bd08ba2786';
|
||||
|
||||
/// Cart Notifier
|
||||
///
|
||||
/// Manages cart state including:
|
||||
/// - Adding/removing items
|
||||
/// - Updating quantities
|
||||
/// - Warehouse selection
|
||||
/// - Discount code application
|
||||
/// - Cart summary calculations
|
||||
|
||||
abstract class _$Cart extends $Notifier<CartState> {
|
||||
CartState build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final created = build();
|
||||
final ref = this.ref as $Ref<CartState, CartState>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<CartState, CartState>,
|
||||
CartState,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleValue(ref, created);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cart item count provider
|
||||
|
||||
@ProviderFor(cartItemCount)
|
||||
const cartItemCountProvider = CartItemCountProvider._();
|
||||
|
||||
/// Cart item count provider
|
||||
|
||||
final class CartItemCountProvider extends $FunctionalProvider<int, int, int>
|
||||
with $Provider<int> {
|
||||
/// Cart item count provider
|
||||
const CartItemCountProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'cartItemCountProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$cartItemCountHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<int> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
int create(Ref ref) {
|
||||
return cartItemCount(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(int value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<int>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$cartItemCountHash() => r'4ddc2979030a4470b2fa1de4832a84313e98e259';
|
||||
|
||||
/// Cart total provider
|
||||
|
||||
@ProviderFor(cartTotal)
|
||||
const cartTotalProvider = CartTotalProvider._();
|
||||
|
||||
/// Cart total provider
|
||||
|
||||
final class CartTotalProvider
|
||||
extends $FunctionalProvider<double, double, double>
|
||||
with $Provider<double> {
|
||||
/// Cart total provider
|
||||
const CartTotalProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'cartTotalProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$cartTotalHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<double> $createElement($ProviderPointer pointer) =>
|
||||
$ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
double create(Ref ref) {
|
||||
return cartTotal(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(double value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<double>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$cartTotalHash() => r'48460600487e734788e6d6cf1e4f7e13d21f21a4';
|
||||
111
lib/features/cart/presentation/providers/cart_state.dart
Normal file
111
lib/features/cart/presentation/providers/cart_state.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
/// Cart State
|
||||
///
|
||||
/// Immutable state class for cart management.
|
||||
library;
|
||||
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
|
||||
/// Cart Item Data
|
||||
///
|
||||
/// Represents a product in the cart with quantity.
|
||||
class CartItemData {
|
||||
final Product product;
|
||||
final double quantity;
|
||||
|
||||
const CartItemData({
|
||||
required this.product,
|
||||
required this.quantity,
|
||||
});
|
||||
|
||||
/// Calculate line total
|
||||
double get lineTotal => product.basePrice * quantity;
|
||||
|
||||
CartItemData copyWith({
|
||||
Product? product,
|
||||
double? quantity,
|
||||
}) {
|
||||
return CartItemData(
|
||||
product: product ?? this.product,
|
||||
quantity: quantity ?? this.quantity,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cart State
|
||||
///
|
||||
/// Represents the complete state of the shopping cart.
|
||||
class CartState {
|
||||
final List<CartItemData> items;
|
||||
final String selectedWarehouse;
|
||||
final String? discountCode;
|
||||
final bool discountCodeApplied;
|
||||
final String memberTier;
|
||||
final double memberDiscountPercent;
|
||||
final double subtotal;
|
||||
final double memberDiscount;
|
||||
final double shippingFee;
|
||||
final double total;
|
||||
|
||||
const CartState({
|
||||
required this.items,
|
||||
required this.selectedWarehouse,
|
||||
this.discountCode,
|
||||
required this.discountCodeApplied,
|
||||
required this.memberTier,
|
||||
required this.memberDiscountPercent,
|
||||
required this.subtotal,
|
||||
required this.memberDiscount,
|
||||
required this.shippingFee,
|
||||
required this.total,
|
||||
});
|
||||
|
||||
factory CartState.initial() {
|
||||
return const CartState(
|
||||
items: [],
|
||||
selectedWarehouse: 'Kho Hà Nội - Nguyễn Trãi',
|
||||
discountCode: null,
|
||||
discountCodeApplied: false,
|
||||
memberTier: '',
|
||||
memberDiscountPercent: 0.0,
|
||||
subtotal: 0.0,
|
||||
memberDiscount: 0.0,
|
||||
shippingFee: 0.0,
|
||||
total: 0.0,
|
||||
);
|
||||
}
|
||||
|
||||
bool get isEmpty => items.isEmpty;
|
||||
bool get isNotEmpty => items.isNotEmpty;
|
||||
int get itemCount => items.length;
|
||||
|
||||
/// Get total quantity across all items
|
||||
double get totalQuantity {
|
||||
return items.fold<double>(0.0, (sum, item) => sum + item.quantity);
|
||||
}
|
||||
|
||||
CartState copyWith({
|
||||
List<CartItemData>? items,
|
||||
String? selectedWarehouse,
|
||||
String? discountCode,
|
||||
bool? discountCodeApplied,
|
||||
String? memberTier,
|
||||
double? memberDiscountPercent,
|
||||
double? subtotal,
|
||||
double? memberDiscount,
|
||||
double? shippingFee,
|
||||
double? total,
|
||||
}) {
|
||||
return CartState(
|
||||
items: items ?? this.items,
|
||||
selectedWarehouse: selectedWarehouse ?? this.selectedWarehouse,
|
||||
discountCode: discountCode ?? this.discountCode,
|
||||
discountCodeApplied: discountCodeApplied ?? this.discountCodeApplied,
|
||||
memberTier: memberTier ?? this.memberTier,
|
||||
memberDiscountPercent: memberDiscountPercent ?? this.memberDiscountPercent,
|
||||
subtotal: subtotal ?? this.subtotal,
|
||||
memberDiscount: memberDiscount ?? this.memberDiscount,
|
||||
shippingFee: shippingFee ?? this.shippingFee,
|
||||
total: total ?? this.total,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user