cropImage static method
Future<String?>
cropImage(
- BuildContext context, {
- required String path,
- List<
CropAspectRatioPreset> aspectRatioPresets = const [CropAspectRatioPreset.square], - CropStyle cropStyle = CropStyle.rectangle,
- required void onError(
- 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;
}
}