mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-10-31 05:15:42 +00:00 
			
		
		
		
	Refactor part image upload and part attachment upload
This commit is contained in:
		| @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; | |||||||
| import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||||
| import 'package:image_picker/image_picker.dart'; | import 'package:image_picker/image_picker.dart'; | ||||||
| import 'package:inventree/inventree/part.dart'; | import 'package:inventree/inventree/part.dart'; | ||||||
|  | import 'package:inventree/widget/fields.dart'; | ||||||
| import 'package:inventree/widget/refreshable_state.dart'; | import 'package:inventree/widget/refreshable_state.dart'; | ||||||
| import 'package:inventree/widget/snacks.dart'; | import 'package:inventree/widget/snacks.dart'; | ||||||
|  |  | ||||||
| @@ -46,16 +47,14 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget | |||||||
|       // File upload |       // File upload | ||||||
|       actions.add( |       actions.add( | ||||||
|         IconButton( |         IconButton( | ||||||
|           icon: FaIcon(FontAwesomeIcons.fileUpload), |           icon: FaIcon(FontAwesomeIcons.plusCircle), | ||||||
|           onPressed: uploadFile, |           onPressed: () async { | ||||||
|         ) |             FilePickerDialog.pickFile( | ||||||
|       ); |               onPicked: (File file) { | ||||||
|  |                 upload(file); | ||||||
|       // Upload from camera |               } | ||||||
|       actions.add( |             ); | ||||||
|         IconButton( |           }, | ||||||
|           icon: FaIcon(FontAwesomeIcons.camera), |  | ||||||
|           onPressed: uploadFromCamera, |  | ||||||
|         ) |         ) | ||||||
|       ); |       ); | ||||||
|     } |     } | ||||||
| @@ -77,45 +76,6 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget | |||||||
|       showSnackIcon(L10().uploadFailed, success: false); |       showSnackIcon(L10().uploadFailed, success: false); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     refresh(); |  | ||||||
|  |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   /* |  | ||||||
|    * Select a file from the device to upload |  | ||||||
|    */ |  | ||||||
|   Future<void> uploadFile() async { |  | ||||||
|  |  | ||||||
|     final FilePickerResult? result = await FilePicker.platform.pickFiles(); |  | ||||||
|  |  | ||||||
|     if (result != null) { |  | ||||||
|  |  | ||||||
|       String? path = result.files.single.path; |  | ||||||
|  |  | ||||||
|       if (path != null) { |  | ||||||
|         File attachment = File(path); |  | ||||||
|  |  | ||||||
|         upload(attachment); |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   /* |  | ||||||
|    * Upload an attachment by taking a new picture with the built in device camera |  | ||||||
|    */ |  | ||||||
|   Future<void> uploadFromCamera() async { |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     final picker = ImagePicker(); |  | ||||||
|  |  | ||||||
|     final pickedImage = await picker.getImage(source: ImageSource.camera); |  | ||||||
|  |  | ||||||
|     if (pickedImage != null) { |  | ||||||
|       File? attachment = File(pickedImage.path); |  | ||||||
|       upload(attachment); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     refresh(); |     refresh(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ import 'package:inventree/api.dart'; | |||||||
|  |  | ||||||
| import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||||
| import 'package:inventree/inventree/part.dart'; | import 'package:inventree/inventree/part.dart'; | ||||||
|  | import 'package:inventree/widget/fields.dart'; | ||||||
| import 'package:inventree/widget/refreshable_state.dart'; | import 'package:inventree/widget/refreshable_state.dart'; | ||||||
| import 'package:inventree/widget/snacks.dart'; | import 'package:inventree/widget/snacks.dart'; | ||||||
|  |  | ||||||
| @@ -37,45 +38,6 @@ class _PartImageState extends RefreshableState<PartImageWidget> { | |||||||
|     await part.reload(); |     await part.reload(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void uploadFromGallery() async { |  | ||||||
|  |  | ||||||
|     final picker = ImagePicker(); |  | ||||||
|  |  | ||||||
|     final pickedImage = await picker.getImage(source: ImageSource.gallery); |  | ||||||
|  |  | ||||||
|     if (pickedImage != null) { |  | ||||||
|       File? img = File(pickedImage.path); |  | ||||||
|  |  | ||||||
|       final result = await part.uploadImage(img); |  | ||||||
|  |  | ||||||
|       if (!result) { |  | ||||||
|         showSnackIcon(L10().uploadFailed, success: false); |  | ||||||
|       } |  | ||||||
|  |  | ||||||
|       refresh(); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   void uploadFromCamera() async { |  | ||||||
|  |  | ||||||
|     final picker = ImagePicker(); |  | ||||||
|  |  | ||||||
|     final pickedImage = await picker.getImage(source: ImageSource.camera); |  | ||||||
|  |  | ||||||
|     if (pickedImage != null) { |  | ||||||
|       File? img = File(pickedImage.path); |  | ||||||
|  |  | ||||||
|       final result = await part.uploadImage(img); |  | ||||||
|  |  | ||||||
|       if (!result) { |  | ||||||
|         showSnackIcon(L10().uploadFailed, success: false); |  | ||||||
|       } |  | ||||||
|  |  | ||||||
|       refresh(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   String getAppBarTitle(BuildContext context) => part.fullname; |   String getAppBarTitle(BuildContext context) => part.fullname; | ||||||
|  |  | ||||||
| @@ -89,16 +51,22 @@ class _PartImageState extends RefreshableState<PartImageWidget> { | |||||||
|       // File upload |       // File upload | ||||||
|       actions.add( |       actions.add( | ||||||
|         IconButton( |         IconButton( | ||||||
|           icon: FaIcon(FontAwesomeIcons.fileImage), |           icon: FaIcon(FontAwesomeIcons.fileUpload), | ||||||
|           onPressed: uploadFromGallery, |           onPressed: () async { | ||||||
|         ) |  | ||||||
|       ); |  | ||||||
|  |  | ||||||
|       // Camera upload |             FilePickerDialog.pickFile( | ||||||
|       actions.add( |               onPicked: (File file) async { | ||||||
|         IconButton( |                 final result = await part.uploadImage(file); | ||||||
|           icon: FaIcon(FontAwesomeIcons.camera), |  | ||||||
|           onPressed: uploadFromCamera, |                 if (!result) { | ||||||
|  |                   showSnackIcon(L10().uploadFailed, success: false); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 refresh(); | ||||||
|  |               } | ||||||
|  |             ); | ||||||
|  |  | ||||||
|  |           }, | ||||||
|         ) |         ) | ||||||
|       ); |       ); | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user