crop_image_utils.dart 2.43 KB
import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';

class CropImageUtils {
  static cropImage(BuildContext context, String path) async {
    final croppedFile = await ImageCropper().cropImage(
      sourcePath: path,
      compressFormat: ImageCompressFormat.jpg,
      compressQuality: 100,
      uiSettings: [
        AndroidUiSettings(
            // backgroundColor: Colors.red,
            // toolbarTitle: 'Cropper',
            // toolbarColor: Colors.deepOrange,
            // toolbarWidgetColor: Colors.white,
            // initAspectRatio: CropAspectRatioPreset.ratio5x4,
            cropStyle: CropStyle.rectangle,
            aspectRatioPresets: [CropAspectRatioPreset.ratio5x4],
            hideBottomControls: true,
            lockAspectRatio: true),
        IOSUiSettings(
            // title: 'Cropper',
            // rotateButtonsHidden: true,
            // rotateClockwiseButtonHidden: true,
            cropStyle: CropStyle.rectangle,
            aspectRatioPresets: [CropAspectRatioPreset.ratio5x4],
            resetAspectRatioEnabled: false,
            doneButtonTitle: '确定',
            cancelButtonTitle: '取消',
            aspectRatioLockEnabled: true),
        // AndroidUiSettings(
        //   toolbarTitle: '裁剪',
        //   toolbarColor: Colors.white,
        //   toolbarWidgetColor: Colors.white,
        //   initAspectRatio: CropAspectRatioPreset.square,
        //   lockAspectRatio: false,
        //   aspectRatioPresets: [
        //     // CropAspectRatioPreset.original,
        //     // CropAspectRatioPreset.square,
        //     CropAspectRatioPreset.ratio5x4,
        //     // CropAspectRatioPresetCustom(),
        //   ],
        // ),
        // IOSUiSettings(
        //   title: '裁剪',
        //   aspectRatioPresets: [
        //     // CropAspectRatioPreset.original,
        //     // CropAspectRatioPreset.square,
        //     CropAspectRatioPreset.ratio5x4,
        //     // CropAspectRatioPresetCustom(),
        //   ],
        // ),
        WebUiSettings(
          context: context,
          presentStyle: WebPresentStyle.dialog,
          size: const CropperSize(
            width: 500,
            height:400,
          ),
        ),
      ],
    );

    return croppedFile;
  }
}

// class CropAspectRatioPresetCustom implements CropAspectRatioPresetData {
//   @override
//   (int, int)? get data => (5, 4);

//   @override
//   String get name => '5x4';
// }