// // import 'package:flutter_svg/svg.dart'; // import 'package:get/get.dart'; // import 'package:get/get_core/src/get_main.dart'; // import 'package:google_fonts/google_fonts.dart'; // // import '../../controller/update_stock_controller.dart'; // import '../../models/product_stock_model.dart'; // import 'package:flutter/material.dart'; // // import '../../utils/show_snackbar.dart'; // import '../../widgets/input_field.dart'; // import 'inventory_management_screen.dart'; // class InventoryUpdateStockScreen extends StatefulWidget { // final ProductStockModel product; // // const InventoryUpdateStockScreen({ // super.key, // required this.product, // }); // // @override // State createState() => _InventoryUpdateStockScreenState(); // } // // class _InventoryUpdateStockScreenState extends State { // final _textController = TextEditingController(); // final UpdateStockController _updateStockController = Get.put(UpdateStockController()); // // // Map to store the updated stock quantities for each product // final Map _updatedStockMap = {}; // // @override // void initState() { // super.initState(); // // Set the initial value of the text controller if there's an updated stock for the current product // _textController.text = _updatedStockMap[widget.product.productid]?.toString() ?? widget.product.stock.toString(); // } // // void _onUpdateStock() async { // try { // // Parse the new stock quantity from the text controller // int? newStockQuantity = int.tryParse(_textController.text.trim()); // // // Check if the parsed quantity is valid // if (newStockQuantity == null || newStockQuantity < 0) { // showSnackbar("Please enter a valid stock quantity."); // return; // } // // // Update the map with the new stock quantity for the current product // setState(() { // _updatedStockMap[widget.product.productid] = newStockQuantity; // }); // // // Create a list of ProductStockModel instances with updated stock // List productStockList = [ // ProductStockModel( // productid: widget.product.productid, // name: widget.product.name, // sku: widget.product.sku, // stock: widget.product.stock, // openingInventory: newStockQuantity, // Update stock with new quantity // ), // ]; // // // Call the updateStock function // final update = await _updateStockController.updateProductStock(productStockList); // // // Optionally, show a confirmation message or navigate to another screen // showSnackbar("Opening Inventory updated successfully"); // Get.to(OpeningInventoryManagementScreen()); // } catch (e) { // print("Error updating stock: $e"); // showSnackbar("Error updating stock. Please try again."); // } // } // // @override // void didUpdateWidget(covariant InventoryUpdateStockScreen oldWidget) { // super.didUpdateWidget(oldWidget); // // Update the text controller with the new product's stock or the previously updated value // _textController.text = _updatedStockMap[widget.product.productid]?.toString() ?? widget.product.stock.toString(); // } // // @override // Widget build(BuildContext context) { // return Scaffold( // extendBodyBehindAppBar: true, // appBar: AppBar( // centerTitle: true, // backgroundColor: Colors.transparent, // elevation: 0, // leading: GestureDetector( // onTap: () {}, // child: Padding( // padding: const EdgeInsets.all(16.0), // child: SvgPicture.asset( // 'assets/svg/menu.svg', // ), // ), // ), // actions: [ // GestureDetector( // onTap: () => Get.back(), // child: Padding( // padding: const EdgeInsets.all(8.0), // child: SvgPicture.asset( // 'assets/svg/back_arrow.svg', // ), // ), // ), // ], // title: const Text( // "Update Inventory", // ), // ), // body: Stack( // fit: StackFit.expand, // children: [ // Image.asset( // 'assets/images/image_1.png', // fit: BoxFit.cover, // ), // SafeArea( // child: Column( // mainAxisAlignment: MainAxisAlignment.start, // children: [ // SizedBox( // height: Get.height * 0.02, // ), // Card( // margin: const EdgeInsets.symmetric(horizontal: 16), // shape: RoundedRectangleBorder( // borderRadius: BorderRadius.circular(19), // side: const BorderSide(color: Color(0xFFFDFDFD)), // ), // color: const Color(0xFFB4D1E5).withOpacity(0.9), // child: Padding( // padding: const EdgeInsets.all(12.0), // child: Column( // mainAxisSize: MainAxisSize.min, // crossAxisAlignment: CrossAxisAlignment.start, // children: [ // Card( // child: SizedBox( // width: Get.width, // child: Padding( // padding: const EdgeInsets.all(12), // child: Row( // children: [ // Text( // "Product Name: ", // style: GoogleFonts.roboto( // fontSize: Get.width * 0.04, // fontWeight: FontWeight.w700, // ), // ), // Text("${widget.product.name}"), // ], // ), // ), // ), // ), // InputField( // hintText: "Enter New Opening Inventory Quantity:", // labelText: "Enter New Opening Inventory Quantity:", // controller: _textController, // ), // ], // ), // ), // ), // SizedBox(height: Get.height * 0.04), // SizedBox( // width: Get.width * 0.9, // height: Get.height * 0.06, // child: ElevatedButton( // onPressed: () { // _onUpdateStock(); // }, // style: ElevatedButton.styleFrom( // foregroundColor: Colors.white, // backgroundColor: const Color(0xFF00784C), // shape: RoundedRectangleBorder( // borderRadius: BorderRadius.circular(10), // ), // ), // child: Text( // "Update Inventory Quantity", // style: GoogleFonts.roboto( // fontSize: Get.width * 0.05, // fontWeight: FontWeight.w600, // ), // ), // ), // ), // ], // ), // ), // ], // ), // ); // } // } // import 'package:flutter_svg/svg.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.dart'; import '../../controller/update_stock_controller.dart'; import '../../models/product_stock_model.dart'; import 'package:flutter/material.dart'; import '../../utils/show_snackbar.dart'; import '../../widgets/input_field.dart'; import 'inventory_management_screen.dart'; class InventoryUpdateStockScreen extends StatefulWidget { final List products; final String selectedProductId; const InventoryUpdateStockScreen({ super.key, required this.products, required this.selectedProductId, }); @override State createState() => _InventoryUpdateStockScreenState(); } class _InventoryUpdateStockScreenState extends State { late TextEditingController _textController; final UpdateStockController _updateStockController = Get.put(UpdateStockController()); @override void initState() { super.initState(); // Initialize text controller with the stock of the selected product final selectedProduct = widget.products.firstWhere((product) => product.productid == widget.selectedProductId); _textController = TextEditingController(text: selectedProduct.stock.toString()); } void _onUpdateStock() async { try { int? newStockQuantity = int.tryParse(_textController.text.trim()); if (newStockQuantity == null || newStockQuantity < 0) { showSnackbar("Please enter a valid stock quantity."); return; } // Update only the stock for the selected product while keeping others the same List updatedProductStockList = widget.products.map((product) { if (product.productid == widget.selectedProductId) { return ProductStockModel( productid: product.productid, name: product.name, sku: product.sku, stock: newStockQuantity, openingInventory: newStockQuantity, ); } else { return product; } }).toList(); // Call the updateStock function with the updated list final update = await _updateStockController.updateProductStock(updatedProductStockList); showSnackbar("Opening Inventory for ${widget.selectedProductId} updated successfully"); Get.to(OpeningInventoryManagementScreen()); } catch (e) { print("Error updating stock: $e"); showSnackbar("Error updating stock. Please try again."); } } @override Widget build(BuildContext context) { final selectedProduct = widget.products.firstWhere((product) => product.productid == widget.selectedProductId); return Scaffold( extendBodyBehindAppBar: true, appBar: AppBar( centerTitle: true, backgroundColor: Colors.transparent, elevation: 0, leading: GestureDetector( onTap: () {}, child: Padding( padding: const EdgeInsets.all(16.0), child: SvgPicture.asset( 'assets/svg/menu.svg', ), ), ), actions: [ GestureDetector( onTap: () => Get.back(), child: Padding( padding: const EdgeInsets.all(8.0), child: SvgPicture.asset( 'assets/svg/back_arrow.svg', ), ), ), ], title: const Text( "Update Inventory", ), ), body: Stack( fit: StackFit.expand, children: [ Image.asset( 'assets/images/image_1.png', fit: BoxFit.cover, ), SafeArea( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ SizedBox(height: Get.height * 0.02), Text( "Product Name: ${selectedProduct.name}", style: GoogleFonts.roboto( fontSize: Get.width * 0.04, fontWeight: FontWeight.w700, ), ), InputField( hintText: "Enter New Opening Inventory Quantity:", labelText: "Enter New Opening Inventory Quantity:", controller: _textController, ), SizedBox(height: 8), ElevatedButton( onPressed: _onUpdateStock, style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: const Color(0xFF00784C), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), ), child: Text( "Update Inventory Quantity", style: GoogleFonts.roboto( fontSize: Get.width * 0.05, fontWeight: FontWeight.w600, ), ), ), ], ), ), ], ), ); } }