You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/pages/device_transfer/update_device_transfer.dart

223 lines
8.1 KiB
Dart

import 'dart:convert';
3 years ago
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import '../../../api/user_api_client.dart';
import '../../../controllers/http_status_manger/http_status_manger.dart';
import '../../../controllers/localization/localization.dart';
import '../../../controllers/providers/api/device_transfer_provider.dart';
import '../../../controllers/providers/settings/setting_provider.dart';
import '../../../controllers/providers/user_provider.dart';
import '../../../models/device/device_transfer.dart';
import '../../../models/device/device_transfer_info.dart';
import '../../../models/subtitle.dart';
import '../../../models/user.dart';
import '../../app_style/sizing.dart';
import '../../widgets/app_text_form_field.dart';
import '../../widgets/buttons/app_button.dart';
import '../../widgets/e_signature/e_signature.dart';
import '../../widgets/loaders/loading_manager.dart';
import '../../widgets/status/gas_refill/gas_status.dart';
import '../../widgets/titles/app_sub_title.dart';
3 years ago
class UpdateDeviceTransfer extends StatefulWidget {
final DeviceTransfer model;
final bool? isSender;
const UpdateDeviceTransfer({Key? key, required this.model, this.isSender}) : super(key: key);
3 years ago
@override
State<UpdateDeviceTransfer> createState() => _UpdateDeviceTransferState();
}
class _UpdateDeviceTransferState extends State<UpdateDeviceTransfer> {
bool _isLoading = false;
bool _validate = false;
Subtitle? _subtitle;
UserProvider? _userProvider;
SettingProvider? _settingProvider;
Uint8List? _signature;
DeviceTransferProvider? _deviceTransferProvider;
3 years ago
final TextEditingController _requestedQuantityController = TextEditingController();
final DeviceTransferInfo _formModel = DeviceTransferInfo();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
_update() async {
_validate = true;
if ((_formKey.currentState?.validate() ?? false)) {
3 years ago
setState(() {});
return false;
}
_formKey.currentState?.save();
3 years ago
_isLoading = true;
3 years ago
setState(() {});
int? status = await _deviceTransferProvider?.updateRequest(
user: UserApiClient().user ?? User(), host: _settingProvider?.host ?? "", requestId: widget.model?.id.toString() ?? "", isSender: widget.isSender ?? false, newModel: _formModel, oldModel: widget.model);
_isLoading = false;
3 years ago
setState(() {});
if (status != null && status >= 200 && status < 300) {
3 years ago
Fluttertoast.showToast(
msg: _subtitle?.requestCompleteSuccessfully ?? "",
3 years ago
);
_validate = false;
Navigator.of(context).pop();
Navigator.of(context).pop();
} else {
String errorMessage = HttpStatusManger.getStatusMessage(status: status, subtitle: _subtitle);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
3 years ago
}
}
@override
void setState(VoidCallback fn) {
if (mounted) super.setState(() {});
3 years ago
}
@override
void initState() {
_formModel.fromDetails((widget.isSender ?? false) ? widget.model?.sender : widget.model?.receiver, withSignature: false);
3 years ago
super.initState();
}
@override
void dispose() {
_requestedQuantityController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_subtitle = AppLocalization.of(context)?.subtitle;
3 years ago
_userProvider = Provider.of<UserProvider>(context);
_settingProvider = Provider.of<SettingProvider>(context);
_deviceTransferProvider = Provider.of<DeviceTransferProvider>(context, listen: false);
3 years ago
return Scaffold(
key: _scaffoldKey,
body: Form(
key: _formKey,
child: SafeArea(
child: LoadingManager(
isLoading: _isLoading,
isFailedLoading: false,
stateCode: 200,
onRefresh: () async {},
child: SingleChildScrollView(
padding: EdgeInsets.all(12 * AppStyle.getScaleFactor(context)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Edit Transfer Device",
style: Theme.of(context).textTheme.headline5?.copyWith(color: Theme.of(context).primaryColor, fontSize: 28, fontWeight: FontWeight.bold),
3 years ago
),
),
),
const SizedBox(
height: 8,
),
3 years ago
ASubTitle("Comment"),
const SizedBox(
height: 4,
),
3 years ago
ATextFormField(
initialValue: _formModel.comment ?? "",
3 years ago
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subtitle1,
textInputType: TextInputType.text,
onSaved: (value) {
3 years ago
_formModel.comment = value;
},
),
const SizedBox(
height: 8,
),
ASubTitle(_subtitle?.travelingHours ?? ""),
const SizedBox(
height: 4,
),
3 years ago
ATextFormField(
initialValue: _formModel.travelingHours ?? "",
3 years ago
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subtitle1,
textInputType: TextInputType.number,
onSaved: (value) {
3 years ago
_formModel.travelingHours = value;
},
),
const SizedBox(
height: 8,
),
ASubTitle(_subtitle?.workingHours ?? ""),
const SizedBox(
height: 4,
),
3 years ago
ATextFormField(
initialValue: _formModel.workingHours ?? "",
3 years ago
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subtitle1,
textInputType: TextInputType.number,
onSaved: (value) {
3 years ago
_formModel.workingHours = value;
},
),
const SizedBox(
height: 8,
),
ASubTitle(_subtitle?.status ?? ""),
const SizedBox(
height: 4,
),
3 years ago
GasStatusMenu(
initialValue: _formModel.status,
onSelect: (status) {
3 years ago
_formModel.status = status;
setState(() {});
},
),
const SizedBox(
height: 8,
),
3 years ago
const ASubTitle("Signature"),
// if(_validate && _formModel.signature == null)
// ASubTitle(_subtitle.requiredWord,color: Colors.red,),
const SizedBox(
height: 4,
),
3 years ago
ESignature(
oldSignature: (widget.isSender ?? false) ? widget.model?.sender?.signature : widget.model?.receiver?.signature,
newSignature: _signature,
onSaved: (signature) {
_signature = signature;
_formModel.signature = base64Encode(signature);
},
3 years ago
),
Padding(
padding: const EdgeInsets.all(16.0),
child: AButton(
text: _subtitle?.update ?? "",
3 years ago
onPressed: _update,
),
),
const SizedBox(
height: 100,
)
3 years ago
],
),
),
),
),
),
);
}
}