cropImage method

  1. @override
Future<CroppedFile?> cropImage({
  1. required String sourcePath,
  2. int? maxWidth,
  3. int? maxHeight,
  4. CropAspectRatio? aspectRatio,
  5. List<CropAspectRatioPreset> aspectRatioPresets = const [CropAspectRatioPreset.original, CropAspectRatioPreset.square, CropAspectRatioPreset.ratio3x2, CropAspectRatioPreset.ratio4x3, CropAspectRatioPreset.ratio16x9],
  6. CropStyle cropStyle = CropStyle.rectangle,
  7. ImageCompressFormat compressFormat = ImageCompressFormat.jpg,
  8. int compressQuality = 90,
  9. List<PlatformUiSettings>? uiSettings,
})
override

Launch cropper UI for an image.

parameters:

return:

A result file of the cropped image.

Implementation

@override
Future<CroppedFile?> cropImage({
  required String sourcePath,
  int? maxWidth,
  int? maxHeight,
  CropAspectRatio? aspectRatio,
  List<CropAspectRatioPreset> aspectRatioPresets = const [
    CropAspectRatioPreset.original,
    CropAspectRatioPreset.square,
    CropAspectRatioPreset.ratio3x2,
    CropAspectRatioPreset.ratio4x3,
    CropAspectRatioPreset.ratio16x9
  ],
  CropStyle cropStyle = CropStyle.rectangle,
  ImageCompressFormat compressFormat = ImageCompressFormat.jpg,
  int compressQuality = 90,
  List<PlatformUiSettings>? uiSettings,
}) async {
  assert(await File(sourcePath).exists());
  assert(maxWidth == null || maxWidth > 0);
  assert(maxHeight == null || maxHeight > 0);
  assert(compressQuality >= 0 && compressQuality <= 100);

  final arguments = <String, dynamic>{
    'source_path': sourcePath,
    'max_width': maxWidth,
    'max_height': maxHeight,
    'ratio_x': aspectRatio?.ratioX,
    'ratio_y': aspectRatio?.ratioY,
    'aspect_ratio_presets':
        aspectRatioPresets.map<String>(aspectRatioPresetName).toList(),
    'crop_style': cropStyleName(cropStyle),
    'compress_format': compressFormatName(compressFormat),
    'compress_quality': compressQuality,
  };

  if (uiSettings != null) {
    for (final settings in uiSettings) {
      arguments.addAll(settings.toMap());
    }
  }

  final String? resultPath =
      await methodChannel.invokeMethod('cropImage', arguments);
  return resultPath == null ? null : CroppedFile(resultPath);
}