first commit
This commit is contained in:
parent
215877afc4
commit
50b0828726
68
lib/controller/cart_controller.dart
Normal file
68
lib/controller/cart_controller.dart
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import 'package:get/get.dart';
|
||||||
|
import '../models/product_model.dart';
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
|
import '../models/product_model1.dart';
|
||||||
|
|
||||||
|
class CartController extends GetxController {
|
||||||
|
|
||||||
|
var cartList = <Product>[].obs;
|
||||||
|
var cartCount = 0.obs;
|
||||||
|
var totalPrice = 0.0.obs;
|
||||||
|
var subtotal = 0.0.obs;
|
||||||
|
var gstTotal = 0.0.obs;
|
||||||
|
var grandTotal = 0.0.obs;
|
||||||
|
|
||||||
|
// Add item to cart
|
||||||
|
void addToCart(Product product) {
|
||||||
|
var existingProduct = cartList.firstWhereOrNull((p) => p.id == product.id);
|
||||||
|
if (existingProduct != null) {
|
||||||
|
// Update quantity if already in cart
|
||||||
|
existingProduct.quantity++;
|
||||||
|
} else {
|
||||||
|
// Add new product to cart
|
||||||
|
cartList.add(product);
|
||||||
|
}
|
||||||
|
cartCount.value = cartList.length;
|
||||||
|
updateTotalPrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateTotalPrice() {
|
||||||
|
double subTotal = 0.0;
|
||||||
|
double gstTotalAmount = 0.0;
|
||||||
|
|
||||||
|
for (var product in cartList) {
|
||||||
|
subTotal += product.price * product.quantity;
|
||||||
|
gstTotalAmount += (product.price * product.quantity * (product.gst / 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
subtotal.value = subTotal;
|
||||||
|
gstTotal.value = gstTotalAmount;
|
||||||
|
grandTotal.value = subtotal.value + gstTotal.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increase the quantity of a product
|
||||||
|
void increaseQuantity(Product product) {
|
||||||
|
final index = cartList.indexWhere((item) => item.id == product.id);
|
||||||
|
if (index != -1) {
|
||||||
|
cartList[index].quantity++;
|
||||||
|
updateTotalPrice();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrease the quantity of a product
|
||||||
|
void decreaseQuantity(Product product) {
|
||||||
|
final index = cartList.indexWhere((item) => item.id == product.id);
|
||||||
|
if (index != -1 && cartList[index].quantity > 1) {
|
||||||
|
cartList[index].quantity--;
|
||||||
|
updateTotalPrice();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove item from cart
|
||||||
|
void removeFromCart(Product product) {
|
||||||
|
cartList.removeWhere((item) => item.id == product.id);
|
||||||
|
cartCount.value = cartList.length;
|
||||||
|
updateTotalPrice();
|
||||||
|
}
|
||||||
|
}
|
@ -27,7 +27,7 @@ class _CartScreenState extends State<CartScreen> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
if (widget.productModel != null) {
|
if (widget.productModel != null) {
|
||||||
_cartController.addToCart(widget.productModel);
|
_cartController.addToCart(widget.productModel!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ class _ProductCardState extends State<ProductCard> {
|
|||||||
SizedBox(width: 20.0,),
|
SizedBox(width: 20.0,),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
_cartController.removeFromCart(widget.productModel);
|
_cartController.removeFromCart(widget.productModel!);
|
||||||
},
|
},
|
||||||
icon: const Icon(
|
icon: const Icon(
|
||||||
Icons.delete_outline_rounded,
|
Icons.delete_outline_rounded,
|
||||||
|
Loading…
Reference in New Issue
Block a user