import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';

/*
 * 图片裁剪引擎封装，目前使用的是 UCorp 框架
 */
class ImageCropEngine {
  /// 开启图片裁剪 - 返回裁剪后的图片的路径 (默认裁剪后的路径在App/Data/Cache目录下)
  static Future<String?> imageCrop(
    BuildContext context,
    String filePath, {
    CropAspectRatioPreset aspect = CropAspectRatioPreset.square,
  }) async {
    CroppedFile? croppedFile = await ImageCropper().cropImage(
      sourcePath: filePath,
      aspectRatioPresets: [
        aspect,
      ],
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: 'Image Crop',
            toolbarColor: const Color(0xff212121),
            statusBarColor: const Color(0xff212121),
            toolbarWidgetColor: Colors.white,
            initAspectRatio: CropAspectRatioPreset.original,
            lockAspectRatio: false),
        IOSUiSettings(
          title: 'Image Crop',
        ),
        WebUiSettings(
          context: context,
        ),
      ],
    );

    if (croppedFile != null) {
      return croppedFile.path;
    }
    return null;
  }
}
