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

220 lines
7.8 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 '../../../controllers/http_status_manger/http_status_manger.dart';
import '../../../controllers/localization/localization.dart';
import '../../../controllers/providers/api/device_transfer_provider.dart';
import '../../../controllers/providers/api/user_provider.dart';
import '../../../controllers/providers/settings/setting_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, 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;
setState(() {});
int? status = await _deviceTransferProvider?.updateRequest(
user: _userProvider?.user??User(),
host: _settingProvider?.host??"",
requestId: widget.model?.id??"",
isSender: widget.isSender??false,
3 years ago
newModel: _formModel,
oldModel: widget.model
);
_isLoading =false;
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
),
)
);
}
}
@override
void setState(VoidCallback fn){
if(mounted) super.setState(() {});
}
@override
void initState() {
_formModel.fromDetails((widget.isSender??false) ? widget.model?.sender : widget.model?.receiver,
3 years ago
withSignature: false
);
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);
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(
3 years ago
color: Theme.of(context).primaryColor,
fontSize: 28,
fontWeight: FontWeight.bold
),
),
),
),
const SizedBox(height: 8,),
ASubTitle("Comment"),
const SizedBox(height: 4,),
ATextFormField(
initialValue: _formModel.comment??"",
3 years ago
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subtitle1,
textInputType: TextInputType.text,
onSaved: (value){
_formModel.comment = value;
},
),
const SizedBox(height: 8,),
ASubTitle(_subtitle?.travelingHours??""),
3 years ago
const SizedBox(height: 4,),
ATextFormField(
initialValue: _formModel.travelingHours??"",
3 years ago
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subtitle1,
textInputType: TextInputType.number,
onSaved: (value){
_formModel.travelingHours = value;
},
),
const SizedBox(height: 8,),
ASubTitle(_subtitle?.workingHours??""),
3 years ago
const SizedBox(height: 4,),
ATextFormField(
initialValue: _formModel.workingHours??"",
3 years ago
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subtitle1,
textInputType: TextInputType.number,
onSaved: (value){
_formModel.workingHours = value;
},
),
const SizedBox(height: 8,),
ASubTitle(_subtitle?.status??""),
3 years ago
const SizedBox(height: 4,),
GasStatusMenu(
initialValue: _formModel.status,
onSelect: (status){
_formModel.status = status;
setState(() {});
},
),
const SizedBox(height: 8,),
const ASubTitle("Signature"),
// if(_validate && _formModel.signature == null)
// ASubTitle(_subtitle.requiredWord,color: Colors.red,),
const SizedBox(height: 4,),
ESignature(
oldSignature: (widget.isSender??false)
? widget.model?.sender?.signature
: widget.model?.receiver?.signature,
3 years ago
newSignature: _signature,
onSaved: (signature){
_signature = signature;
_formModel.signature = base64Encode(signature);
},
),
Padding(
padding: const EdgeInsets.all(16.0),
child: AButton(
text: _subtitle?.update??"",
3 years ago
onPressed: _update,
),
),
const SizedBox(height: 100,)
],
),
),
),
),
),
);
}
}