imagePicker static method

dynamic imagePicker({
  1. required FileData fileData,
  2. bool crop = false,
  3. int? maxFileSizeInMb,
  4. bool cropOnlySquare = false,
  5. String cropperToolbarTitle = Files.cropperToolbarTitle,
  6. Color cropperToolbarColor = Files.cropperToolbarColor,
  7. Color cropperToolbarWidgetsColor = Files.cropperToolbarWidgetsColor,
  8. required dynamic onSelected(
    1. FileData fileData
    ),
  9. dynamic onCancel(
    1. 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;
  }
}