mirror of
https://github.com/inventree/inventree-app.git
synced 2025-05-13 12:33:11 +00:00
Refactor part image upload and part attachment upload
This commit is contained in:
parent
7317f9cbad
commit
3f63f1e8a7
@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:inventree/inventree/part.dart';
|
||||
import 'package:inventree/widget/fields.dart';
|
||||
import 'package:inventree/widget/refreshable_state.dart';
|
||||
import 'package:inventree/widget/snacks.dart';
|
||||
|
||||
@ -46,16 +47,14 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
|
||||
// File upload
|
||||
actions.add(
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.fileUpload),
|
||||
onPressed: uploadFile,
|
||||
)
|
||||
);
|
||||
|
||||
// Upload from camera
|
||||
actions.add(
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.camera),
|
||||
onPressed: uploadFromCamera,
|
||||
icon: FaIcon(FontAwesomeIcons.plusCircle),
|
||||
onPressed: () async {
|
||||
FilePickerDialog.pickFile(
|
||||
onPicked: (File file) {
|
||||
upload(file);
|
||||
}
|
||||
);
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -77,45 +76,6 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import 'package:inventree/api.dart';
|
||||
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:inventree/inventree/part.dart';
|
||||
import 'package:inventree/widget/fields.dart';
|
||||
import 'package:inventree/widget/refreshable_state.dart';
|
||||
import 'package:inventree/widget/snacks.dart';
|
||||
|
||||
@ -37,45 +38,6 @@ class _PartImageState extends RefreshableState<PartImageWidget> {
|
||||
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
|
||||
String getAppBarTitle(BuildContext context) => part.fullname;
|
||||
|
||||
@ -89,16 +51,22 @@ class _PartImageState extends RefreshableState<PartImageWidget> {
|
||||
// File upload
|
||||
actions.add(
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.fileImage),
|
||||
onPressed: uploadFromGallery,
|
||||
)
|
||||
);
|
||||
icon: FaIcon(FontAwesomeIcons.fileUpload),
|
||||
onPressed: () async {
|
||||
|
||||
// Camera upload
|
||||
actions.add(
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.camera),
|
||||
onPressed: uploadFromCamera,
|
||||
FilePickerDialog.pickFile(
|
||||
onPicked: (File file) async {
|
||||
final result = await part.uploadImage(file);
|
||||
|
||||
if (!result) {
|
||||
showSnackIcon(L10().uploadFailed, success: false);
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
);
|
||||
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user