gas refill and asset transfer ui completed waiting for api
parent
c9b3538720
commit
3f90630db9
@ -0,0 +1,424 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
|
||||
import 'package:test_sa/controllers/providers/api/gas_refill_provider.dart';
|
||||
import 'package:test_sa/controllers/providers/api/user_provider.dart';
|
||||
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
|
||||
import 'package:test_sa/controllers/validator/validator.dart';
|
||||
import 'package:test_sa/extensions/context_extension.dart';
|
||||
import 'package:test_sa/extensions/int_extensions.dart';
|
||||
import 'package:test_sa/extensions/string_extensions.dart';
|
||||
import 'package:test_sa/extensions/widget_extensions.dart';
|
||||
import 'package:test_sa/models/lookup.dart';
|
||||
import 'package:test_sa/models/timer_model.dart';
|
||||
import 'package:test_sa/new_views/app_style/app_color.dart';
|
||||
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
|
||||
import 'package:test_sa/providers/loading_list_notifier.dart';
|
||||
import 'package:test_sa/service_request_latest/utilities/service_request_utils.dart';
|
||||
import 'package:test_sa/views/widgets/images/multi_image_picker.dart';
|
||||
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
|
||||
|
||||
import '../../../../controllers/providers/api/hospitals_provider.dart';
|
||||
import '../../../../extensions/text_extensions.dart';
|
||||
import '../../../../models/new_models/gas_refill_model.dart';
|
||||
import '../../../../new_views/common_widgets/app_text_form_field.dart';
|
||||
import '../../../../new_views/common_widgets/default_app_bar.dart';
|
||||
import '../../../../new_views/common_widgets/single_item_drop_down_menu.dart';
|
||||
import '../../../../providers/gas_request_providers/gas_status_provider.dart';
|
||||
import '../../../widgets/e_signature/e_signature.dart';
|
||||
import '../../../widgets/timer/app_timer.dart';
|
||||
|
||||
class GasRefillForm extends StatefulWidget {
|
||||
static const String id = "/gas-refill-form";
|
||||
final GasRefillModel? gasRefillModel;
|
||||
|
||||
const GasRefillForm({this.gasRefillModel, Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<GasRefillForm> createState() => _GasRefillFormState();
|
||||
}
|
||||
|
||||
class _GasRefillFormState extends State<GasRefillForm> {
|
||||
final bool _isLoading = false;
|
||||
bool _validate = false;
|
||||
Uint8List? _engineerSignature;
|
||||
Uint8List? _nurseSignature;
|
||||
double totalWorkingHours = 0.0;
|
||||
|
||||
late UserProvider _userProvider;
|
||||
late SettingProvider _settingProvider;
|
||||
GasRefillProvider? _gasRefillProvider;
|
||||
GasRefillDetails _currentDetails = GasRefillDetails();
|
||||
final TextEditingController _deliveredQuantityController = TextEditingController();
|
||||
final TextEditingController _commentController = TextEditingController();
|
||||
final TextEditingController _workingHoursController = TextEditingController();
|
||||
|
||||
final GasRefillModel _formModel = GasRefillModel(gazRefillDetails: []);
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
final GlobalKey _DetailsKey = GlobalKey<FormState>();
|
||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||
bool _firstTime = true;
|
||||
|
||||
Lookup? _deliveredQuantity;
|
||||
|
||||
static List<Lookup> deliveredQuantity = [
|
||||
Lookup(name: "1", id: 1, value: 1),
|
||||
Lookup(name: "2", id: 2, value: 2),
|
||||
Lookup(name: "3", id: 3, value: 3),
|
||||
Lookup(name: "4", id: 4, value: 4),
|
||||
Lookup(name: "5", id: 5, value: 5)
|
||||
];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_gasRefillProvider ??= Provider.of<GasRefillProvider>(context, listen: false);
|
||||
|
||||
if (widget.gasRefillModel != null) {
|
||||
_formModel.fromGasRefillModel(widget.gasRefillModel!);
|
||||
|
||||
// totalWorkingHours =
|
||||
// _formModel?.fold(0.0, (sum, item) => (sum ?? 0) + DateTime.parse(item.endDateTime!).difference(DateTime.parse(item.startDateTime!)).inSeconds) ?? 0;
|
||||
|
||||
_commentController.text = _formModel.comment ?? "";
|
||||
try {
|
||||
_deliveredQuantity = deliveredQuantity.singleWhere((element) => element.value == _formModel.gazRefillDetails![0].deliverdQty);
|
||||
_currentDetails.deliverdQty = _deliveredQuantity!.value;
|
||||
} catch (ex) {}
|
||||
}
|
||||
if (_formModel.gasRefillAttachments != null && _formModel.gasRefillAttachments!.isNotEmpty) {
|
||||
_gasRefillProvider?.gasRefillAttachments = [];
|
||||
_gasRefillProvider?.gasRefillAttachments.addAll(_formModel.gasRefillAttachments!.map((e) => File(e.attachmentName!)).toList());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void setState(VoidCallback fn) {
|
||||
if (mounted) super.setState(() {});
|
||||
}
|
||||
|
||||
_onSubmit(BuildContext context) async {
|
||||
if (_formModel.status == null) {
|
||||
await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.status}");
|
||||
return false;
|
||||
}
|
||||
if (_formModel.timer?.startAt == null) {
|
||||
await Fluttertoast.showToast(msg: "Working Hours Required");
|
||||
return false;
|
||||
}
|
||||
if (_formModel.timer?.endAt == null) {
|
||||
await Fluttertoast.showToast(msg: "Please Stop The Timer");
|
||||
return false;
|
||||
}
|
||||
if (_formModel.gazRefillDetails?.isNotEmpty ?? false) {
|
||||
if (!(await _addNewModel(context))) return;
|
||||
}
|
||||
|
||||
//TODO need to check when added in backend....
|
||||
_formModel.gasRefillTimer = _formModel.gasRefillTimer ?? [];
|
||||
_formModel.timerModelList?.forEach((timer) {
|
||||
int durationInSecond = timer.endAt!.difference(timer.startAt!).inSeconds;
|
||||
_formModel.gasRefillTimer?.add(
|
||||
GasRefillTimer(
|
||||
id: 0,
|
||||
startTime: timer.startAt!.toIso8601String(), // Handle potential null
|
||||
endTime: timer.endAt?.toIso8601String(), // Handle potential null
|
||||
workingHours: ((durationInSecond) / 60 / 60),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
_formModel.gasRefillAttachments = [];
|
||||
for (var item in _gasRefillProvider!.gasRefillAttachments) {
|
||||
_formModel.gasRefillAttachments
|
||||
?.add(GasRefillAttachments(id: 0, attachmentName: ServiceRequestUtils.isLocalUrl(item.path) ? "${item.path.split("/").last}|${base64Encode(item.readAsBytesSync())}" : item.path));
|
||||
}
|
||||
|
||||
//..till here...
|
||||
|
||||
setState(() {});
|
||||
_formKey.currentState!.save();
|
||||
_formModel.comment = _commentController.text;
|
||||
int? status = widget.gasRefillModel == null
|
||||
? 0 /*await _gasRefillProvider.createModel(
|
||||
user: _userProvider.user,
|
||||
model: _formModel,
|
||||
)*/
|
||||
: await _gasRefillProvider!.updateModel(
|
||||
user: _userProvider.user!,
|
||||
host: _settingProvider.host!,
|
||||
oldModel: widget.gasRefillModel!,
|
||||
newModel: _formModel,
|
||||
);
|
||||
//_isLoading = false;
|
||||
setState(() {});
|
||||
if (status >= 200 && status < 300) {
|
||||
Fluttertoast.showToast(
|
||||
msg: context.translation.successfulRequestMessage,
|
||||
);
|
||||
Navigator.of(context).pop(_formModel);
|
||||
setState(() {});
|
||||
} else {
|
||||
String errorMessage = HttpStatusManger.getStatusMessage(status: status, subtitle: context.translation);
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text(errorMessage),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
bool _addNewModel(BuildContext context) {
|
||||
_validate = true;
|
||||
if (_currentDetails.deliverdQty == null) {
|
||||
Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.quantity}");
|
||||
setState(() {});
|
||||
return false;
|
||||
}
|
||||
// if (!_formKey.currentState!.validate()) {
|
||||
// setState(() {});
|
||||
// return false;
|
||||
// }
|
||||
_formKey.currentState!.save();
|
||||
_currentDetails.gasType = _formModel.gazRefillDetails![0].gasType;
|
||||
_currentDetails.cylinderSize = _formModel.gazRefillDetails![0].cylinderSize;
|
||||
_currentDetails.cylinderType = _formModel.gazRefillDetails![0].cylinderType;
|
||||
_currentDetails.requestedQty = _formModel.gazRefillDetails![0].requestedQty;
|
||||
if (!(_currentDetails.validate(context))) {
|
||||
setState(() {});
|
||||
return false;
|
||||
}
|
||||
_formModel.gazRefillDetails![0].deliverdQty = _currentDetails.deliverdQty;
|
||||
_validate = false;
|
||||
// Scrollable.ensureVisible(_DetailsKey.currentContext);
|
||||
_deliveredQuantityController.clear();
|
||||
_workingHoursController.clear();
|
||||
//_commentController.clear();
|
||||
_currentDetails = GasRefillDetails();
|
||||
setState(() {});
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_deliveredQuantityController.dispose();
|
||||
_commentController.dispose();
|
||||
_workingHoursController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void updateTimer({TimerModel? timer}) {
|
||||
_formModel.timer = timer;
|
||||
if (timer?.startAt != null && timer?.endAt != null) {
|
||||
_formModel.timerModelList = _formModel.timerModelList ?? [];
|
||||
_formModel.timerModelList!.add(timer!);
|
||||
}
|
||||
// notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_userProvider = Provider.of<UserProvider>(context);
|
||||
_settingProvider = Provider.of<SettingProvider>(context);
|
||||
if (_firstTime) {
|
||||
String? clientName;
|
||||
if (widget.gasRefillModel != null) {
|
||||
//_formModel.status = widget.gasRefillModel?.status ?? Lookup(value: 0);
|
||||
_gasRefillProvider!.expectedDateTime = DateTime.tryParse(_formModel.expectedDate ?? "");
|
||||
_formModel.timer = TimerModel(startAt: DateTime.tryParse(widget.gasRefillModel?.startDate ?? ""), endAt: DateTime.tryParse(widget.gasRefillModel?.endDate ?? ""));
|
||||
clientName = _formModel.site?.custName;
|
||||
} else {
|
||||
_formModel.timer = null;
|
||||
clientName = _userProvider.user?.clientName;
|
||||
}
|
||||
|
||||
HospitalsProvider().getHospitalsListByVal(searchVal: clientName ?? '').then((value) {
|
||||
_gasRefillProvider!.hospital = value.firstWhere((element) => element.name == clientName, orElse: null);
|
||||
_gasRefillProvider!.building = _gasRefillProvider!.hospital?.buildings?.firstWhere((element) => element.name == widget.gasRefillModel?.building?.name, orElse: null);
|
||||
_gasRefillProvider!.floor = _gasRefillProvider!.building?.floors?.firstWhere((element) => element.name == widget.gasRefillModel?.floor?.name, orElse: null);
|
||||
_gasRefillProvider!.department = _gasRefillProvider!.floor?.departments?.firstWhere((element) => element.name == widget.gasRefillModel?.department?.departmentName, orElse: null);
|
||||
_firstTime = false;
|
||||
setState(() {});
|
||||
});
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: DefaultAppBar(title: context.translation.gasRefill),
|
||||
key: _scaffoldKey,
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: SafeArea(
|
||||
child: LoadingManager(
|
||||
isLoading: _isLoading,
|
||||
isFailedLoading: false,
|
||||
stateCode: 200,
|
||||
onRefresh: () async {},
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Column(
|
||||
// crossAxisAlignment: CrossAxisAlignment.start,
|
||||
// children: [
|
||||
// context.translation.gasRefill.heading5(context),
|
||||
// 8.height,
|
||||
// '${context.translation.gasRequest}: ${widget.gasRefillModel!.gazRefillDetails![0].gasType!.name}'.bodyText(context),
|
||||
// '${context.translation.cylinderType}: ${widget.gasRefillModel!.gazRefillDetails![0].cylinderType!.name}'.bodyText(context),
|
||||
// '${context.translation.cylinderSize}: ${widget.gasRefillModel!.gazRefillDetails![0].cylinderSize!.name}'.bodyText(context),
|
||||
// '${context.translation.quantity}: ${widget.gasRefillModel!.gazRefillDetails![0].requestedQty ?? 0}'.bodyText(context),
|
||||
// '${context.translation.site}: ${widget.gasRefillModel!.site?.name}'.bodyText(context),
|
||||
// ],
|
||||
// ).toShadowContainer(context),
|
||||
8.height,
|
||||
SingleItemDropDownMenu<Lookup, NullableLoadingProvider>(
|
||||
context: context,
|
||||
title: context.translation.quantity,
|
||||
backgroundColor: AppColor.neutral100,
|
||||
showShadow: false,
|
||||
initialValue: _deliveredQuantity,
|
||||
staticData: deliveredQuantity,
|
||||
onSelect: (value) {
|
||||
_deliveredQuantity = value;
|
||||
_currentDetails.deliverdQty = value!.value;
|
||||
},
|
||||
),
|
||||
8.height,
|
||||
_timerWidget(context, _formModel.workingHours ?? 0),
|
||||
8.height,
|
||||
// SingleItemDropDownMenu<Lookup, GasStatusProvider>(
|
||||
// context: context,
|
||||
// title: context.translation.requestStatus,
|
||||
// initialValue: _formModel.status,
|
||||
// onSelect: (value) {
|
||||
// if (value?.value == 0) {
|
||||
// "Status cannot be change to ${value?.name}.".addTranslation.showToast;
|
||||
// setState(() {});
|
||||
// return;
|
||||
// }
|
||||
// if (value != null) {
|
||||
// _formModel.status = value;
|
||||
// }
|
||||
// },
|
||||
// ),
|
||||
// 8.height,
|
||||
|
||||
// 8.height,
|
||||
// AppTextFormField(
|
||||
// initialValue: _formModel.gazRefillDetails?[0].deliverdQty?.toString() ?? "",
|
||||
// labelText: context.translation.deliveredQuantity,
|
||||
// onSaved: (value) {
|
||||
// _currentDetails.deliverdQty = double.tryParse(value);
|
||||
// },
|
||||
// textInputType: TextInputType.number,
|
||||
// controller: _deliveredQuantityController,
|
||||
// validator: (value) => value == null || value.isEmpty
|
||||
// ? context.translation.requiredField
|
||||
// : Validator.isNumeric(value)
|
||||
// ? null
|
||||
// : context.translation.onlyNumbers,
|
||||
// ),
|
||||
8.height,
|
||||
|
||||
/// TBD
|
||||
AppTextFormField(
|
||||
labelText: context.translation.technicalComment,
|
||||
textInputType: TextInputType.multiline,
|
||||
alignLabelWithHint: true,
|
||||
backgroundColor: AppColor.neutral100,
|
||||
showShadow: false,
|
||||
controller: _commentController,
|
||||
onSaved: (value) {},
|
||||
),
|
||||
16.height,
|
||||
MultiFilesPicker(
|
||||
label: context.translation.attachFiles,
|
||||
files: _gasRefillProvider!.gasRefillAttachments,
|
||||
buttonColor: AppColor.black10,
|
||||
onlyImages: false,
|
||||
buttonIcon: 'image-plus'.toSvgAsset(color: AppColor.neutral120),
|
||||
),
|
||||
8.height,
|
||||
// ESignature(
|
||||
// title: "Engineer Signature",
|
||||
// oldSignature: widget.gasRefillModel?.engSignature,
|
||||
// newSignature: _engineerSignature,
|
||||
// onSaved: (signature) {
|
||||
// _engineerSignature = signature;
|
||||
// if (signature == null || signature.isEmpty) return;
|
||||
// _formModel.engSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
|
||||
// //base64Encode(signature);
|
||||
// },
|
||||
// ),
|
||||
// 8.height,
|
||||
// ESignature(
|
||||
// title: "Nurse Signature",
|
||||
// oldSignature: widget.gasRefillModel?.nurseSignature,
|
||||
// newSignature: _nurseSignature,
|
||||
// onSaved: (signature) {
|
||||
// _nurseSignature = signature;
|
||||
// if (signature == null || signature.isEmpty) return;
|
||||
// _formModel.nurseSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
|
||||
// //base64Encode(signature);
|
||||
// },
|
||||
// ),
|
||||
],
|
||||
).toShadowContainer(context),
|
||||
).expanded,
|
||||
AppFilledButton(
|
||||
label: widget.gasRefillModel == null ? context.translation.submit : context.translation.update,
|
||||
onPressed: () async {
|
||||
_onSubmit.call(context);
|
||||
},
|
||||
).paddingAll(16),
|
||||
],
|
||||
)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _timerWidget(BuildContext context, double totalWorkingHours) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
AppTimer(
|
||||
label: context.translation.workingHours,
|
||||
timer: _formModel.timer,
|
||||
//TODO need to fix this..
|
||||
// enabled: _formModel.endDate == null,
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColor.neutral100,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
timerProgress: (isRunning) {},
|
||||
onChange: (timer) async {
|
||||
updateTimer(timer: timer);
|
||||
return true;
|
||||
},
|
||||
),
|
||||
if (totalWorkingHours > 0.0) ...[
|
||||
12.height,
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
'Total Working Time:'.bodyText2(context).custom(color: AppColor.neutral120),
|
||||
8.width,
|
||||
Text(
|
||||
ServiceRequestUtils.formatTimerDuration(totalWorkingHours.round()),
|
||||
style: AppTextStyles.bodyText.copyWith(color: AppColor.neutral50, fontWeight: FontWeight.w600),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue