import 'package:cached_network_image/cached_network_image.dart'; import 'package:cheminova/controller/cart_controller.dart'; import 'package:cheminova/models/oder_place_model.dart'; import 'package:cheminova/models/order_item_model.dart'; import 'package:cheminova/models/place_order_list_model.dart'; import 'package:cheminova/models/product_model.dart'; import 'package:cheminova/models/rd_get_order_model.dart'; import 'package:cheminova/screens/product/product_detail_screen.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.dart'; import '../models/product_model1.dart'; import '../utils/show_snackbar.dart'; class ProductCard extends StatefulWidget { final Product? productModel; PlacedOrderModel? placedOrder; PlacedOrderList? placedOrderList; PlaceOrderItem1? placeorderItem; PlacedOrdersResponse ? placeRDOrder; ProductModel? product; final bool isInCart; final bool isCheckout; final bool isConfirmation; int? quantity; ProductCard({ super.key, this.product, this.quantity = 1, this.productModel, this.placedOrder, this.placedOrderList, this.placeorderItem, this.placeRDOrder, this.isInCart = false, this.isCheckout = false, this.isConfirmation = false, }); @override State createState() => _ProductCardState(); } class _ProductCardState extends State { String capitalizeFirstLetter(String text) { if (text.isEmpty) return text; return text[0].toUpperCase() + text.substring(1).toLowerCase(); } String? imageurl; @override Widget build(BuildContext context) { final CartController _cartController = Get.put(CartController()); bool showQuantity = widget.isInCart || widget.isCheckout || widget.isConfirmation; bool isProductInCart = _cartController.cartList.any((p) => p.id == widget.productModel!.id); // Get the current quantity of the product, either from the cart or the default value int currentQuantity = isProductInCart ? _cartController.cartList.firstWhere((p) => p.id == widget.productModel!.id).quantity : widget.productModel!.quantity ??1; TextEditingController quantityController = TextEditingController(text: currentQuantity.toString()); imageurl = widget.productModel!.brand.images.isNotEmpty ? widget.productModel!.brand.images[0].url : 'assets/images/no_image_available.jpg'; // Use a fallback image URL or handle it accordingly return GestureDetector( // Navigate to the ProductDetailScreen on tap unless the product is in cart or checkout onTap: () => widget.isInCart || widget.isCheckout ? null : Get.to(() => ProductDetailScreen(productModel: widget.productModel)), child: SizedBox( // height: Get.height * 0.21, // width: Get.width *0.20, child: Card( child: Row( children: [ Padding( padding: const EdgeInsets.all(8.0), child: ClipRRect( borderRadius: BorderRadius.circular(15.0), child: Container( height: Get.height * 0.18, width: Get.width * 0.32, decoration: BoxDecoration( image: DecorationImage( image: imageurl!.startsWith('http') ? CachedNetworkImageProvider(imageurl!) as ImageProvider : AssetImage(imageurl!), fit: BoxFit.cover, ), ), ), ), ), Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 0.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Display the product name with the first letter capitalized Text( capitalizeFirstLetter(widget.productModel!.name), style: GoogleFonts.roboto( fontSize: 16, fontWeight: FontWeight.w500, ), ), // Display the category name of the product Text( capitalizeFirstLetter(widget.productModel!.category!.categoryName), style: GoogleFonts.roboto( fontSize: 14, fontWeight: FontWeight.w400, ), ), // Display the price of the product if (!widget.isCheckout) widget.isInCart ? Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "₹ ${widget.productModel!.price.toString()}", style: GoogleFonts.roboto( fontSize: 20, fontWeight: FontWeight.w700, ), ), // SizedBox( // width: 60, // ), IconButton( onPressed: () { _cartController.removeFromCart(widget.productModel!); showSnackbar("Product has been removed successfully!"); setState(() { currentQuantity = 1; }); }, icon: const Icon( Icons.delete_outline_rounded, color: Colors.red, ), ), ], ) :SizedBox(), if (showQuantity) SizedBox( height: Get.height * 0.04, child: Text( "Quantity: ${currentQuantity}", style: GoogleFonts.roboto( color: Color(0xFF004791), fontSize: 15, fontWeight: FontWeight.w700, ), ), ), // Display quantity adjustment buttons and remove button if the product is in cart if (!widget.isCheckout) widget.isInCart ? Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( height: Get.height * 0.05, width: Get.width * 0.40, decoration: BoxDecoration( color: const Color(0xFF004791), borderRadius: BorderRadius.circular(10), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ SizedBox( height: 25, width: 25, child: ElevatedButton( onPressed: () { _cartController.decreaseQuantity(widget.productModel!); setState(() { currentQuantity = _cartController .cartList .firstWhere((p) => p.id == widget.productModel!.id) .quantity; }); }, style: ElevatedButton.styleFrom( padding: EdgeInsets.zero, backgroundColor:Color(0xFF004791), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), child: Text( '-', style: GoogleFonts.roboto( color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold, ), ), ), ), // Text( // "${currentQuantity}", // style: const TextStyle( // color: Colors.white, // fontSize: 16, // ), // Expanded( // child: TextFormField( // // decoration:InputDecoration( // // border: InputBorder.none, // // ), // // controller: quantityController, // style: TextStyle(color: Color(0xFF004791),fontWeight: FontWeight.bold), // keyboardType: TextInputType.number, // // textAlign: TextAlign.center, // onFieldSubmitted: (value) { // int enteredQuantity = int.tryParse(value) ?? currentQuantity; // if (enteredQuantity <= 0) { // showSnackbar("Quantity must be at least 1"); // enteredQuantity = 1; // } // setState(() { // currentQuantity = enteredQuantity; // _cartController.updateQuantity( // widget.productModel!, currentQuantity); // }); // }, // decoration: const InputDecoration( // // contentPadding: EdgeInsets.only(left: 8.0,right: 8.0,bottom: 8.0), // border: OutlineInputBorder( // // // ), // ), // ), // ), SizedBox(width: 5,), Expanded( child: TextFormField( controller: quantityController, style: const TextStyle( color: Color(0xFF004791), fontWeight: FontWeight.bold, fontSize: 16, ), keyboardType: TextInputType.number, textAlign: TextAlign.center, onFieldSubmitted: (value) { int enteredQuantity = int.tryParse(value) ?? currentQuantity; if (enteredQuantity <= 0) { showSnackbar("Quantity must be at least 1"); enteredQuantity = 1; } setState(() { currentQuantity = enteredQuantity; _cartController.updateQuantity(widget.productModel!, currentQuantity); }); }, decoration: InputDecoration( labelText: "Enter Quantity", labelStyle: const TextStyle( color: Color(0xFF004791), fontWeight: FontWeight.w600, fontSize: 14, ), hintText: "", hintStyle: const TextStyle( color: Colors.grey, fontWeight: FontWeight.w400, fontSize: 14, ), filled: true, fillColor: Colors.white, contentPadding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide( color: Color(0xFF004791), width: 1.5, ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide( color: Color(0xFF004791), width: 2, ), ), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide( color: Colors.red, width: 1.5, ), ), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide( color: Colors.red, width: 2, ), ), ), ), ), SizedBox(width: 5,), SizedBox( height: 25, width: 25, child: ElevatedButton( onPressed: () { _cartController.increaseQuantity(widget.productModel!); setState(() { currentQuantity = _cartController .cartList .firstWhere((p) => p.id == widget.productModel!.id) .quantity; }); }, style: ElevatedButton.styleFrom( backgroundColor: Color(0xFF004791), padding: EdgeInsets.zero, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), child: Text( '+', style: GoogleFonts.roboto( color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold, ), ), ), ), ], ), ), SizedBox( width: 1.0, ), // IconButton( // onPressed: () { // _cartController.removeFromCart(widget.productModel!); // showSnackbar("Product has been removed successfully!"); // setState(() { // currentQuantity = 1; // }); // }, // icon: const Icon( // Icons.delete_outline_rounded, // color: Colors.red, // ), // ), ], ) : ElevatedButton( onPressed: () { if (isProductInCart) { showSnackbar("Product already added to cart"); } else { _cartController.addToCart(widget.productModel!); showSnackbar("Product successfully added to your cart"); } }, style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: const Color(0xFF00784C), padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 8), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30), ), ), child: Text( "Add To Cart", style: GoogleFonts.roboto( fontSize: 14, fontWeight: FontWeight.w600, ), ), ), ], ), ), ), ], ), ), ), ); } } // // class ProductCard extends StatefulWidget { // final Product? productModel; // PlacedOrderModel? placedOrder; // PlacedOrderList? placedOrderList; // PlaceOrderItem1? placeorderItem; // PlacedOrdersResponse ? placeRDOrder; // ProductModel? product; // final bool isInCart; // final bool isCheckout; // final bool isConfirmation; // int? quantity; // // ProductCard({ // super.key, // this.product, // this.quantity = 1, // this.productModel, // this.placedOrder, // this.placedOrderList, // this.placeorderItem, // this.placeRDOrder, // this.isInCart = false, // this.isCheckout = false, // this.isConfirmation = false, // }); // // @override // State createState() => _ProductCardState(); // } // // class _ProductCardState extends State { // final CartController _cartController = Get.put(CartController()); // // String capitalizeFirstLetter(String text) { // if (text.isEmpty) return text; // return text[0].toUpperCase() + text.substring(1).toLowerCase(); // } // // @override // Widget build(BuildContext context) { // bool showQuantity = widget.isInCart || widget.isCheckout || widget.isConfirmation; // bool isProductInCart = _cartController.cartList.any((p) => p.id == widget.productModel!.id); // // // Get the current quantity of the product // int currentQuantity = isProductInCart // ? _cartController.cartList.firstWhere((p) => p.id == widget.productModel!.id).quantity // : widget.productModel!.quantity ?? 1; // // TextEditingController quantityController = // TextEditingController(text: currentQuantity.toString()); // // return GestureDetector( // onTap: () => widget.isInCart || widget.isCheckout // ? null // : Get.to(() => ProductDetailScreen(productModel: widget.productModel)), // child: Card( // child: Row( // children: [ // Padding( // padding: const EdgeInsets.all(8.0), // child: ClipRRect( // borderRadius: BorderRadius.circular(15.0), // child: Container( // height: Get.height * 0.15, // width: Get.width * 0.31, // decoration: const BoxDecoration( // image: DecorationImage( // image: AssetImage("assets/images/product.png"), // fit: BoxFit.cover, // ), // ), // ), // ), // ), // Expanded( // child: Padding( // padding: const EdgeInsets.symmetric(horizontal: 3.0), // child: Column( // crossAxisAlignment: CrossAxisAlignment.start, // children: [ // Text( // capitalizeFirstLetter(widget.productModel!.name), // style: GoogleFonts.roboto( // fontSize: 16, // fontWeight: FontWeight.w500, // ), // ), // Text( // capitalizeFirstLetter(widget.productModel!.category!.categoryName), // style: GoogleFonts.roboto( // fontSize: 14, // fontWeight: FontWeight.w400, // ), // ), // Text( // "₹ ${widget.productModel!.price.toString()}", // style: GoogleFonts.roboto( // fontSize: 22, // fontWeight: FontWeight.w700, // ), // ), // if (showQuantity) // Row( // children: [ // SizedBox( // width: 50, // child: TextFormField( // controller: quantityController, // keyboardType: TextInputType.number, // textAlign: TextAlign.center, // onFieldSubmitted: (value) { // int enteredQuantity = int.tryParse(value) ?? currentQuantity; // if (enteredQuantity <= 0) { // showSnackbar("Quantity must be at least 1"); // enteredQuantity = 1; // } // setState(() { // currentQuantity = enteredQuantity; // _cartController.updateQuantity( // widget.productModel!, currentQuantity); // }); // }, // decoration: const InputDecoration( // contentPadding: EdgeInsets.all(8), // border: OutlineInputBorder(), // ), // ), // ), // IconButton( // onPressed: () { // _cartController.decreaseQuantity(widget.productModel!); // setState(() { // currentQuantity = _cartController // .cartList // .firstWhere((p) => p.id == widget.productModel!.id) // .quantity; // quantityController.text = currentQuantity.toString(); // }); // }, // icon: const Icon(Icons.remove), // ), // IconButton( // onPressed: () { // _cartController.increaseQuantity(widget.productModel!); // setState(() { // currentQuantity = _cartController // .cartList // .firstWhere((p) => p.id == widget.productModel!.id) // .quantity; // quantityController.text = currentQuantity.toString(); // }); // }, // icon: const Icon(Icons.add), // ), // ], // ), // if (!widget.isCheckout) // widget.isInCart // ? Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ // ElevatedButton( // onPressed: () { // _cartController.removeFromCart(widget.productModel!); // showSnackbar("Product removed successfully!"); // setState(() { // currentQuantity = 1; // quantityController.text = currentQuantity.toString(); // }); // }, // style: ElevatedButton.styleFrom( // backgroundColor: Colors.red, // ), // child: const Text("Remove"), // ), // ], // ) // : ElevatedButton( // onPressed: () { // if (isProductInCart) { // showSnackbar("Product already added to cart"); // } else { // _cartController.addToCart(widget.productModel!); // showSnackbar("Product added to cart successfully!"); // } // }, // child: const Text("Add To Cart"), // ), // ], // ), // ), // ), // ], // ), // ), // ); // } // }