Home > Mobile >  Flutter image_cropper plugin error: "You need to use a Theme.AppCompat > theme (or descendan
Flutter image_cropper plugin error: "You need to use a Theme.AppCompat > theme (or descendan

Time:05-21

I'm using Flutter's image_cropper plugin & it's throwing an error about AppCompat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.pottertour.meloan/com.yalantis.ucrop.UCropActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

What happens is: I launch the camera from my profile edit screen, after the user takes a photo, it should then launch the crop activity, but it throws that error and crashes my entire app.

This has started to happen with the latest image_cropper plugin upgrade, 2.0.3.

  Future getImage(ImageSource _imageSource) async {
    final pickedFile =
        await imagePicker.pickImage(source: _imageSource, imageQuality: 85);
    if(pickedFile == null) return;

    print(LOG   "getImage file from camera path: ${pickedFile.path}");
    _cropImage(pickedFile.path);

    setState(() {
      _image = File(pickedFile.path);
    });
    imageChanged = true;
  }

  Future<Null> _cropImage(String imageFilePath) async {
    final croppedFile = await ImageCropper().cropImage(
        compressQuality: 80,
        sourcePath: imageFilePath,
        maxWidth: 428,
        maxHeight: 428,
        aspectRatioPresets: Platform.isAndroid
            ? [
                CropAspectRatioPreset.square,
              ]
            : [
                CropAspectRatioPreset.square,
              ],
        uiSettings: [
          AndroidUiSettings(
              toolbarTitle: 'Cropper',
              toolbarColor: Colors.deepOrange,
              toolbarWidgetColor: Colors.white,
              initAspectRatio: CropAspectRatioPreset.original,
              lockAspectRatio: true),
          IOSUiSettings(
            title: 'Cropper',
          )
        ]);
    if (croppedFile != null) {
      setState(() {
        _image = File(croppedFile.path);
      });
    }
  }

I tweaked the cropper code, to the revised format suggested in the plugin's readme but to no avail. I wonder if the screen needs to rebuild after the camera completes but because the camera & _cropImage methods are async, it doesn't in time, so Flutter doesn't have appCompat inherited from the root widget from main.dart.

CodePudding user response:

To use the plugin add this to AndroidManifest.xml. Pay attention especially to the last line.

<activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
  • Related