import 'package:cheminova/models/get_pd_rd_response.dart'; import 'package:cheminova/provider/pd_rd_provider.dart'; import 'package:cheminova/widgets/common_app_bar.dart'; import 'package:cheminova/widgets/common_background.dart'; import 'package:cheminova/widgets/common_drawer.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class UpdateInventoryScreen extends StatefulWidget { const UpdateInventoryScreen({super.key}); @override State createState() => _UpdateInventoryScreenState(); } class _UpdateInventoryScreenState extends State { late PdRdProvider pdRdProvider; // Provider for managing inventory data @override void initState() { super.initState(); pdRdProvider = PdRdProvider(); // Initialize the provider } @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (context) => pdRdProvider, // Provide the PdRdProvider to the widget tree child: CommonBackground( child: Scaffold( backgroundColor: Colors.transparent, // Set the background to transparent appBar: CommonAppBar( actions: [ // Back button in the app bar IconButton( onPressed: () => Navigator.pop(context), // Navigate back icon: Image.asset('assets/Back_attendance.png'), padding: const EdgeInsets.only(right: 20), ), ], title: const Text('Update Inventory Data', style: TextStyle( fontSize: 20, color: Colors.black, fontWeight: FontWeight.w400, fontFamily: 'Anek')), // Title of the app bar backgroundColor: Colors.transparent, // Transparent app bar background elevation: 0, // No shadow for the app bar ), drawer: const CommonDrawer(), // Drawer for navigation body: Stack( children: [ Consumer( builder: (context, value, child) => Column( children: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 25), child: DropdownButtonFormField( decoration: const InputDecoration( fillColor: Colors.white, filled: true, border: OutlineInputBorder()), value: value.selectedDistributorType, // Currently selected distributor type items: [ 'Principal Distributor', 'Retail Distributor' ].map((String type) { return DropdownMenuItem( value: type, child: Text(type)); // Create dropdown items }).toList(), hint: const Text('Select Distributor Type'), // Hint text onChanged: (val) => value.updateDistributorType(val), // Update selected distributor type ), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 25), child: DropdownButtonFormField( decoration: const InputDecoration( fillColor: Colors.white, filled: true, border: OutlineInputBorder()), value: value.selectedPdRd, // Currently selected distributor items: value.pdRdList.map((e) { return DropdownMenuItem( value: e, child: Text(e.name ?? '')); // Create dropdown items }).toList(), onChanged: (val) => value.updatePdRdValue(val), // Update selected distributor value isExpanded: true, isDense: true, iconSize: 24, hint: const Text('Select Distributor Name'), // Hint text ), ) ], ), ), Consumer( builder: (context, value, child) => value.isLoading ? Container( color: Colors.black12, child: const Center( child: CircularProgressIndicator())) // Show loading indicator : const SizedBox(), ) ], ), ), ), ); } } // Product model class to represent product details class Product { final String name; // Product name final String sku; // Stock keeping unit final bool isPurchased; // Purchase status int? sale; // Sales count int? inventory; // Inventory count Product({ required this.name, required this.sku, required this.isPurchased, this.sale, this.inventory, }); } // Widget to represent a block of product details class ProductBlock extends StatefulWidget { final Product product; // Product data passed to the widget const ProductBlock({super.key, required this.product}); @override _ProductBlockState createState() => _ProductBlockState(); } class _ProductBlockState extends State { final saleController = TextEditingController(); // Controller for sale input final inventoryController = TextEditingController(); // Controller for inventory input String? errorMessage; // Variable to hold error messages @override void initState() { super.initState(); // Initialize controllers with product data saleController.text = widget.product.sale?.toString() ?? ''; inventoryController.text = widget.product.inventory?.toString() ?? ''; } void validateInput() { setState(() { if (saleController.text.isNotEmpty && inventoryController.text.isNotEmpty) { int sale = int.parse(saleController.text); // Parse sale input int inventory = int.parse(inventoryController.text); // Parse inventory input if (inventory > sale) { errorMessage = 'Inventory should be less than or equal to sales'; // Error message } else { errorMessage = null; // Clear error message if valid } } else { errorMessage = null; // Clear error message if fields are empty } }); } @override Widget build(BuildContext context) { return Card( color: !widget.product.isPurchased ? Colors.white54 : Colors.white, // Change color based on purchase status margin: const EdgeInsets.all(8), child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, // Align children to the start children: [ Text('Product: ${widget.product.name}', style: const TextStyle(fontSize: 16)), // Display product name Text('SKU: ${widget.product.sku}', style: const TextStyle(fontSize: 15)), // Display SKU const SizedBox(height: 8), TextField( controller: saleController, // Controller for sale input field decoration: const InputDecoration(labelText: 'Sale'), // Label for sale input keyboardType: TextInputType.number, // Numeric keyboard enabled: widget.product.isPurchased, // Enable input based on purchase status onChanged: (_) => validateInput(), // Validate input on change ), TextField( controller: inventoryController, // Controller for inventory input field decoration: const InputDecoration(labelText: 'Inventory'), // Label for inventory input keyboardType: TextInputType.number, // Numeric keyboard enabled: widget.product.isPurchased, // Enable input based on purchase status onChanged: (_) => validateInput(), // Validate input on change ), if (errorMessage != null) // Display error message if exists Padding( padding: const EdgeInsets.only(top: 8.0), child: Text( errorMessage!, style: const TextStyle(color: Colors.red), // Style for error message ), ), ], ), ), ); } }