import 'package:cached_network_image/cached_network_image.dart'; import 'package:cheminova/controller/cart_controller.dart'; import 'package:cheminova/controller/rd_get_order_controller.dart'; import 'package:cheminova/models/rd_get_order_model.dart'; import 'package:cheminova/models/rd_order_item_model.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.dart'; import '../utils/show_snackbar.dart'; class ProductCard1 extends StatefulWidget { final RDOrderItem productModel; // The specific product to be displayed int? quantity; ProductCard1({ super.key, required this.productModel, // Pass the product model explicitly this.quantity = 1, }); @override State createState() => _ProductCard1State(); } class _ProductCard1State 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()); TextEditingController quantityController = TextEditingController(); // Current quantity the user wants to process int processQuantity = widget.productModel.processquantity ?? 1; // Total available quantity int availableQuantity = widget.productModel.remainingQuantity ?? 1; imageurl = widget.productModel.image.isNotEmpty ? widget.productModel.image[0].url : 'assets/images/no_image_available.jpg'; return 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: BoxDecoration( image: DecorationImage( image: imageurl!.startsWith('http') ? CachedNetworkImageProvider(imageurl!) as ImageProvider : AssetImage(imageurl!), fit: BoxFit.cover, ), ), ), ), ), Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 3.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Display product name with capitalization Text( capitalizeFirstLetter(widget.productModel.name), style: GoogleFonts.roboto( fontSize: 16, fontWeight: FontWeight.w500, ), ), // Display category name Text( capitalizeFirstLetter(widget.productModel.categoryName), style: GoogleFonts.roboto( fontSize: 14, fontWeight: FontWeight.w400, ), ), // Display the price of the product Text( "₹ ${widget.productModel.price.toString()}", style: GoogleFonts.roboto( fontSize: 22, fontWeight: FontWeight.w700, ), ), Row( children: [ Text( "Ordered Quantity : ", style: GoogleFonts.roboto( fontSize: 15, fontWeight: FontWeight.w700, ), ), Text( " ${widget.productModel.remainingQuantity}", style: GoogleFonts.roboto( fontSize: 15, ), ), ], ), // Quantity adjustment buttons Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Availability :", style: GoogleFonts.roboto( fontSize: 15, fontWeight: FontWeight.w700, ), ), Container( height: Get.height * 0.04, width: Get.width * 0.21, decoration: BoxDecoration( color: const Color(0xFF004791), borderRadius: BorderRadius.circular(10), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ // Decrease quantity button SizedBox( height: 24, width: 24, child: ElevatedButton( onPressed: () { setState(() { if (processQuantity > 1) { processQuantity--; widget.productModel.processquantity = processQuantity; } }); }, style: ElevatedButton.styleFrom( padding: EdgeInsets.zero, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), child: Text( '-', style: GoogleFonts.roboto( fontSize: 16, fontWeight: FontWeight.w800, ), ), ), ), // Display the current process quantity Text( "$processQuantity", style: const TextStyle( color: Colors.white, fontSize: 16, ), ), // Expanded( // child: TextFormField( // // decoration:InputDecoration( // // border: InputBorder.none, // // ), // // controller: quantityController, // style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold), // keyboardType: TextInputType.number, // // textAlign: TextAlign.center, // onFieldSubmitted: (value) { // int enteredQuantity = int.tryParse(value) ?? processQuantity; // if (enteredQuantity <= 0) { // showSnackbar("Quantity must be at least 1"); // enteredQuantity = 1; // } // // setState(() { // // // widget.productModel.processquantity = enteredQuantity; // // // // _cartController.updateQuantity( // // // // widget.productModel.processquantity!, processQuantity); // // processQuantity = enteredQuantity; // // widget.productModel.processquantity = processQuantity; // // }); // }, // decoration: const InputDecoration( // // contentPadding: EdgeInsets.only(left: 8.0,right: 8.0,bottom: 8.0), // // border: OutlineInputBorder(), // ), // ), // ), // Increase quantity button SizedBox( height: 22, width: 22, child: ElevatedButton( onPressed: () { setState(() { if (processQuantity < availableQuantity) { processQuantity++; widget.productModel.processquantity = processQuantity; } }); }, style: ElevatedButton.styleFrom( padding: EdgeInsets.zero, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), child: Text( '+', style: GoogleFonts.roboto( fontSize: 16, fontWeight: FontWeight.w800, ), ), ), ), ], ), ), ], ), ]), ), ) ], ), ); } }