Validations on add product screen

This commit is contained in:
kratikpal 2024-08-16 16:36:18 +05:30
parent aa04f39fca
commit 7cce7db73c
4 changed files with 52 additions and 22 deletions

View File

@ -2,13 +2,13 @@ class ProductResponse {
final bool success; final bool success;
final int totalData; final int totalData;
final int totalPages; final int totalPages;
final List<Product> product; final List<Product> products;
ProductResponse({ ProductResponse({
required this.success, required this.success,
required this.totalData, required this.totalData,
required this.totalPages, required this.totalPages,
required this.product, required this.products,
}); });
factory ProductResponse.fromJson(Map<String, dynamic> json) { factory ProductResponse.fromJson(Map<String, dynamic> json) {
@ -16,7 +16,7 @@ class ProductResponse {
success: json['success'], success: json['success'],
totalData: json['total_data'], totalData: json['total_data'],
totalPages: json['total_pages'], totalPages: json['total_pages'],
product: (json['product'] as List) products: (json['products'] as List)
.map((item) => Product.fromJson(item)) .map((item) => Product.fromJson(item))
.toList(), .toList(),
); );
@ -27,7 +27,7 @@ class ProductResponse {
'success': success, 'success': success,
'total_data': totalData, 'total_data': totalData,
'total_pages': totalPages, 'total_pages': totalPages,
'product': product.map((item) => item.toJson()).toList(), 'products': products.map((item) => item.toJson()).toList(),
}; };
} }
} }
@ -85,6 +85,8 @@ class Product {
createdAt: DateTime.parse(json['createdAt']), createdAt: DateTime.parse(json['createdAt']),
updatedAt: DateTime.parse(json['updatedAt']), updatedAt: DateTime.parse(json['updatedAt']),
v: json['__v'], v: json['__v'],
sale: json['sale'], // Nullable field
inventory: json['inventory'], // Nullable field
); );
} }
@ -104,6 +106,8 @@ class Product {
'createdAt': createdAt.toIso8601String(), 'createdAt': createdAt.toIso8601String(),
'updatedAt': updatedAt.toIso8601String(), 'updatedAt': updatedAt.toIso8601String(),
'__v': v, '__v': v,
'sale': sale, // Nullable field
'inventory': inventory, // Nullable field
}; };
} }
} }

View File

@ -32,7 +32,7 @@ class ProductProvider extends ChangeNotifier {
setLoading(false); setLoading(false);
if (response.statusCode == 200) { if (response.statusCode == 200) {
productResponse = ProductResponse.fromJson(response.data); productResponse = ProductResponse.fromJson(response.data);
productList = productResponse!.product; productList = productResponse!.products;
notifyListeners(); notifyListeners();
} }
} catch (e) { } catch (e) {

View File

@ -1,3 +1,4 @@
import 'package:cheminova/models/pd_rd_response_model.dart';
import 'package:cheminova/models/product_model.dart'; import 'package:cheminova/models/product_model.dart';
import 'package:cheminova/provider/product_provider.dart'; import 'package:cheminova/provider/product_provider.dart';
import 'package:cheminova/screens/data_submit_successfull.dart'; import 'package:cheminova/screens/data_submit_successfull.dart';
@ -9,7 +10,13 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class AddProductsScreen extends StatefulWidget { class AddProductsScreen extends StatefulWidget {
const AddProductsScreen({super.key}); final PdRdResponseModel distributor;
final String distributorType;
const AddProductsScreen({
super.key,
required this.distributor,
required this.distributorType,
});
@override @override
State<AddProductsScreen> createState() => _AddProductsScreenState(); State<AddProductsScreen> createState() => _AddProductsScreenState();
@ -64,9 +71,9 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
padding: const EdgeInsets.only(right: 20), padding: const EdgeInsets.only(right: 20),
), ),
], ],
title: const Text( title: Text(
'Add Products', widget.distributor.name,
style: TextStyle( style: const TextStyle(
fontSize: 20, fontSize: 20,
color: Colors.black, color: Colors.black,
fontWeight: FontWeight.w400, fontWeight: FontWeight.w400,
@ -215,7 +222,7 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
provider provider
.submitSelectedProducts( .submitSelectedProducts(
"PrincipalDistributor", "PrincipalDistributor",
"66a0e19c981736b70ed4e34e") widget.distributor.id)
.then((value) { .then((value) {
if (value) { if (value) {
Navigator.push( Navigator.push(
@ -268,17 +275,27 @@ class _ProductBlockState extends State<ProductBlock> {
setState(() { setState(() {
if (saleController.text.isNotEmpty && if (saleController.text.isNotEmpty &&
inventoryController.text.isNotEmpty) { inventoryController.text.isNotEmpty) {
int sale = int.parse(saleController.text); // Check if the input can be parsed as an integer
int inventory = int.parse(inventoryController.text); if (int.tryParse(saleController.text) == null ||
if (inventory > sale) { int.tryParse(inventoryController.text) == null) {
errorMessage = 'Inventory should be less than or equal to sales'; errorMessage = 'Please enter valid integer value';
} else { } else {
errorMessage = null; // Parse the input as integers
int sale = int.parse(saleController.text);
int inventory = int.parse(inventoryController.text);
// Validate if inventory is less than or equal to sales
if (inventory > sale) {
errorMessage = 'Inventory should be less than or equal to sales';
} else {
errorMessage = null;
}
} }
} else { } else {
errorMessage = null; errorMessage = 'Please fill in both fields';
} }
}); });
return errorMessage == null; return errorMessage == null;
} }

View File

@ -65,12 +65,21 @@ class _UpdateInventoryScreenState extends State<UpdateInventoryScreen> {
text: 'SUBMIT', text: 'SUBMIT',
backgroundColor: const Color(0xff004791), backgroundColor: const Color(0xff004791),
onPressed: () { onPressed: () {
Navigator.push( if(selectedDistributor == null || selectedDistributorType == null){
context, ScaffoldMessenger.of(context).showSnackBar(
MaterialPageRoute( const SnackBar(content: Text('Please select distributor type and distributor')),
builder: (context) => const AddProductsScreen(), );
), }else{
); Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddProductsScreen(
distributor: selectedDistributor!,
distributorType: selectedDistributorType!,
),
),
);
}
}, },
), ),
), ),