mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +00:00
Refactor create state (#184)
* Refactor createstate for api_form - Refer to attributes of widget * Refactor barcode state variables * More udpates
This commit is contained in:
parent
13ebaf43e1
commit
277193ecb0
@ -974,31 +974,19 @@ class APIFormWidget extends StatefulWidget {
|
||||
final Function(Map<String, dynamic>)? onSuccess;
|
||||
|
||||
@override
|
||||
_APIFormWidgetState createState() => _APIFormWidgetState(title, url, fields, method, onSuccess, fileField, icon);
|
||||
_APIFormWidgetState createState() => _APIFormWidgetState();
|
||||
|
||||
}
|
||||
|
||||
|
||||
class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
_APIFormWidgetState(this.title, this.url, this.fields, this.method, this.onSuccess, this.fileField, this.icon) : super();
|
||||
_APIFormWidgetState() : super();
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
final String title;
|
||||
|
||||
final String url;
|
||||
|
||||
final String method;
|
||||
|
||||
final String fileField;
|
||||
|
||||
final IconData icon;
|
||||
|
||||
List<String> nonFieldErrors = [];
|
||||
|
||||
List<APIFormField> fields;
|
||||
|
||||
Function(Map<String, dynamic>)? onSuccess;
|
||||
|
||||
bool spacerRequired = false;
|
||||
@ -1030,7 +1018,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
}
|
||||
|
||||
for (var field in fields) {
|
||||
for (var field in widget.fields) {
|
||||
|
||||
if (field.hidden) {
|
||||
continue;
|
||||
@ -1087,13 +1075,13 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
Future<APIResponse> _submit(Map<String, dynamic> data) async {
|
||||
|
||||
// If a file upload is required, we have to handle the submission differently
|
||||
if (fileField.isNotEmpty) {
|
||||
if (widget.fileField.isNotEmpty) {
|
||||
|
||||
// Pop the "file" field
|
||||
data.remove(fileField);
|
||||
data.remove(widget.fileField);
|
||||
|
||||
for (var field in fields) {
|
||||
if (field.name == fileField) {
|
||||
for (var field in widget.fields) {
|
||||
if (field.name == widget.fileField) {
|
||||
|
||||
File? file = field.attachedfile;
|
||||
|
||||
@ -1101,9 +1089,9 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
// A valid file has been supplied
|
||||
final response = await InvenTreeAPI().uploadFile(
|
||||
url,
|
||||
widget.url,
|
||||
file,
|
||||
name: fileField,
|
||||
name: widget.fileField,
|
||||
fields: data,
|
||||
);
|
||||
|
||||
@ -1113,9 +1101,9 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
if (method == "POST") {
|
||||
if (widget.method == "POST") {
|
||||
final response = await InvenTreeAPI().post(
|
||||
url,
|
||||
widget.url,
|
||||
body: data,
|
||||
expectedStatusCode: null
|
||||
);
|
||||
@ -1124,7 +1112,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
} else {
|
||||
final response = await InvenTreeAPI().patch(
|
||||
url,
|
||||
widget.url,
|
||||
body: data,
|
||||
expectedStatusCode: null
|
||||
);
|
||||
@ -1182,7 +1170,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
match = true;
|
||||
continue;
|
||||
default:
|
||||
for (var field in fields) {
|
||||
for (var field in widget.fields) {
|
||||
|
||||
// Hidden fields can't display errors, so we won't match
|
||||
if (field.hidden) {
|
||||
@ -1239,7 +1227,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
// Iterate through and find "simple" top-level fields
|
||||
|
||||
for (var field in fields) {
|
||||
for (var field in widget.fields) {
|
||||
|
||||
if (field.isSimple) {
|
||||
// Simple top-level field data
|
||||
@ -1275,7 +1263,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
// An "empty" URL means we don't want to submit the form anywhere
|
||||
// Perhaps we just want to process the data?
|
||||
if (url.isEmpty) {
|
||||
if (widget.url.isEmpty) {
|
||||
// Hide the form
|
||||
Navigator.pop(context);
|
||||
|
||||
@ -1290,7 +1278,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
final response = await _submit(data);
|
||||
|
||||
if (!response.isValid()) {
|
||||
showServerError(url, L10().serverError, L10().responseInvalid);
|
||||
showServerError(widget.url, L10().serverError, L10().responseInvalid);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -1324,7 +1312,7 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
);
|
||||
|
||||
// Update field errors
|
||||
for (var field in fields) {
|
||||
for (var field in widget.fields) {
|
||||
field.extractErrorMessages(response);
|
||||
}
|
||||
|
||||
@ -1380,10 +1368,10 @@ class _APIFormWidgetState extends State<APIFormWidget> {
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(title),
|
||||
title: Text(widget.title),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: FaIcon(icon),
|
||||
icon: FaIcon(widget.icon),
|
||||
onPressed: () {
|
||||
|
||||
if (_formKey.currentState!.validate()) {
|
||||
|
@ -606,20 +606,18 @@ class InvenTreeQRView extends StatefulWidget {
|
||||
final BarcodeHandler _handler;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _QRViewState(_handler);
|
||||
State<StatefulWidget> createState() => _QRViewState();
|
||||
}
|
||||
|
||||
|
||||
class _QRViewState extends State<InvenTreeQRView> {
|
||||
|
||||
_QRViewState(this._handler) : super();
|
||||
_QRViewState() : super();
|
||||
|
||||
final GlobalKey qrKey = GlobalKey(debugLabel: "QR");
|
||||
|
||||
QRViewController? _controller;
|
||||
|
||||
final BarcodeHandler _handler;
|
||||
|
||||
bool flash_status = false;
|
||||
|
||||
Future<void> updateFlashStatus() async {
|
||||
@ -652,7 +650,7 @@ class _QRViewState extends State<InvenTreeQRView> {
|
||||
_controller?.pauseCamera();
|
||||
|
||||
if (barcode.code != null) {
|
||||
_handler.processBarcode(_controller, barcode.code ?? "");
|
||||
widget._handler.processBarcode(_controller, barcode.code ?? "");
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -711,7 +709,7 @@ class _QRViewState extends State<InvenTreeQRView> {
|
||||
children: [
|
||||
Spacer(),
|
||||
Padding(
|
||||
child: Text(_handler.getOverlayText(context),
|
||||
child: Text(widget._handler.getOverlayText(context),
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white),
|
||||
|
@ -236,14 +236,12 @@ class ProfileEditWidget extends StatefulWidget {
|
||||
final UserProfile? profile;
|
||||
|
||||
@override
|
||||
_ProfileEditState createState() => _ProfileEditState(profile);
|
||||
_ProfileEditState createState() => _ProfileEditState();
|
||||
}
|
||||
|
||||
class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
|
||||
_ProfileEditState(this.profile) : super();
|
||||
|
||||
UserProfile? profile;
|
||||
_ProfileEditState() : super();
|
||||
|
||||
final formKey = GlobalKey<FormState>();
|
||||
|
||||
@ -258,7 +256,7 @@ class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(profile == null ? L10().profileAdd : L10().profileEdit),
|
||||
title: Text(widget.profile == null ? L10().profileAdd : L10().profileEdit),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.save),
|
||||
@ -266,7 +264,7 @@ class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
if (formKey.currentState!.validate()) {
|
||||
formKey.currentState!.save();
|
||||
|
||||
UserProfile? prf = profile;
|
||||
UserProfile? prf = widget.profile;
|
||||
|
||||
if (prf == null) {
|
||||
UserProfile profile = UserProfile(
|
||||
@ -307,7 +305,7 @@ class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
labelText: L10().profileName,
|
||||
labelStyle: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
initialValue: profile?.name ?? "",
|
||||
initialValue: widget.profile?.name ?? "",
|
||||
maxLines: 1,
|
||||
keyboardType: TextInputType.text,
|
||||
onSaved: (value) {
|
||||
@ -327,7 +325,7 @@ class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
labelStyle: TextStyle(fontWeight: FontWeight.bold),
|
||||
hintText: "http[s]://<server>:<port>",
|
||||
),
|
||||
initialValue: profile?.server ?? "",
|
||||
initialValue: widget.profile?.server ?? "",
|
||||
keyboardType: TextInputType.url,
|
||||
onSaved: (value) {
|
||||
server = value?.trim() ?? "";
|
||||
@ -374,7 +372,7 @@ class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
labelStyle: TextStyle(fontWeight: FontWeight.bold),
|
||||
hintText: L10().enterUsername
|
||||
),
|
||||
initialValue: profile?.username ?? "",
|
||||
initialValue: widget.profile?.username ?? "",
|
||||
keyboardType: TextInputType.text,
|
||||
onSaved: (value) {
|
||||
username = value?.trim() ?? "";
|
||||
@ -401,7 +399,7 @@ class _ProfileEditState extends State<ProfileEditWidget> {
|
||||
},
|
||||
),
|
||||
),
|
||||
initialValue: profile?.password ?? "",
|
||||
initialValue: widget.profile?.password ?? "",
|
||||
keyboardType: TextInputType.visiblePassword,
|
||||
obscureText: _obscured,
|
||||
onSaved: (value) {
|
||||
|
@ -26,17 +26,13 @@ class AttachmentWidget extends StatefulWidget {
|
||||
final bool hasUploadPermission;
|
||||
|
||||
@override
|
||||
_AttachmentWidgetState createState() => _AttachmentWidgetState(attachment, referenceId, hasUploadPermission);
|
||||
_AttachmentWidgetState createState() => _AttachmentWidgetState();
|
||||
}
|
||||
|
||||
|
||||
class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
|
||||
|
||||
_AttachmentWidgetState(this.attachment, this.referenceId, this.hasUploadPermission);
|
||||
|
||||
final InvenTreeAttachment attachment;
|
||||
final int referenceId;
|
||||
final bool hasUploadPermission;
|
||||
_AttachmentWidgetState();
|
||||
|
||||
List<InvenTreeAttachment> attachments = [];
|
||||
|
||||
@ -48,7 +44,7 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
|
||||
|
||||
List<Widget> actions = [];
|
||||
|
||||
if (hasUploadPermission) {
|
||||
if (widget.hasUploadPermission) {
|
||||
// File upload
|
||||
actions.add(
|
||||
IconButton(
|
||||
@ -69,7 +65,7 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
|
||||
|
||||
Future<void> upload(File file) async {
|
||||
|
||||
final bool result = await attachment.uploadAttachment(file, referenceId);
|
||||
final bool result = await widget.attachment.uploadAttachment(file, widget.referenceId);
|
||||
|
||||
if (result) {
|
||||
showSnackIcon(L10().uploadSuccess, success: true);
|
||||
@ -83,9 +79,9 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
|
||||
@override
|
||||
Future<void> request(BuildContext context) async {
|
||||
|
||||
await attachment.list(
|
||||
await widget.attachment.list(
|
||||
filters: {
|
||||
attachment.REFERENCE_FIELD: referenceId.toString()
|
||||
widget.attachment.REFERENCE_FIELD: widget.referenceId.toString()
|
||||
}
|
||||
).then((var results) {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user