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