From 277193ecb0e757cd3e9a95adaa046dcd25e9f6a6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Jul 2022 23:34:26 +1000 Subject: [PATCH] Refactor create state (#184) * Refactor createstate for api_form - Refer to attributes of widget * Refactor barcode state variables * More udpates --- lib/api_form.dart | 50 ++++++++++++------------------- lib/barcode.dart | 10 +++---- lib/settings/login.dart | 18 +++++------ lib/widget/attachment_widget.dart | 16 ++++------ 4 files changed, 37 insertions(+), 57 deletions(-) diff --git a/lib/api_form.dart b/lib/api_form.dart index 8aef06a2..a4a1ed5c 100644 --- a/lib/api_form.dart +++ b/lib/api_form.dart @@ -974,31 +974,19 @@ class APIFormWidget extends StatefulWidget { final Function(Map)? onSuccess; @override - _APIFormWidgetState createState() => _APIFormWidgetState(title, url, fields, method, onSuccess, fileField, icon); + _APIFormWidgetState createState() => _APIFormWidgetState(); } class _APIFormWidgetState extends State { - _APIFormWidgetState(this.title, this.url, this.fields, this.method, this.onSuccess, this.fileField, this.icon) : super(); + _APIFormWidgetState() : super(); final _formKey = GlobalKey(); - final String title; - - final String url; - - final String method; - - final String fileField; - - final IconData icon; - List nonFieldErrors = []; - List fields; - Function(Map)? onSuccess; bool spacerRequired = false; @@ -1030,7 +1018,7 @@ class _APIFormWidgetState extends State { } - for (var field in fields) { + for (var field in widget.fields) { if (field.hidden) { continue; @@ -1087,13 +1075,13 @@ class _APIFormWidgetState extends State { Future _submit(Map 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 { // 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 { } } - 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 { } else { final response = await InvenTreeAPI().patch( - url, + widget.url, body: data, expectedStatusCode: null ); @@ -1182,7 +1170,7 @@ class _APIFormWidgetState extends State { 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 { // 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 { // 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 { 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 { ); // 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 { 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()) { diff --git a/lib/barcode.dart b/lib/barcode.dart index c5825e51..10a40bad 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -606,20 +606,18 @@ class InvenTreeQRView extends StatefulWidget { final BarcodeHandler _handler; @override - State createState() => _QRViewState(_handler); + State createState() => _QRViewState(); } class _QRViewState extends State { - _QRViewState(this._handler) : super(); + _QRViewState() : super(); final GlobalKey qrKey = GlobalKey(debugLabel: "QR"); QRViewController? _controller; - final BarcodeHandler _handler; - bool flash_status = false; Future updateFlashStatus() async { @@ -652,7 +650,7 @@ class _QRViewState extends State { _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 { children: [ Spacer(), Padding( - child: Text(_handler.getOverlayText(context), + child: Text(widget._handler.getOverlayText(context), style: TextStyle( fontWeight: FontWeight.bold, color: Colors.white), diff --git a/lib/settings/login.dart b/lib/settings/login.dart index 743ba9ad..279540e4 100644 --- a/lib/settings/login.dart +++ b/lib/settings/login.dart @@ -236,14 +236,12 @@ class ProfileEditWidget extends StatefulWidget { final UserProfile? profile; @override - _ProfileEditState createState() => _ProfileEditState(profile); + _ProfileEditState createState() => _ProfileEditState(); } class _ProfileEditState extends State { - _ProfileEditState(this.profile) : super(); - - UserProfile? profile; + _ProfileEditState() : super(); final formKey = GlobalKey(); @@ -258,7 +256,7 @@ class _ProfileEditState extends State { 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 { 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 { 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 { labelStyle: TextStyle(fontWeight: FontWeight.bold), hintText: "http[s]://:", ), - initialValue: profile?.server ?? "", + initialValue: widget.profile?.server ?? "", keyboardType: TextInputType.url, onSaved: (value) { server = value?.trim() ?? ""; @@ -374,7 +372,7 @@ class _ProfileEditState extends State { 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 { }, ), ), - initialValue: profile?.password ?? "", + initialValue: widget.profile?.password ?? "", keyboardType: TextInputType.visiblePassword, obscureText: _obscured, onSaved: (value) { diff --git a/lib/widget/attachment_widget.dart b/lib/widget/attachment_widget.dart index 0a95d0fb..518316fd 100644 --- a/lib/widget/attachment_widget.dart +++ b/lib/widget/attachment_widget.dart @@ -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 { - _AttachmentWidgetState(this.attachment, this.referenceId, this.hasUploadPermission); - - final InvenTreeAttachment attachment; - final int referenceId; - final bool hasUploadPermission; + _AttachmentWidgetState(); List attachments = []; @@ -48,7 +44,7 @@ class _AttachmentWidgetState extends RefreshableState { List actions = []; - if (hasUploadPermission) { + if (widget.hasUploadPermission) { // File upload actions.add( IconButton( @@ -69,7 +65,7 @@ class _AttachmentWidgetState extends RefreshableState { Future 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 { @override Future 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) {