Flutter i trying to crop/resize image into 1:1 ratio but i got a problem when i use image_cropper package

3.7k Views Asked by At

im trying to crop an image that i retrieve from gallery or take a photo and i need to resize the image into 1:1 size and upload it to firebase

this is the code that i already try but i got some error

 Future<Null> _cropImage(File image) async {
    File? croppedImage = await ImageCropper.cropImage(
        sourcePath: image.path,
        maxWidth: 1080,
        maxHeight: 1080,
        aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
    );
    if (croppedImage  != null) {
        imageFile = croppedImage ;
        setState(() {});
    }
  }

this is the code that i use to get image and crop the image

down below is my original code that i used to take image without no problem

Future _getCameraImage() async {
    final image = await ImagePicker().pickImage(
        source: ImageSource.camera,
        imageQuality: 40,
        maxHeight: 800,
        maxWidth: 800);

    setState(() {
      _image = File(image!.path);
    });
  } 

and this is the error i got from flutter

and when i put the _getCameraImage in my TextButton.icon onpressed like this. they didt gave me anything

TextButton.icon(
                            onPressed: () => _getCameraImage,
                            icon: const Icon(Icons.camera_alt),
                            label: const Text('Camera'),
                          ),

i want to get image from camera and display it on my imageview

enter image description here

1

There are 1 best solutions below

7
On

I think image cropper library's read me file is not updated properly or it was an mistake from their end, thats why they show ImageCropper.cropImage instead of ImageCropper().cropImage.

As per image cropper library example, you have to create the image cropper object first to use the cropping mechanism because cropImage is not an static method.

So try to use ImageCropper().cropImage

 Future<Null> _cropImage(File image) async {
    File? croppedImage = await ImageCropper().cropImage(
        sourcePath: image.path,
        maxWidth: 1080,
        maxHeight: 1080,
        aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
    );
    if (croppedImage  != null) {
        imageFile = croppedImage ;
        setState(() {});
    }
  }