first commit
This commit is contained in:
164
lib/Screens/shelfs/add_edit_shelf_screen.dart
Normal file
164
lib/Screens/shelfs/add_edit_shelf_screen.dart
Normal file
@@ -0,0 +1,164 @@
|
||||
// File: add_edit_shelf.dart
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:mobile_pos/generated/l10n.dart' as lang;
|
||||
|
||||
// --- Local Imports ---
|
||||
import 'package:mobile_pos/Screens/shelfs/repo/shelf_repo.dart';
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
|
||||
import 'model/shelf_list_model.dart';
|
||||
|
||||
class AddEditShelf extends ConsumerStatefulWidget {
|
||||
final bool isEdit;
|
||||
final ShelfData? shelf;
|
||||
|
||||
const AddEditShelf({super.key, this.isEdit = false, this.shelf});
|
||||
|
||||
@override
|
||||
ConsumerState<AddEditShelf> createState() => _AddEditShelfState();
|
||||
}
|
||||
|
||||
class _AddEditShelfState extends ConsumerState<AddEditShelf> {
|
||||
final GlobalKey<FormState> _key = GlobalKey<FormState>();
|
||||
final TextEditingController nameController = TextEditingController();
|
||||
// TextEditingController descController is removed
|
||||
|
||||
String? _selectedStatus;
|
||||
final List<String> _statusOptions = ['Active', 'Inactive'];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.isEdit && widget.shelf != null) {
|
||||
final data = widget.shelf!;
|
||||
nameController.text = data.name ?? '';
|
||||
// descController.text = data.description ?? ''; // Removed
|
||||
_selectedStatus = (data.status == 1 || data.status == 'Active') ? 'Active' : 'Inactive';
|
||||
} else {
|
||||
_selectedStatus = 'Active';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
nameController.dispose();
|
||||
// descController.dispose(); // Removed
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// --- Submission Logic ---
|
||||
Future<void> _submit() async {
|
||||
if (!_key.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final repo = ShelfRepo();
|
||||
final String apiStatus = _selectedStatus == 'Active' ? '1' : '0';
|
||||
|
||||
EasyLoading.show(status: widget.isEdit ? 'Updating...' : 'Saving...');
|
||||
|
||||
if (widget.isEdit) {
|
||||
await repo.updateShelf(
|
||||
ref: ref,
|
||||
context: context,
|
||||
id: widget.shelf!.id!.round(),
|
||||
name: nameController.text,
|
||||
status: apiStatus,
|
||||
);
|
||||
} else {
|
||||
await repo.createShelf(
|
||||
ref: ref,
|
||||
context: context,
|
||||
name: nameController.text,
|
||||
status: apiStatus,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Reset Logic ---
|
||||
void _resetForm() {
|
||||
if (widget.isEdit) {
|
||||
Navigator.pop(context);
|
||||
} else {
|
||||
setState(() {
|
||||
_key.currentState?.reset();
|
||||
nameController.clear();
|
||||
// descController.clear(); // Removed
|
||||
_selectedStatus = 'Active';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _lang = lang.S.of(context);
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
title: Text(widget.isEdit ? _lang.editShift : _lang.addShelf),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _key,
|
||||
child: Column(
|
||||
children: [
|
||||
// 1. Shelf Name
|
||||
TextFormField(
|
||||
controller: nameController,
|
||||
decoration: InputDecoration(labelText: _lang.shelfName, hintText: _lang.enterShelfName),
|
||||
validator: (value) => value!.isEmpty ? _lang.pleaseEnterShelfName : null,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 2. Status Dropdown
|
||||
DropdownButtonFormField<String>(
|
||||
value: _selectedStatus,
|
||||
icon: const Icon(Icons.keyboard_arrow_down, color: kNeutral800),
|
||||
decoration: InputDecoration(labelText: _lang.status, hintText: _lang.selectOne),
|
||||
items:
|
||||
_statusOptions.map((String value) => DropdownMenuItem(value: value, child: Text(value))).toList(),
|
||||
onChanged: (String? newValue) {
|
||||
setState(() => _selectedStatus = newValue);
|
||||
},
|
||||
validator: (value) => value == null ? _lang.pleaseSelectStatus : null,
|
||||
),
|
||||
const SizedBox(height: 30),
|
||||
|
||||
// NOTE: Description field removed here
|
||||
|
||||
// 4. Action Buttons (Reset/Save)
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed: _resetForm,
|
||||
style: OutlinedButton.styleFrom(
|
||||
minimumSize: const Size(double.infinity, 50),
|
||||
),
|
||||
child: Text(widget.isEdit ? _lang.cancel : _lang.resets),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: _submit,
|
||||
style: ElevatedButton.styleFrom(
|
||||
minimumSize: const Size(double.infinity, 50),
|
||||
),
|
||||
child: Text(widget.isEdit ? _lang.update : _lang.save),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
59
lib/Screens/shelfs/model/shelf_list_model.dart
Normal file
59
lib/Screens/shelfs/model/shelf_list_model.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
// File: product_shelf_model.dart
|
||||
|
||||
class ShelfListModel {
|
||||
ShelfListModel({
|
||||
this.message,
|
||||
this.data,
|
||||
});
|
||||
|
||||
ShelfListModel.fromJson(dynamic json) {
|
||||
message = json['message'];
|
||||
if (json['data'] != null) {
|
||||
data = [];
|
||||
// Handles list of shelves in the 'data' key
|
||||
json['data'].forEach((v) {
|
||||
data?.add(ShelfData.fromJson(v));
|
||||
});
|
||||
}
|
||||
}
|
||||
String? message;
|
||||
List<ShelfData>? data;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['message'] = message;
|
||||
if (data != null) {
|
||||
map['data'] = data?.map((v) => v.toJson()).toList();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
class ShelfData {
|
||||
ShelfData({
|
||||
this.id,
|
||||
this.name,
|
||||
this.description, // Keeping it nullable in model as API might return it
|
||||
this.status,
|
||||
});
|
||||
|
||||
ShelfData.fromJson(dynamic json) {
|
||||
id = json['id'];
|
||||
name = json['name'];
|
||||
description = json['description'];
|
||||
status = json['status'];
|
||||
}
|
||||
num? id;
|
||||
String? name;
|
||||
String? description; // Nullable string
|
||||
dynamic status; // Use dynamic to handle potential num (1/0) or String ("Active")
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['id'] = id;
|
||||
map['name'] = name;
|
||||
map['description'] = description;
|
||||
map['status'] = status;
|
||||
return map;
|
||||
}
|
||||
}
|
||||
11
lib/Screens/shelfs/provider/shelf_provider.dart
Normal file
11
lib/Screens/shelfs/provider/shelf_provider.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../model/shelf_list_model.dart';
|
||||
import '../repo/shelf_repo.dart';
|
||||
|
||||
// Instantiate the repository
|
||||
final repo = ShelfRepo();
|
||||
|
||||
final shelfListProvider = FutureProvider.autoDispose<ShelfListModel>((ref) {
|
||||
return repo.fetchAllShelves();
|
||||
});
|
||||
166
lib/Screens/shelfs/repo/shelf_repo.dart
Normal file
166
lib/Screens/shelfs/repo/shelf_repo.dart
Normal file
@@ -0,0 +1,166 @@
|
||||
// File: shelf_repo.dart (FINAL CODE WITHOUT DESCRIPTION)
|
||||
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
// --- Local Imports (Ensure these paths are correct for your project) ---
|
||||
import '../../../../Const/api_config.dart';
|
||||
import '../../../../http_client/custome_http_client.dart';
|
||||
import '../../../../http_client/customer_http_client_get.dart';
|
||||
import '../model/shelf_list_model.dart';
|
||||
import '../provider/shelf_provider.dart';
|
||||
|
||||
|
||||
class ShelfRepo {
|
||||
|
||||
static const String _endpoint = '/shelfs';
|
||||
|
||||
|
||||
///---------------- FETCH ALL SHELVES (GET) ----------------///
|
||||
Future<ShelfListModel> fetchAllShelves() async {
|
||||
CustomHttpClientGet clientGet = CustomHttpClientGet(client: http.Client());
|
||||
final uri = Uri.parse('${APIConfig.url}$_endpoint');
|
||||
|
||||
final response = await clientGet.get(url: uri);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final parsedData = jsonDecode(response.body);
|
||||
return ShelfListModel.fromJson(parsedData);
|
||||
} else {
|
||||
throw Exception('Failed to fetch shelf list. Status: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
|
||||
///---------------- CREATE SHELF (POST) ----------------///
|
||||
Future<void> createShelf({
|
||||
required WidgetRef ref,
|
||||
required BuildContext context,
|
||||
required String name,
|
||||
// Description removed from parameters
|
||||
required String status, // "1" or "0"
|
||||
}) async {
|
||||
final uri = Uri.parse('${APIConfig.url}$_endpoint');
|
||||
|
||||
final requestBody = jsonEncode({
|
||||
'name': name,
|
||||
'status': status,
|
||||
});
|
||||
|
||||
try {
|
||||
EasyLoading.show(status: 'Creating Shelf...');
|
||||
CustomHttpClient customHttpClient = CustomHttpClient(client: http.Client(), context: context, ref: ref);
|
||||
|
||||
var responseData = await customHttpClient.post(
|
||||
url: uri,
|
||||
addContentTypeInHeader: true,
|
||||
body: requestBody,
|
||||
);
|
||||
|
||||
final parsedData = jsonDecode(responseData.body);
|
||||
EasyLoading.dismiss();
|
||||
|
||||
if (responseData.statusCode == 200 || responseData.statusCode == 201) {
|
||||
ref.invalidate(shelfListProvider); // Refresh list on success
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(parsedData['message'] ?? 'Shelf created successfully')),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Creation failed: ${parsedData['message'] ?? 'Unknown error'}')),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
EasyLoading.dismiss();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('An error occurred: $error')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///---------------- UPDATE SHELF (PUT) ----------------///
|
||||
Future<void> updateShelf({
|
||||
required WidgetRef ref,
|
||||
required BuildContext context,
|
||||
required num id,
|
||||
required String name,
|
||||
required String status, // "1" or "0"
|
||||
}) async {
|
||||
final uri = Uri.parse('${APIConfig.url}$_endpoint/$id');
|
||||
|
||||
final requestBody = jsonEncode({
|
||||
'_method': 'put', // Use POST method with _method=put
|
||||
'name': name,
|
||||
'status': status,
|
||||
});
|
||||
|
||||
try {
|
||||
EasyLoading.show(status: 'Updating Shelf...');
|
||||
CustomHttpClient customHttpClient = CustomHttpClient(client: http.Client(), context: context, ref: ref);
|
||||
|
||||
var responseData = await customHttpClient.post(
|
||||
url: uri,
|
||||
addContentTypeInHeader: true,
|
||||
body: requestBody,
|
||||
);
|
||||
|
||||
final parsedData = jsonDecode(responseData.body);
|
||||
EasyLoading.dismiss();
|
||||
|
||||
if (responseData.statusCode == 200) {
|
||||
ref.invalidate(shelfListProvider); // Refresh list on success
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(parsedData['message'] ?? 'Shelf updated successfully')),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Update failed: ${parsedData['message'] ?? 'Unknown error'}')),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
EasyLoading.dismiss();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('An error occurred: $error')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///---------------- DELETE SHELF (DELETE) ----------------///
|
||||
Future<bool> deleteShelf({
|
||||
required num id,
|
||||
required BuildContext context,
|
||||
required WidgetRef ref,
|
||||
}) async {
|
||||
try {
|
||||
EasyLoading.show(status: 'Deleting...');
|
||||
final url = Uri.parse('${APIConfig.url}$_endpoint/$id');
|
||||
CustomHttpClient customHttpClient = CustomHttpClient(ref: ref, context: context, client: http.Client());
|
||||
final response = await customHttpClient.delete(url: url);
|
||||
|
||||
EasyLoading.dismiss();
|
||||
if (response.statusCode == 200) {
|
||||
ref.invalidate(shelfListProvider); // Refresh list on success
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Shelf deleted successfully')),
|
||||
);
|
||||
return true;
|
||||
} else {
|
||||
final parsedData = jsonDecode(response.body);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Deletion failed: ${parsedData['message'] ?? 'Unknown error'}')),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
EasyLoading.dismiss();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('An error occurred during deletion: $error')),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
202
lib/Screens/shelfs/shelf_list_screen.dart
Normal file
202
lib/Screens/shelfs/shelf_list_screen.dart
Normal file
@@ -0,0 +1,202 @@
|
||||
// File: product_shelf_list_screen.dart (Based on BrandsList structure)
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:mobile_pos/Screens/shelfs/provider/shelf_provider.dart';
|
||||
import 'package:mobile_pos/Screens/shelfs/repo/shelf_repo.dart';
|
||||
import 'package:nb_utils/nb_utils.dart'; // Assuming nb_utils is needed for .launch()
|
||||
|
||||
// --- Local Imports ---
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
import 'package:mobile_pos/generated/l10n.dart' as lang;
|
||||
import '../../GlobalComponents/glonal_popup.dart';
|
||||
import '../../service/check_user_role_permission_provider.dart';
|
||||
import '../../widgets/empty_widget/_empty_widget.dart';
|
||||
import '../hrm/widgets/deleteing_alart_dialog.dart';
|
||||
import '../product_category/product_category_list_screen.dart';
|
||||
import 'add_edit_shelf_screen.dart';
|
||||
|
||||
// --- Data Layer Imports ---
|
||||
|
||||
class ProductShelfList extends ConsumerStatefulWidget {
|
||||
const ProductShelfList({super.key, required this.isFromProductList});
|
||||
|
||||
final bool isFromProductList;
|
||||
|
||||
@override
|
||||
ConsumerState<ProductShelfList> createState() => _ProductShelfListState();
|
||||
}
|
||||
|
||||
class _ProductShelfListState extends ConsumerState<ProductShelfList> {
|
||||
String search = '';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _lang = lang.S.of(context);
|
||||
return GlobalPopup(
|
||||
child: Scaffold(
|
||||
backgroundColor: kWhite,
|
||||
appBar: AppBar(
|
||||
title: Text(_lang.shelf),
|
||||
iconTheme: const IconThemeData(color: Colors.black),
|
||||
centerTitle: true,
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0.0,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Consumer(builder: (context, ref, __) {
|
||||
final shelfDataAsync = ref.watch(shelfListProvider);
|
||||
// Assuming businessInfoProvider and Permit enum are accessible
|
||||
final permissionService = PermissionService(ref);
|
||||
|
||||
// NOTE: I'm skipping the outer businessInfo.when block
|
||||
// for simplicity, as the main data is shelfDataAsync.
|
||||
|
||||
// You may insert the outer businessInfo.when block here if required:
|
||||
// final businessInfo = ref.watch(businessInfoProvider);
|
||||
// return businessInfo.when(data: (details) { ... });
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: AppTextField(
|
||||
textFieldType: TextFieldType.NAME,
|
||||
decoration: InputDecoration(
|
||||
border: const OutlineInputBorder(),
|
||||
hintText: lang.S.of(context).search,
|
||||
prefixIcon: Icon(
|
||||
Icons.search,
|
||||
color: kGreyTextColor.withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
search = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10.0),
|
||||
// Add Button
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: GestureDetector(
|
||||
onTap: () async {
|
||||
// Assuming AddEditShelf is used for adding too (isEdit=false)
|
||||
const AddEditShelf().launch(context);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.only(left: 20.0, right: 20.0),
|
||||
height: 48.0,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
border: Border.all(color: kGreyTextColor),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.add,
|
||||
color: kGreyTextColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Shelf Data List Loading and Display
|
||||
shelfDataAsync.when(
|
||||
data: (model) {
|
||||
final allShelves = model.data ?? [];
|
||||
// Apply Search Filtering
|
||||
final filteredShelves = allShelves.where((shelf) {
|
||||
return (shelf.name ?? '').toLowerCase().contains(search.toLowerCase()) ||
|
||||
(shelf.description ?? '').toLowerCase().contains(search.toLowerCase());
|
||||
}).toList();
|
||||
|
||||
return filteredShelves.isNotEmpty
|
||||
? ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: filteredShelves.length,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemBuilder: (context, i) {
|
||||
final shelf = filteredShelves[i];
|
||||
return ListCardWidget(
|
||||
// OnSelect action depends on context (e.g., selecting for product creation)
|
||||
onSelect: widget.isFromProductList
|
||||
? () => Navigator.pop(context, shelf)
|
||||
: () {
|
||||
// Default action if not from product list
|
||||
// You might navigate to a detailed view or do nothing
|
||||
},
|
||||
title: shelf.name ?? 'N/A Shelf',
|
||||
|
||||
// Delete Action
|
||||
onDelete: () async {
|
||||
// NOTE: Replace 'shelf_delete_permit' with your actual Permit.value
|
||||
if (!permissionService.hasPermission('shelf_delete_permit')) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.red,
|
||||
content: Text(_lang.youDoNotHavePermissionDeleteTheShelf),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
bool confirmDelete =
|
||||
await showDeleteConfirmationDialog(context: context, itemName: 'shelf');
|
||||
if (confirmDelete) {
|
||||
EasyLoading.show();
|
||||
if (await ShelfRepo().deleteShelf(context: context, id: shelf.id ?? 0, ref: ref)) {
|
||||
ref.refresh(shelfListProvider);
|
||||
}
|
||||
EasyLoading.dismiss();
|
||||
}
|
||||
},
|
||||
|
||||
// Edit Action
|
||||
onEdit: () async {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => AddEditShelf(
|
||||
isEdit: true,
|
||||
shelf: shelf,
|
||||
),
|
||||
));
|
||||
},
|
||||
);
|
||||
})
|
||||
: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Text(
|
||||
search.isEmpty ? lang.S.of(context).noDataFound : _lang.notMatchingResultFound,
|
||||
),
|
||||
);
|
||||
},
|
||||
error: (e, __) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Text('Error loading data: ${e.toString()}'),
|
||||
);
|
||||
},
|
||||
loading: () {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
// End of businessInfo.when block if it was used
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user