201 lines
6.5 KiB
Dart
201 lines
6.5 KiB
Dart
import 'package:get/get.dart';
|
|
import '../models/product_model.dart';
|
|
import '../models/product_model1.dart';
|
|
|
|
class CartController extends GetxController {
|
|
// Observable list to store products added to the cart
|
|
var cartList = <Product>[].obs;
|
|
// Observable variable to track the total count of items in the cart
|
|
var cartCount = 0.obs;
|
|
// Observable variables to calculate price details
|
|
var subtotal = 0.0.obs; // The total price of items without tax
|
|
var gstTotal = 0.0.obs; // The total GST amount
|
|
var grandTotal = 0.0.obs;// The total amount including GST
|
|
|
|
|
|
// Observable list to track selected products for price calculation
|
|
var selectedProducts = <Product>[].obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// Initialize the cart list or other initializations if needed
|
|
initializeSelections();
|
|
}
|
|
// Function to add a product to the cart
|
|
void addToCart(Product product) {
|
|
// Check if the product already exists in the cart
|
|
var existingProduct = cartList.firstWhereOrNull((p) => p.id == product.id);
|
|
if (existingProduct != null) {
|
|
// If the product exists, increase its quantity
|
|
existingProduct.quantity++;
|
|
} else {
|
|
// If it's a new product, add it to the cart
|
|
cartList.add(product);
|
|
}
|
|
// Update the cart count
|
|
cartCount.value = cartList.length;
|
|
// Recalculate the total price
|
|
updateTotalPrice();
|
|
}
|
|
// Function to update the total price, subtotal, and GST total
|
|
void updateTotalPrice() {
|
|
double subTotal = 0.0;
|
|
double gstTotalAmount = 0.0;
|
|
// Calculate subtotal and GST for selected products only
|
|
for (var product in selectedProducts) {
|
|
subTotal += product.price * product.quantity;
|
|
gstTotalAmount += (product.price * product.quantity * (product.gst / 100));
|
|
}
|
|
// Update observable values
|
|
subtotal.value = subTotal;
|
|
gstTotal.value = gstTotalAmount;
|
|
grandTotal.value = subtotal.value + gstTotal.value;
|
|
}
|
|
// Function to increase the quantity of a product in the cart
|
|
void increaseQuantity(Product product) {
|
|
final index = cartList.indexWhere((item) => item.id == product.id);
|
|
if (index != -1) {
|
|
// Increment the quantity of the product
|
|
cartList[index].quantity++;
|
|
// If the product is selected, update the total price
|
|
if (selectedProducts.contains(cartList[index])) {
|
|
updateTotalPrice();
|
|
}
|
|
}
|
|
}
|
|
// Function to decrease the quantity of a product in the cart
|
|
void decreaseQuantity(Product product) {
|
|
final index = cartList.indexWhere((item) => item.id == product.id);
|
|
if (index != -1 && cartList[index].quantity > 1) {
|
|
// Decrement the quantity of the product
|
|
cartList[index].quantity--;
|
|
// If the product is selected, update the total price
|
|
if (selectedProducts.contains(cartList[index])) {
|
|
updateTotalPrice();
|
|
}
|
|
}
|
|
}
|
|
// Function to remove a product from the cart
|
|
void removeFromCart(Product product) {
|
|
// Remove the product from the cart list
|
|
cartList.removeWhere((item) => item.id == product.id);
|
|
// Remove the product from the selected list if it's there
|
|
selectedProducts.remove(product);
|
|
// Update the cart count and total price
|
|
cartCount.value = cartList.length;
|
|
updateTotalPrice();
|
|
}
|
|
// Function to toggle the selection of a product
|
|
void toggleProductSelection(Product product, bool isSelected) {
|
|
if (isSelected) {
|
|
// Add the product to the selected list if it's not already selected
|
|
if (!selectedProducts.contains(product)) {
|
|
selectedProducts.add(product);
|
|
}
|
|
} else {
|
|
// Remove the product from the selected list
|
|
selectedProducts.remove(product);
|
|
}
|
|
// Update the total price after selection change
|
|
updateTotalPrice();
|
|
}
|
|
// Function to select or deselect all products
|
|
void selectAllProducts(bool selectAll) {
|
|
if (selectAll) {
|
|
// Select all products in the cart
|
|
selectedProducts.assignAll(cartList);
|
|
} else {
|
|
// Clear all selections
|
|
selectedProducts.clear();
|
|
}
|
|
// Update the total price after selection change
|
|
updateTotalPrice();
|
|
}
|
|
// Function to initialize product selections; selects all by default
|
|
void initializeSelections() {
|
|
selectAllProducts(true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 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.where((p) => p.selected)) {
|
|
// 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();
|
|
// }
|
|
//
|
|
// // Toggle product selection
|
|
// void toggleProductSelection(Product product, bool isSelected) {
|
|
// final index = cartList.indexWhere((item) => item.id == product.id);
|
|
// if (index != -1) {
|
|
// cartList[index].selected = isSelected;
|
|
// updateTotalPrice();
|
|
// }
|
|
// }
|
|
// }
|