imagePicker static method
dynamic
imagePicker({
- required FileData fileData,
- bool crop = false,
- int? maxFileSizeInMb,
- bool cropOnlySquare = false,
- String cropperToolbarTitle = Files.cropperToolbarTitle,
- Color cropperToolbarColor = Files.cropperToolbarColor,
- Color cropperToolbarWidgetsColor = Files.cropperToolbarWidgetsColor,
- required dynamic onSelected(
- FileData fileData
- dynamic onCancel(
- String message
function image picker for pick image from gallery and save to temporary cache directory
Implementation
static imagePicker(
{required FileData fileData,
bool crop = false,
int? maxFileSizeInMb,
bool cropOnlySquare = false,
String cropperToolbarTitle = Files.cropperToolbarTitle,
Color cropperToolbarColor = Files.cropperToolbarColor,
Color cropperToolbarWidgetsColor = Files.cropperToolbarWidgetsColor,
required Function(FileData fileData) onSelected,
Function(String message)? onCancel}) async {
XFile? image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image != null) {
String filePath = "";
if (crop) {
CroppedFile? croppedImage = await Files._imageCrop(
filePath: image.path,
cropOnlySquare: cropOnlySquare,
cropperToolbarTitle: cropperToolbarTitle,
cropperToolbarColor: cropperToolbarColor,
cropperToolbarWidgetsColor: cropperToolbarWidgetsColor);
if (croppedImage != null) {
filePath = croppedImage.path;
}
} else {
filePath = image.path;
}
if (!Files._isNullOREmpty(filePath)) {
if (maxFileSizeInMb != null &&
Files.mb(File(filePath).readAsBytesSync().lengthInBytes) >
maxFileSizeInMb) {
dev.log(Files._fileMoreThanMB(maxFileSizeInMb));
if (onCancel != null) {
onCancel(Files._fileMoreThanMB(maxFileSizeInMb));
}
return;
}
fileData.hasFile = true;
fileData.fileName = Files.getFileName(filePath);
fileData.filePath = filePath;
fileData.fileMimeType = Files.getMimeType(filePath);
fileData.path = filePath;
onSelected(fileData);
} else {
dev.log(Files._filePickCancel);
if (onCancel != null) {
onCancel(Files._filePickCancel);
}
return;
}
} else {
dev.log(Files._filePickCancel);
if (onCancel != null) {
onCancel(Files._filePickCancel);
}
return;
}
}