imagePicker static method

dynamic imagePicker({
  1. required FileData fileData,
  2. required dynamic onSelected(
    1. FileData fileData
    ),
  3. dynamic onCancel(
    1. String message,
    2. int messageCode
    )?,
  4. bool crop = false,
  5. int? maxFileSizeInMb,
  6. bool cropOnlySquare = false,
  7. String cropperToolbarTitle = Files.cropperToolbarTitle,
  8. Color cropperToolbarColor = Files.cropperToolbarColor,
  9. Color cropperToolbarWidgetsColor = Files.cropperToolbarWidgetsColor,
})

function image picker for pick image from gallery and save to temporary cache directory

Implementation

static imagePicker(
    {required FileData fileData,
    required Function(FileData fileData) onSelected,
    Function(String message, int messageCode)? onCancel,
    bool crop = false,
    int? maxFileSizeInMb,
    bool cropOnlySquare = false,
    String cropperToolbarTitle = Files.cropperToolbarTitle,
    Color cropperToolbarColor = Files.cropperToolbarColor,
    Color cropperToolbarWidgetsColor =
        Files.cropperToolbarWidgetsColor}) 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._mcFPCForSize}] ${Files._fileMoreThanMB(maxFileSizeInMb)}");
        if (onCancel != null) {
          onCancel(
              Files._fileMoreThanMB(maxFileSizeInMb), Files._mcFPCForSize);
        }
        return;
      }
      FileData fileData = FileData(
          hasFile: true,
          fileName: Files.getFileName(filePath),
          filePath: filePath,
          fileMimeType: Files.getMimeType(filePath),
          path: filePath);
      onSelected(fileData);
    } else {
      dev.log("[${Files._mcFPCForCancel}] ${Files._filePickCancel}");
      if (onCancel != null) {
        onCancel(Files._filePickCancel, Files._mcFPCForCancel);
      }
      return;
    }
  } else {
    dev.log("[${Files._mcFPCForCancel}] ${Files._filePickCancel}");
    if (onCancel != null) {
      onCancel(Files._filePickCancel, Files._mcFPCForCancel);
    }
    return;
  }
}