sc-android-app/lib/screens/update_inventory_screen.dart
2024-08-16 17:54:32 +05:30

200 lines
7.6 KiB
Dart

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<UpdateInventoryScreen> createState() => _UpdateInventoryScreenState();
}
class _UpdateInventoryScreenState extends State<UpdateInventoryScreen> {
late PdRdProvider pdRdProvider;
@override
void initState() {
super.initState();
pdRdProvider = PdRdProvider();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => pdRdProvider,
child: CommonBackground(
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: CommonAppBar(
actions: [
IconButton(
onPressed: () => Navigator.pop(context),
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')),
backgroundColor: Colors.transparent,
elevation: 0),
drawer: const CommonDrawer(),
body: Stack(children: [
Consumer<PdRdProvider>(
builder: (context, value, child) => Column(children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 15.0, vertical: 25),
child: DropdownButtonFormField<String>(
decoration: const InputDecoration(
fillColor: Colors.white,
filled: true,
border: OutlineInputBorder()),
value: value.selectedDistributorType,
items: [
'Principal Distributor',
'Retail Distributor'
].map((String type) {
return DropdownMenuItem<String>(
value: type, child: Text(type));
}).toList(),
hint: const Text('Select Distributor Type'),
onChanged: (val) =>
value.updateDistributorType(val))),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 15.0, vertical: 25),
child: DropdownButtonFormField<GetPdRdResponse>(
decoration: const InputDecoration(
fillColor: Colors.white,
filled: true,
border: OutlineInputBorder()),
value: value.selectedPdRd,
items: value.pdRdList.map((e) {
return DropdownMenuItem<GetPdRdResponse>(
value: e, child: Text(e.name ?? ''));
}).toList(),
onChanged: (val) =>
value.updatePdRdValue(val),
// Disable the dropdown if no distributor type is selected
isExpanded: true,
isDense: true,
iconSize: 24,
hint:
const Text('Select Distributor Name')))
])),
Consumer<PdRdProvider>(
builder: (context, value, child) => value.isLoading
? Container(
color: Colors.black12,
child: const Center(
child: CircularProgressIndicator()))
: const SizedBox())
]))));
}
}
class Product {
final String name;
final String sku;
final bool isPurchased;
int? sale;
int? inventory;
Product({
required this.name,
required this.sku,
required this.isPurchased,
this.sale,
this.inventory,
});
}
class ProductBlock extends StatefulWidget {
final Product product;
const ProductBlock({super.key, required this.product});
@override
_ProductBlockState createState() => _ProductBlockState();
}
class _ProductBlockState extends State<ProductBlock> {
final saleController = TextEditingController();
final inventoryController = TextEditingController();
String? errorMessage;
@override
void initState() {
super.initState();
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);
int inventory = int.parse(inventoryController.text);
if (inventory > sale) {
errorMessage = 'Inventory should be less than or equal to sales';
} else {
errorMessage = null;
}
} else {
errorMessage = null;
}
});
}
@override
Widget build(BuildContext context) {
return Card(
color: !widget.product.isPurchased ? Colors.white54 : Colors.white,
margin: const EdgeInsets.all(8),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Product: ${widget.product.name}',
style: const TextStyle(fontSize: 16)),
Text('SKU: ${widget.product.sku}',
style: const TextStyle(fontSize: 15)),
const SizedBox(height: 8),
TextField(
controller: saleController,
decoration: const InputDecoration(labelText: 'Sale'),
keyboardType: TextInputType.number,
enabled: widget.product.isPurchased,
onChanged: (_) => validateInput(),
),
TextField(
controller: inventoryController,
decoration: const InputDecoration(labelText: 'Inventory'),
keyboardType: TextInputType.number,
enabled: widget.product.isPurchased,
onChanged: (_) => validateInput(),
),
if (errorMessage != null)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
errorMessage!,
style: const TextStyle(color: Colors.red),
),
),
],
),
),
);
}
}