cropImage static method

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

Crops an image at the specified path using the given aspectRatioPresets.

Returns the path of the cropped image or null if cropping fails. Calls onError in case of an error.

Implementation

static Future<String?> cropImage(
  BuildContext context, {
  required String path,
  List<CropAspectRatioPreset> aspectRatioPresets = const [
    CropAspectRatioPreset.square,
  ],
  CropStyle cropStyle = CropStyle.rectangle,
  required void Function(String e) onError,
}) async {
  // Helper function to map aspect ratio preset names to the appropriate values

  try {
    final croppedFile = await image_cropper.ImageCropper().cropImage(
      sourcePath: path,
      uiSettings: [
        image_cropper.AndroidUiSettings(
          initAspectRatio:
              mapAspectRatioPreset(aspectRatioPresets.first.name),
          cropStyle: cropStyle == CropStyle.circle
              ? image_cropper.CropStyle.circle
              : image_cropper.CropStyle.rectangle,
          toolbarTitle: 'Crop',
          aspectRatioPresets: aspectRatioPresets
              .map((e) => mapAspectRatioPreset(e.name))
              .toList(),
        ),
        image_cropper.IOSUiSettings(
          title: 'Crop',
          cropStyle: cropStyle == CropStyle.circle
              ? image_cropper.CropStyle.circle
              : image_cropper.CropStyle.rectangle,
          aspectRatioPresets: aspectRatioPresets
              .map((e) => mapAspectRatioPreset(e.name))
              .toList(),
        ),
        image_cropper.WebUiSettings(
          context: context,
        ),
      ],
    );
    return croppedFile?.path;
  } catch (e) {
    onError('$e');
    return null;
  }
}