From 7317f9cbadd6c9bfd08f2ae2233207880d107180 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 16 Aug 2021 17:47:55 +1000 Subject: [PATCH] New class for selecting an image --- lib/l10n | 2 +- lib/widget/fields.dart | 125 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/lib/l10n b/lib/l10n index 48ed29ca..fc01d982 160000 --- a/lib/l10n +++ b/lib/l10n @@ -1 +1 @@ -Subproject commit 48ed29ca98d35585721e6af5718d7dcb08869705 +Subproject commit fc01d9826d51e15377894d9cb6347c61b22e3be1 diff --git a/lib/widget/fields.dart b/lib/widget/fields.dart index 75abe85e..42ca8e90 100644 --- a/lib/widget/fields.dart +++ b/lib/widget/fields.dart @@ -1,12 +1,135 @@ +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:image_picker/image_picker.dart'; import 'package:inventree/l10.dart'; import 'dart:async'; import 'dart:io'; -// TODO - Perhaps refactor all this using flutter_form_builder - https://pub.dev/packages/flutter_form_builder +import 'package:one_context/one_context.dart'; + + + +class FilePickerDialog { + + static Future pickImageFromCamera() async { + + final picker = ImagePicker(); + + final pickedImage = await picker.pickImage(source: ImageSource.camera); + + if (pickedImage != null) { + return File(pickedImage.path); + } + + return null; + } + + static Future pickImageFromGallery() async { + + final picker = ImagePicker(); + + final pickedImage = await picker.pickImage(source: ImageSource.gallery); + + if (pickedImage != null) { + return File(pickedImage.path); + } + + return null; + } + + static Future pickFileFromDevice() async { + + final FilePickerResult? result = await FilePicker.platform.pickFiles(); + + if (result != null) { + String? path = result.files.single.path; + + if (path != null) { + return File(path); + } + + return null; + } + } + + // Present a dialog to pick a file, either from local file system or from camera + static Future pickFile({bool allowImages = true, bool allowFiles = true, Function(File)? onPicked}) async { + + String title = ""; + + if (allowImages && !allowFiles) { + title = L10().selectImage; + } else { + title = L10().selectFile; + } + + // Construct actions + List actions = []; + + actions.add( + SimpleDialogOption( + child: ListTile( + leading: FaIcon(FontAwesomeIcons.fileUpload), + title: Text(allowFiles ? L10().selectFile : L10().selectImage), + ), + onPressed: () async { + + // Close the dialog + OneContext().popDialog(); + + File? file; + if (allowFiles) { + file = await pickFileFromDevice(); + } else { + file = await pickImageFromGallery(); + } + + if (file != null) { + if (onPicked != null) { + onPicked(file); + } + } + }, + ) + ); + + if (allowImages) { + actions.add( + SimpleDialogOption( + child: ListTile( + leading: FaIcon(FontAwesomeIcons.camera), + title: Text(L10().takePicture), + ), + onPressed: () async { + // Close the dialog + OneContext().popDialog(); + + File? file = await pickImageFromCamera(); + + if (file != null) { + if (onPicked != null) { + onPicked(file); + } + } + } + ) + ); + } + + OneContext().showDialog( + builder: (context) { + return SimpleDialog( + title: Text(title), + children: actions, + ); + } + ); + } + +} /*