pickAndCropImage static method

Future<String?> pickAndCropImage(
  1. BuildContext context, {
  2. ImageSource source = ImageSource.gallery,
  3. List<CropAspectRatioPreset> aspectRatioPresets = const [CropAspectRatioPreset.square],
  4. CropStyle cropStyle = CropStyle.rectangle,
  5. required void onError(
    1. String e
    ),
})

Picks an image from the gallery or camera and allows the user to crop it.

The context is used to show the image picker and crop UI. The source determines whether the image is picked from the gallery or the camera (default is ImageSource.gallery). The aspectRatioPresets defines a list of aspect ratio presets available for cropping (default is CropAspectRatioPreset.square). The cropStyle defines the shape of the cropped image (default is CropStyle.rectangle). The onError callback is invoked in case of any error, passing the error message as a string.

Returns the path of the cropped image if successful, or null if any step fails.

Implementation

static Future<String?> pickAndCropImage(
  BuildContext context, {
  ImageSource source = ImageSource.gallery,
  List<CropAspectRatioPreset> aspectRatioPresets = const [
    CropAspectRatioPreset.square
  ],
  CropStyle cropStyle = CropStyle.rectangle,
  required void Function(String e) onError,
}) async {
  try {
    final pickedFile = await _imagePicker.pickImage(
      source: source == ImageSource.camera
          ? image_picker.ImageSource.camera
          : image_picker.ImageSource.gallery,
    );
    if (pickedFile == null) return null;

    final path = pickedFile.path;
    final cropPath = await cropImage(
      context,
      path: path,
      aspectRatioPresets: aspectRatioPresets,
      cropStyle: cropStyle,
      onError: onError,
    );
    return cropPath;
  } catch (e) {
    onError(e.toString());
    return null;
  }
}