2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-05-06 01:08:53 +00:00

New class for selecting an image

This commit is contained in:
Oliver 2021-08-16 17:47:55 +10:00
parent b9c9b8acc1
commit 7317f9cbad
2 changed files with 125 additions and 2 deletions

@ -1 +1 @@
Subproject commit 48ed29ca98d35585721e6af5718d7dcb08869705 Subproject commit fc01d9826d51e15377894d9cb6347c61b22e3be1

View File

@ -1,12 +1,135 @@
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.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/l10.dart'; import 'package:inventree/l10.dart';
import 'dart:async'; import 'dart:async';
import 'dart:io'; 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<File?> pickImageFromCamera() async {
final picker = ImagePicker();
final pickedImage = await picker.pickImage(source: ImageSource.camera);
if (pickedImage != null) {
return File(pickedImage.path);
}
return null;
}
static Future<File?> pickImageFromGallery() async {
final picker = ImagePicker();
final pickedImage = await picker.pickImage(source: ImageSource.gallery);
if (pickedImage != null) {
return File(pickedImage.path);
}
return null;
}
static Future<File?> 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<void> 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<Widget> 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,
);
}
);
}
}
/* /*