2022-09-01 15:06:47 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2022-08-31 10:19:57 +02:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
|
2022-09-05 09:54:42 +02:00
|
|
|
/// The Image Picker Service class is the functionality of the Image Picker package which uses the Image Picker package to choose an image.
|
|
|
|
/// If you have your own implementation of the Image Picker you can add it to the constructor when creating the class.
|
2022-08-31 10:19:57 +02:00
|
|
|
class ImagePickerService {
|
2022-09-02 16:48:45 +02:00
|
|
|
ImagePickerService({this.imagePicker});
|
|
|
|
|
2022-09-05 09:54:42 +02:00
|
|
|
/// It's possible to have your own implementation for the Image Picker if you don't want to use the Image Picker Package.
|
2022-09-02 16:48:45 +02:00
|
|
|
ImagePicker? imagePicker;
|
2022-08-31 10:19:57 +02:00
|
|
|
|
2022-09-01 15:06:47 +02:00
|
|
|
/// [pickImage] is the function that picks the image and returns it as a [Uint8List].
|
2022-09-01 10:22:32 +02:00
|
|
|
/// The function requires [source], an [ImageSource] that's the method of how the image needs to be picked, for example gallery or camera.
|
2022-09-01 15:06:47 +02:00
|
|
|
Future<Uint8List?> pickImage(ImageSource source) async {
|
2022-08-31 10:19:57 +02:00
|
|
|
var image =
|
2022-09-02 16:48:45 +02:00
|
|
|
await (await (imagePicker ?? ImagePicker()).pickImage(source: source))
|
|
|
|
?.readAsBytes();
|
2022-09-01 15:06:47 +02:00
|
|
|
return image;
|
2022-08-31 10:19:57 +02:00
|
|
|
}
|
|
|
|
}
|