add cart
This commit is contained in:
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