Search of add products
This commit is contained in:
parent
6599d9a05c
commit
600bcdd3d7
@ -1,6 +1,7 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
import 'package:cheminova/provider/home_provider.dart';
|
||||
import 'package:cheminova/provider/products_provider.dart';
|
||||
import 'package:cheminova/screens/splash_screen.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
||||
@ -89,6 +90,7 @@ Future<void> main() async {
|
||||
|
||||
runApp(MultiProvider(providers: [
|
||||
ChangeNotifierProvider(create: (context) => HomeProvider()),
|
||||
ChangeNotifierProvider(create: (context) => ProductProvider()),
|
||||
], child: const MyApp()));
|
||||
}
|
||||
|
||||
|
181
lib/models/products_response.dart
Normal file
181
lib/models/products_response.dart
Normal file
@ -0,0 +1,181 @@
|
||||
class ProductResponse {
|
||||
final bool success;
|
||||
final int totalData;
|
||||
final int totalPages;
|
||||
final List<Product> product;
|
||||
|
||||
ProductResponse({
|
||||
required this.success,
|
||||
required this.totalData,
|
||||
required this.totalPages,
|
||||
required this.product,
|
||||
});
|
||||
|
||||
factory ProductResponse.fromJson(Map<String, dynamic> json) {
|
||||
return ProductResponse(
|
||||
success: json['success'],
|
||||
totalData: json['total_data'],
|
||||
totalPages: json['total_pages'],
|
||||
product: (json['product'] as List)
|
||||
.map((item) => Product.fromJson(item))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'success': success,
|
||||
'total_data': totalData,
|
||||
'total_pages': totalPages,
|
||||
'product': product.map((item) => item.toJson()).toList(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Product {
|
||||
final String id;
|
||||
final String SKU;
|
||||
final String name;
|
||||
final Category category;
|
||||
final double price;
|
||||
final GST gst;
|
||||
final String description;
|
||||
final String specialInstructions;
|
||||
final String productStatus;
|
||||
final AddedBy addedBy;
|
||||
final List<dynamic> image;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
final int v;
|
||||
|
||||
Product({
|
||||
required this.id,
|
||||
required this.SKU,
|
||||
required this.name,
|
||||
required this.category,
|
||||
required this.price,
|
||||
required this.gst,
|
||||
required this.description,
|
||||
required this.specialInstructions,
|
||||
required this.productStatus,
|
||||
required this.addedBy,
|
||||
required this.image,
|
||||
required this.createdAt,
|
||||
required this.updatedAt,
|
||||
required this.v,
|
||||
});
|
||||
|
||||
factory Product.fromJson(Map<String, dynamic> json) {
|
||||
return Product(
|
||||
id: json['_id'],
|
||||
SKU: json['SKU'],
|
||||
name: json['name'],
|
||||
category: Category.fromJson(json['category']),
|
||||
price: (json['price'] as num).toDouble(),
|
||||
gst: GST.fromJson(json['GST']),
|
||||
description: json['description'],
|
||||
specialInstructions: json['special_instructions'],
|
||||
productStatus: json['product_Status'],
|
||||
addedBy: AddedBy.fromJson(json['addedBy']),
|
||||
image: json['image'] as List<dynamic>,
|
||||
createdAt: DateTime.parse(json['createdAt']),
|
||||
updatedAt: DateTime.parse(json['updatedAt']),
|
||||
v: json['__v'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'_id': id,
|
||||
'SKU': SKU,
|
||||
'name': name,
|
||||
'category': category.toJson(),
|
||||
'price': price,
|
||||
'GST': gst.toJson(),
|
||||
'description': description,
|
||||
'special_instructions': specialInstructions,
|
||||
'product_Status': productStatus,
|
||||
'addedBy': addedBy.toJson(),
|
||||
'image': image,
|
||||
'createdAt': createdAt.toIso8601String(),
|
||||
'updatedAt': updatedAt.toIso8601String(),
|
||||
'__v': v,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Category {
|
||||
final String id;
|
||||
final String categoryName;
|
||||
|
||||
Category({
|
||||
required this.id,
|
||||
required this.categoryName,
|
||||
});
|
||||
|
||||
factory Category.fromJson(Map<String, dynamic> json) {
|
||||
return Category(
|
||||
id: json['_id'],
|
||||
categoryName: json['categoryName'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'_id': id,
|
||||
'categoryName': categoryName,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class GST {
|
||||
final String id;
|
||||
final String name;
|
||||
final int tax;
|
||||
|
||||
GST({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.tax,
|
||||
});
|
||||
|
||||
factory GST.fromJson(Map<String, dynamic> json) {
|
||||
return GST(
|
||||
id: json['_id'],
|
||||
name: json['name'],
|
||||
tax: json['tax'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'_id': id,
|
||||
'name': name,
|
||||
'tax': tax,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class AddedBy {
|
||||
final String id;
|
||||
final String name;
|
||||
|
||||
AddedBy({
|
||||
required this.id,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
factory AddedBy.fromJson(Map<String, dynamic> json) {
|
||||
return AddedBy(
|
||||
id: json['_id'],
|
||||
name: json['name'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'_id': id,
|
||||
'name': name,
|
||||
};
|
||||
}
|
||||
}
|
41
lib/provider/products_provider.dart
Normal file
41
lib/provider/products_provider.dart
Normal file
@ -0,0 +1,41 @@
|
||||
import 'package:cheminova/services/api_client.dart';
|
||||
import 'package:cheminova/services/api_urls.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../models/products_response.dart';
|
||||
|
||||
class ProductProvider extends ChangeNotifier {
|
||||
ProductProvider() {
|
||||
getProducts();
|
||||
}
|
||||
|
||||
final _apiClient = ApiClient();
|
||||
ProductResponse? productResponse;
|
||||
List<Product> productList = [];
|
||||
|
||||
bool _isLoading = false;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
void setLoading(bool loading) {
|
||||
_isLoading = loading;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> getProducts() async {
|
||||
setLoading(true);
|
||||
try {
|
||||
Response response = await _apiClient.get(ApiUrls.getProducts);
|
||||
setLoading(false);
|
||||
if (response.statusCode == 200) {
|
||||
productResponse = ProductResponse.fromJson(response.data);
|
||||
productList = productResponse!.product;
|
||||
notifyListeners();
|
||||
}
|
||||
} catch (e) {
|
||||
setLoading(false);
|
||||
print("Error: $e");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:cheminova/widgets/common_background.dart';
|
||||
import 'package:cheminova/screens/data_submit_successfull.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:cheminova/widgets/common_elevated_button.dart';
|
||||
import 'package:cheminova/screens/data_submit_successfull.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../models/products_response.dart';
|
||||
import '../provider/products_provider.dart';
|
||||
|
||||
class AddProductsScreen extends StatefulWidget {
|
||||
const AddProductsScreen({super.key});
|
||||
@ -13,27 +16,25 @@ class AddProductsScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
final List<Product> products = [
|
||||
Product(name: 'Product A', sku: 'SKU001', isPurchased: true),
|
||||
Product(name: 'Product B', sku: 'SKU002', isPurchased: true),
|
||||
Product(name: 'Product C', sku: 'SKU003', isPurchased: false),
|
||||
];
|
||||
|
||||
List<Product> selectedProducts = [];
|
||||
List<Product> filteredProducts = [];
|
||||
final searchController = TextEditingController();
|
||||
late ProductProvider provider;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
filteredProducts = products;
|
||||
provider=Provider.of<ProductProvider>(context,listen: false);
|
||||
provider.getProducts();
|
||||
filteredProducts = provider.productList;
|
||||
}
|
||||
|
||||
void filterProducts(String query) {
|
||||
setState(() {
|
||||
filteredProducts = products.where((product) {
|
||||
final provider = Provider.of<ProductProvider>(context, listen: false);
|
||||
filteredProducts = provider.productList.where((product) {
|
||||
final productNameLower = product.name.toLowerCase();
|
||||
final productSkuLower = product.sku.toLowerCase();
|
||||
final productSkuLower = product.SKU.toLowerCase();
|
||||
final searchLower = query.toLowerCase();
|
||||
|
||||
return productNameLower.contains(searchLower) ||
|
||||
@ -44,7 +45,9 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CommonBackground(
|
||||
|
||||
return
|
||||
CommonBackground(
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
appBar: CommonAppBar(
|
||||
@ -67,7 +70,13 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
elevation: 0,
|
||||
),
|
||||
drawer: const CommonDrawer(),
|
||||
body: Stack(
|
||||
body: Consumer<ProductProvider>(
|
||||
builder: (context, provider, child) {
|
||||
if (provider.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
@ -76,14 +85,17 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
child: ListView.builder(
|
||||
itemCount: selectedProducts.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ProductBlock(product: selectedProducts[index]);
|
||||
return ProductBlock(
|
||||
product: selectedProducts[index]);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Align(
|
||||
alignment: selectedProducts.isEmpty ? Alignment.center : Alignment.bottomCenter,
|
||||
alignment: selectedProducts.isEmpty
|
||||
? Alignment.center
|
||||
: Alignment.bottomCenter,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
@ -92,18 +104,24 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
showModalBottomSheet(
|
||||
isScrollControlled: true,
|
||||
constraints: BoxConstraints(
|
||||
maxHeight:
|
||||
MediaQuery.of(context).size.height * 0.9,
|
||||
),
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return StatefulBuilder(
|
||||
return Consumer<ProductProvider>(builder: (context, value, child) =>StatefulBuilder(
|
||||
builder: (context, setState) {
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
padding: const EdgeInsets.all(18.0),
|
||||
child: TextField(
|
||||
controller: searchController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Search by name or SKU',
|
||||
labelText:
|
||||
'Search by name or SKU',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.search),
|
||||
),
|
||||
@ -117,28 +135,40 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
child: ListView.builder(
|
||||
itemCount: filteredProducts.length,
|
||||
itemBuilder: (context, index) {
|
||||
bool isAlreadySelected = selectedProducts.contains(filteredProducts[index]);
|
||||
return ListTile(
|
||||
bool isAlreadySelected =
|
||||
selectedProducts.contains(
|
||||
filteredProducts[index]);
|
||||
return Card(
|
||||
child: ListTile(
|
||||
title: Text(
|
||||
filteredProducts[index].name,
|
||||
filteredProducts[index]
|
||||
.name,
|
||||
style: TextStyle(
|
||||
color: isAlreadySelected ? Colors.grey : Colors.black,
|
||||
color: isAlreadySelected
|
||||
? Colors.grey
|
||||
: Colors.black,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
filteredProducts[index].sku,
|
||||
filteredProducts[index].SKU,
|
||||
style: TextStyle(
|
||||
color: isAlreadySelected ? Colors.grey : Colors.black,
|
||||
color: isAlreadySelected
|
||||
? Colors.grey
|
||||
: Colors.black,
|
||||
),
|
||||
),
|
||||
onTap: isAlreadySelected
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
selectedProducts.add(filteredProducts[index]);
|
||||
selectedProducts.add(
|
||||
filteredProducts[
|
||||
index]);
|
||||
});
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(
|
||||
context);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
@ -146,6 +176,7 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
).whenComplete(() {
|
||||
@ -171,7 +202,8 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const DataSubmitSuccessfull(),
|
||||
builder: (context) =>
|
||||
const DataSubmitSuccessfull(),
|
||||
),
|
||||
);
|
||||
},
|
||||
@ -182,28 +214,15 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@ -221,13 +240,12 @@ class _ProductBlockState extends State<ProductBlock> {
|
||||
@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) {
|
||||
if (saleController.text.isNotEmpty &&
|
||||
inventoryController.text.isNotEmpty) {
|
||||
int sale = int.parse(saleController.text);
|
||||
int inventory = int.parse(inventoryController.text);
|
||||
if (inventory > sale) {
|
||||
@ -244,28 +262,33 @@ class _ProductBlockState extends State<ProductBlock> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
color: !widget.product.isPurchased ? Colors.white54 : Colors.white,
|
||||
// color: !widget.product.isPurchased ? Colors.white54 : Colors.white,
|
||||
color: 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)),
|
||||
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,
|
||||
// enabled: widget.product.isPurchased,
|
||||
enabled: true,
|
||||
onChanged: (_) => validateInput(),
|
||||
),
|
||||
TextField(
|
||||
controller: inventoryController,
|
||||
decoration: const InputDecoration(labelText: 'Inventory'),
|
||||
keyboardType: TextInputType.number,
|
||||
enabled: widget.product.isPurchased,
|
||||
// enabled: widget.product.isPurchased,
|
||||
enabled: true,
|
||||
onChanged: (_) => validateInput(),
|
||||
),
|
||||
if (errorMessage != null)
|
||||
|
@ -13,4 +13,6 @@ class ApiUrls {
|
||||
static const String rejectedApplication = '${baseUrl}kyc/getAllrejected';
|
||||
static const String notificationUrl = '${baseUrl}/get-notification-sc';
|
||||
static const String fcmUrl = '${baseUrl}kyc/save-fcm-sc';
|
||||
static const String getProducts = '${baseUrl}product/getAll/user';
|
||||
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ class CommonElevatedButton extends StatelessWidget {
|
||||
height: height ?? kToolbarHeight - 25,
|
||||
width: width ?? 200,
|
||||
child: ElevatedButton(
|
||||
onPressed: onPressed,
|
||||
onPressed: isLoading ? null : onPressed,
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(borderRadius ?? 6)),
|
||||
|
Loading…
Reference in New Issue
Block a user