feat: product variant
This commit is contained in:
@@ -25,15 +25,29 @@ class CheckoutBloc extends Bloc<CheckoutEvent, CheckoutState> {
|
||||
on<_AddItem>((event, emit) {
|
||||
var currentState = state as _Loaded;
|
||||
List<ProductQuantity> items = [...currentState.items];
|
||||
var index =
|
||||
items.indexWhere((element) => element.product.id == event.product.id);
|
||||
var index = items.indexWhere(
|
||||
(element) =>
|
||||
element.product.id == event.product.id &&
|
||||
element.variant?.id == event.variant?.id,
|
||||
);
|
||||
emit(const _Loading());
|
||||
if (index != -1) {
|
||||
items[index] = ProductQuantity(
|
||||
product: event.product, quantity: items[index].quantity + 1);
|
||||
product: event.product,
|
||||
quantity: items[index].quantity + 1,
|
||||
variant: event.variant,
|
||||
);
|
||||
} else {
|
||||
items.add(ProductQuantity(product: event.product, quantity: 1));
|
||||
items.add(
|
||||
ProductQuantity(
|
||||
product: event.product,
|
||||
quantity: 1,
|
||||
variant: event.variant,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
log("Items: $items");
|
||||
emit(_Loaded(
|
||||
items,
|
||||
currentState.discountModel,
|
||||
@@ -50,13 +64,19 @@ class CheckoutBloc extends Bloc<CheckoutEvent, CheckoutState> {
|
||||
on<_RemoveItem>((event, emit) {
|
||||
var currentState = state as _Loaded;
|
||||
List<ProductQuantity> items = [...currentState.items];
|
||||
var index =
|
||||
items.indexWhere((element) => element.product.id == event.product.id);
|
||||
var index = items.indexWhere(
|
||||
(element) =>
|
||||
element.product.id == event.product.id &&
|
||||
element.variant?.id == event.variant?.id,
|
||||
);
|
||||
emit(const _Loading());
|
||||
if (index != -1) {
|
||||
if (items[index].quantity > 1) {
|
||||
items[index] = ProductQuantity(
|
||||
product: event.product, quantity: items[index].quantity - 1);
|
||||
product: event.product,
|
||||
quantity: items[index].quantity - 1,
|
||||
variant: event.variant);
|
||||
;
|
||||
} else {
|
||||
items.removeAt(index);
|
||||
}
|
||||
@@ -77,8 +97,11 @@ class CheckoutBloc extends Bloc<CheckoutEvent, CheckoutState> {
|
||||
on<_DeleteItem>((event, emit) {
|
||||
var currentState = state as _Loaded;
|
||||
List<ProductQuantity> items = [...currentState.items];
|
||||
var index =
|
||||
items.indexWhere((element) => element.product.id == event.product.id);
|
||||
var index = items.indexWhere(
|
||||
(element) =>
|
||||
element.product.id == event.product.id &&
|
||||
element.variant?.id == event.variant?.id,
|
||||
);
|
||||
emit(const _Loading());
|
||||
|
||||
items.removeAt(index);
|
||||
|
||||
Reference in New Issue
Block a user