first commit
This commit is contained in:
129
lib/Screens/product_unit/add_units.dart
Normal file
129
lib/Screens/product_unit/add_units.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
// ignore_for_file: unused_result
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:mobile_pos/Screens/Products/Repo/unit_repo.dart';
|
||||
import 'package:mobile_pos/Screens/product_unit/model/unit_model.dart';
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
import 'package:mobile_pos/generated/l10n.dart' as lang;
|
||||
|
||||
import '../../GlobalComponents/glonal_popup.dart';
|
||||
import '../../http_client/custome_http_client.dart';
|
||||
import '../../service/check_user_role_permission_provider.dart';
|
||||
|
||||
class AddUnits extends StatefulWidget {
|
||||
const AddUnits({super.key, this.unit});
|
||||
|
||||
final Unit? unit;
|
||||
|
||||
@override
|
||||
// ignore: library_private_types_in_public_api
|
||||
_AddUnitsState createState() => _AddUnitsState();
|
||||
}
|
||||
|
||||
class _AddUnitsState extends State<AddUnits> {
|
||||
bool showProgress = false;
|
||||
TextEditingController unitController = TextEditingController();
|
||||
final GlobalKey<FormState> _key = GlobalKey();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
|
||||
if (widget.unit != null) {
|
||||
unitController.text = widget.unit?.unitName ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer(builder: (context, ref, __) {
|
||||
final permissionService = PermissionService(ref);
|
||||
return GlobalPopup(
|
||||
child: Scaffold(
|
||||
backgroundColor: kWhite,
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
lang.S.of(context).addUnit,
|
||||
),
|
||||
iconTheme: const IconThemeData(color: Colors.black),
|
||||
centerTitle: true,
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0.0,
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
children: [
|
||||
Visibility(
|
||||
visible: showProgress,
|
||||
child: const CircularProgressIndicator(
|
||||
color: kMainColor,
|
||||
strokeWidth: 5.0,
|
||||
),
|
||||
),
|
||||
Form(
|
||||
key: _key,
|
||||
child: TextFormField(
|
||||
controller: unitController,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
// return 'Please enter a valid unit name';
|
||||
return lang.S.of(context).pleaseEnterAValidUnitName;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
border: const OutlineInputBorder(),
|
||||
// hintText: 'Please enter unit name',
|
||||
hintText: lang.S.of(context).pleaseEnterUnitName,
|
||||
floatingLabelBehavior: FloatingLabelBehavior.always,
|
||||
labelText: lang.S.of(context).unitName,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 15),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (widget.unit == null) {
|
||||
if (!permissionService.hasPermission(Permit.unitsCreate.value)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.red,
|
||||
content: Text('You do not have permission to create unit'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (!permissionService.hasPermission(Permit.unitsUpdate.value)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.red,
|
||||
content: Text('You do not have permission to update unit'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (_key.currentState!.validate()) {
|
||||
UnitsRepo unit = UnitsRepo();
|
||||
|
||||
if ((widget.unit == null)) {
|
||||
await unit.addUnit(ref: ref, context: context, name: unitController.text);
|
||||
} else {
|
||||
await unit.editUnit(ref: ref, id: widget.unit?.id ?? 0, context: context, name: unitController.text);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Text(lang.S.of(context).save),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
37
lib/Screens/product_unit/model/unit_model.dart
Normal file
37
lib/Screens/product_unit/model/unit_model.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
class Unit {
|
||||
Unit({
|
||||
this.id,
|
||||
this.unitName,
|
||||
this.businessId,
|
||||
this.status,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
Unit.fromJson(dynamic json) {
|
||||
id = json['id'];
|
||||
unitName = json['unitName'];
|
||||
businessId = json['business_id'];
|
||||
status = json['status'];
|
||||
createdAt = json['created_at'];
|
||||
updatedAt = json['updated_at'];
|
||||
}
|
||||
|
||||
num? id;
|
||||
String? unitName;
|
||||
num? businessId;
|
||||
num? status;
|
||||
String? createdAt;
|
||||
String? updatedAt;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['id'] = id;
|
||||
map['unitName'] = unitName;
|
||||
map['business_id'] = businessId;
|
||||
map['status'] = status;
|
||||
map['created_at'] = createdAt;
|
||||
map['updated_at'] = updatedAt;
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../Products/Repo/unit_repo.dart';
|
||||
import '../../product_unit/model/unit_model.dart';
|
||||
|
||||
final unitsProvider = FutureProvider<List<Unit>>((ref) => UnitsRepo().fetchAllUnits());
|
||||
178
lib/Screens/product_unit/unit_list.dart
Normal file
178
lib/Screens/product_unit/unit_list.dart
Normal file
@@ -0,0 +1,178 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:mobile_pos/Provider/profile_provider.dart';
|
||||
import 'package:mobile_pos/Screens/product_unit/provider/product_unit_provider.dart';
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
import 'package:mobile_pos/generated/l10n.dart' as lang;
|
||||
import 'package:nb_utils/nb_utils.dart';
|
||||
|
||||
import '../../GlobalComponents/glonal_popup.dart';
|
||||
import '../../http_client/custome_http_client.dart';
|
||||
import '../../widgets/empty_widget/_empty_widget.dart';
|
||||
import '../Products/Repo/unit_repo.dart';
|
||||
import '../../service/check_user_role_permission_provider.dart';
|
||||
import '../hrm/widgets/deleteing_alart_dialog.dart';
|
||||
import '../product_category/product_category_list_screen.dart';
|
||||
import 'add_units.dart';
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class UnitList extends StatefulWidget {
|
||||
const UnitList({super.key, required this.isFromProductList});
|
||||
|
||||
final bool isFromProductList;
|
||||
|
||||
@override
|
||||
// ignore: library_private_types_in_public_api
|
||||
_UnitListState createState() => _UnitListState();
|
||||
}
|
||||
|
||||
class _UnitListState extends State<UnitList> {
|
||||
String search = '';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GlobalPopup(
|
||||
child: Scaffold(
|
||||
backgroundColor: kWhite,
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
lang.S.of(context).units,
|
||||
),
|
||||
iconTheme: const IconThemeData(color: Colors.black),
|
||||
centerTitle: true,
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0.0,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Consumer(builder: (context, ref, __) {
|
||||
final unitData = ref.watch(unitsProvider);
|
||||
final businessInfo = ref.watch(businessInfoProvider);
|
||||
final permissionService = PermissionService(ref);
|
||||
return businessInfo.when(data: (details) {
|
||||
if (!permissionService.hasPermission(Permit.categoriesRead.value)) {
|
||||
return Center(child: PermitDenyWidget());
|
||||
}
|
||||
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,
|
||||
),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: GestureDetector(
|
||||
onTap: () async {
|
||||
final currentIndex = context;
|
||||
const AddUnits().launch(currentIndex);
|
||||
},
|
||||
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,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
unitData.when(data: (data) {
|
||||
return data.isNotEmpty
|
||||
? ListView.builder(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
itemCount: data.length,
|
||||
itemBuilder: (context, i) {
|
||||
return (data[i].unitName ?? '').toLowerCase().contains(search.toLowerCase())
|
||||
? ListCardWidget(
|
||||
onSelect: widget.isFromProductList
|
||||
? () {}
|
||||
: () {
|
||||
Navigator.pop(context, data[i]);
|
||||
},
|
||||
title: data[i].unitName.toString(),
|
||||
onDelete: () async {
|
||||
if (!permissionService.hasPermission(Permit.unitsDelete.value)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.red,
|
||||
content: Text('You do not have permission to delete unit.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
bool confirmDelete = await showDeleteConfirmationDialog(context: context, itemName: 'unit');
|
||||
if (confirmDelete) {
|
||||
EasyLoading.show();
|
||||
if (await UnitsRepo().deleteUnit(context: context, unitId: data[i].id ?? 0, ref: ref)) {
|
||||
ref.refresh(unitsProvider);
|
||||
}
|
||||
EasyLoading.dismiss();
|
||||
}
|
||||
},
|
||||
onEdit: () async {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => AddUnits(
|
||||
unit: data[i],
|
||||
),
|
||||
));
|
||||
},
|
||||
)
|
||||
: Container();
|
||||
})
|
||||
: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Text(
|
||||
lang.S.of(context).noDataFound,
|
||||
//'No Data Found'
|
||||
),
|
||||
);
|
||||
}, error: (_, __) {
|
||||
return Container();
|
||||
}, loading: () {
|
||||
return const CircularProgressIndicator();
|
||||
}),
|
||||
],
|
||||
);
|
||||
}, error: (e, stack) {
|
||||
return Text(e.toString());
|
||||
}, loading: () {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
});
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user