pickImageFromGallery method

Future<File?> pickImageFromGallery({
  1. double? maxWidth,
  2. double? maxHeight,
  3. int? imageQuality,
  4. bool enableCrop = false,
  5. CropSettings? cropSettings,
})

Implementation

Future<File?> pickImageFromGallery({
  double? maxWidth,
  double? maxHeight,
  int? imageQuality,
  bool enableCrop = false,
  CropSettings? cropSettings,
}) async {
  bool hasPermission = await _checkGalleryPermission();
  if (!hasPermission) return null;

  try {
    final XFile? image = await _picker.pickImage(
      source: ImageSource.gallery,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      imageQuality: imageQuality,
    );

    if (image != null) {
      File imageFile = File(image.path);

      if (enableCrop) {
        imageFile = await cropImage(imageFile, cropSettings: cropSettings) ??
            imageFile;
      }

      return imageFile;
    }
  } catch (e) {
    print('Error picking gallery image: $e');
  }
  return null;
}