1) Added checkbox single or multiselect

This commit is contained in:
saritabirare 2024-09-16 17:14:45 +05:30
parent 545637e035
commit 61553fd7f1
7 changed files with 282 additions and 16 deletions

View File

@ -1,26 +1,29 @@
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
// Track the selected products
var selectedProducts = <Product>[].obs;
@override
void onInit() {
super.onInit();
// Initialize the cart list or other initializations if needed
initializeSelections();
}
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;
@ -31,7 +34,7 @@ class CartController extends GetxController {
double subTotal = 0.0;
double gstTotalAmount = 0.0;
for (var product in cartList) {
for (var product in selectedProducts) {
subTotal += product.price * product.quantity;
gstTotalAmount += (product.price * product.quantity * (product.gst / 100));
}
@ -41,28 +44,135 @@ class CartController extends GetxController {
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++;
if (selectedProducts.contains(cartList[index])) {
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--;
if (selectedProducts.contains(cartList[index])) {
updateTotalPrice();
}
}
}
// Remove item from cart
void removeFromCart(Product product) {
cartList.removeWhere((item) => item.id == product.id);
selectedProducts.remove(product);
cartCount.value = cartList.length;
updateTotalPrice();
}
void toggleProductSelection(Product product, bool isSelected) {
if (isSelected) {
if (!selectedProducts.contains(product)) {
selectedProducts.add(product);
}
} else {
selectedProducts.remove(product);
}
updateTotalPrice();
}
void selectAllProducts(bool selectAll) {
if (selectAll) {
selectedProducts.assignAll(cartList);
} else {
selectedProducts.clear();
}
updateTotalPrice();
}
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();
// }
// }
// }

View File

@ -121,7 +121,7 @@ class _CheckoutScreenState extends State<CheckoutScreen> {
void _onPlaceOrder() async {
try {
// Map the cart items (Product) to OrderItem objects
List<OrderItem> orderItems = _cartController.cartList.map<OrderItem>((product) {
List<OrderItem> orderItems = _cartController.selectedProducts.map<OrderItem>((product) {
return OrderItem(
id: product.id,
name: product.name,
@ -135,6 +135,7 @@ class _CheckoutScreenState extends State<CheckoutScreen> {
createdAt: product.createdAt,
updatedAt: product.createdAt,
count: product.quantity,
//category:product.category,
category:Category(id: product.category.id, categoryName: product.category.categoryName),
// brand:product.brand,
@ -342,12 +343,12 @@ class _CheckoutScreenState extends State<CheckoutScreen> {
child: Padding(
padding: EdgeInsets.all(Get.width * 0.02),
child: ListView.builder(
itemCount: _cartController.cartList.length,
itemCount: _cartController.selectedProducts.length,
itemBuilder: (context, index) {
final cartItem =
_cartController.cartList[index];
return ProductCard(
productModel:_cartController.cartList[index] ,
productModel:_cartController.selectedProducts[index] ,
isCheckout: true,
quantity: _cartController.cartList[0].quantity,

View File

@ -7,6 +7,11 @@ import Foundation
import device_info_plus
import file_selector_macos
import firebase_analytics
import firebase_core
import firebase_crashlytics
import firebase_messaging
import flutter_local_notifications
import path_provider_foundation
import shared_preferences_foundation
import syncfusion_pdfviewer_macos
@ -15,6 +20,11 @@ import url_launcher_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FLTFirebaseAnalyticsPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAnalyticsPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseCrashlyticsPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCrashlyticsPlugin"))
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SyncfusionFlutterPdfViewerPlugin.register(with: registry.registrar(forPlugin: "SyncfusionFlutterPdfViewerPlugin"))

View File

@ -1,6 +1,14 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_flutterfire_internals:
dependency: transitive
description:
name: _flutterfire_internals
sha256: ddc6f775260b89176d329dee26f88b9469ef46aa3228ff6a0b91caf2b2989692
url: "https://pub.dev"
source: hosted
version: "1.3.42"
args:
dependency: transitive
description:
@ -97,6 +105,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.8"
dbus:
dependency: transitive
description:
name: dbus
sha256: "365c771ac3b0e58845f39ec6deebc76e3276aa9922b0cc60840712094d9047ac"
url: "https://pub.dev"
source: hosted
version: "0.7.10"
device_info_plus:
dependency: transitive
description:
@ -185,6 +201,94 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.9.3+2"
firebase_analytics:
dependency: "direct main"
description:
name: firebase_analytics
sha256: "7b5ae39d853ead76f9d030dc23389bfec4ea826d7cccb4eea4873dcb0cdd172b"
url: "https://pub.dev"
source: hosted
version: "11.3.1"
firebase_analytics_platform_interface:
dependency: transitive
description:
name: firebase_analytics_platform_interface
sha256: "0205e05bb37abd29d5dec5cd89aeb04f3f58bf849aad21dd938be0507d52a40c"
url: "https://pub.dev"
source: hosted
version: "4.2.3"
firebase_analytics_web:
dependency: transitive
description:
name: firebase_analytics_web
sha256: "434807f8b30526e21cc062410c28ee5c6680a13626c4443b5ffede29f84b0c74"
url: "https://pub.dev"
source: hosted
version: "0.5.10"
firebase_core:
dependency: "direct main"
description:
name: firebase_core
sha256: "40921de9795fbf5887ed5c0adfdf4972d5a8d7ae7e1b2bb98dea39bc02626a88"
url: "https://pub.dev"
source: hosted
version: "3.4.1"
firebase_core_platform_interface:
dependency: transitive
description:
name: firebase_core_platform_interface
sha256: f7d7180c7f99babd4b4c517754d41a09a4943a0f7a69b65c894ca5c68ba66315
url: "https://pub.dev"
source: hosted
version: "5.2.1"
firebase_core_web:
dependency: transitive
description:
name: firebase_core_web
sha256: f4ee170441ca141c5f9ee5ad8737daba3ee9c8e7efb6902aee90b4fbd178ce25
url: "https://pub.dev"
source: hosted
version: "2.18.0"
firebase_crashlytics:
dependency: "direct main"
description:
name: firebase_crashlytics
sha256: c4fdbb14ba6f36794f89dc27fb5c759c9cc67ecbaeb079edc4dba515bbf9f555
url: "https://pub.dev"
source: hosted
version: "4.1.1"
firebase_crashlytics_platform_interface:
dependency: transitive
description:
name: firebase_crashlytics_platform_interface
sha256: "891d6f7ba4b93672d0e1265f27b6a9dccd56ba2cc30ce6496586b32d1d8770ac"
url: "https://pub.dev"
source: hosted
version: "3.6.42"
firebase_messaging:
dependency: "direct main"
description:
name: firebase_messaging
sha256: cc02c4afd6510cd84586020670140c4a23fbe52af16cd260ccf8ede101bb8d1b
url: "https://pub.dev"
source: hosted
version: "15.1.1"
firebase_messaging_platform_interface:
dependency: transitive
description:
name: firebase_messaging_platform_interface
sha256: d8a4984635f09213302243ea670fe5c42f3261d7d8c7c0a5f7dcd5d6c84be459
url: "https://pub.dev"
source: hosted
version: "4.5.44"
firebase_messaging_web:
dependency: transitive
description:
name: firebase_messaging_web
sha256: "258b9d637965db7855299b123533609ed95e52350746a723dfd1d8d6f3fac678"
url: "https://pub.dev"
source: hosted
version: "3.9.0"
fixnum:
dependency: transitive
description:
@ -206,6 +310,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
flutter_local_notifications:
dependency: "direct main"
description:
name: flutter_local_notifications
sha256: c500d5d9e7e553f06b61877ca6b9c8b92c570a4c8db371038702e8ce57f8a50f
url: "https://pub.dev"
source: hosted
version: "17.2.2"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
sha256: c49bd06165cad9beeb79090b18cd1eb0296f4bf4b23b84426e37dd7c027fc3af
url: "https://pub.dev"
source: hosted
version: "4.0.1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
sha256: "85f8d07fe708c1bdcf45037f2c0109753b26ae077e9d9e899d55971711a4ea66"
url: "https://pub.dev"
source: hosted
version: "7.2.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
@ -717,6 +845,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.0"
timezone:
dependency: transitive
description:
name: timezone
sha256: "2236ec079a174ce07434e89fcd3fcda430025eb7692244139a9cf54fdcf1fc7d"
url: "https://pub.dev"
source: hosted
version: "0.9.4"
typed_data:
dependency: transitive
description:

View File

@ -49,6 +49,11 @@ dependencies:
image_picker: ^1.1.2
csc_picker: ^0.2.7
auto_size_text: ^3.0.0
firebase_core: ^3.3.0
firebase_messaging: ^15.0.4
flutter_local_notifications: ^17.2.1+2
firebase_crashlytics: ^4.0.4
firebase_analytics: ^11.2.1
dev_dependencies:
flutter_test:

View File

@ -7,12 +7,15 @@
#include "generated_plugin_registrant.h"
#include <file_selector_windows/file_selector_windows.h>
#include <firebase_core/firebase_core_plugin_c_api.h>
#include <syncfusion_pdfviewer_windows/syncfusion_pdfviewer_windows_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
FileSelectorWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FileSelectorWindows"));
FirebaseCorePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
SyncfusionPdfviewerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("SyncfusionPdfviewerWindowsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(

View File

@ -4,6 +4,7 @@
list(APPEND FLUTTER_PLUGIN_LIST
file_selector_windows
firebase_core
syncfusion_pdfviewer_windows
url_launcher_windows
)