flutter 选择图片并裁剪上传头像功能
1、引用 选择图片库
image_picker: ^0.6.3+1
//选择照片
Utils.showChoose(context, "选择照片来源", photoArray, (int position) async { var image = await ImagePicker.pickImage(source: position==0 ? ImageSource.camera : ImageSource.gallery);
2、引用图片裁剪库
image_cropper: ^1.2.1
//裁剪照片
File croppedFile = await ImageCropper.cropImage( sourcePath: image.path, maxWidth: 300, maxHeight: 300, aspectRatioPresets: [ CropAspectRatioPreset.square ], androidUiSettings: AndroidUiSettings( toolbarTitle: '裁剪', toolbarColor: Colors.white, toolbarWidgetColor: MyColors.blue, initAspectRatio: CropAspectRatioPreset.square, lockAspectRatio: true), iosUiSettings: IOSUiSettings( minimumAspectRatio: 1.0, ) );
获取到裁剪后的照片后调用上传接口即可。
编译运行报错 程序包androidx.lifecycle不存在。
这个问题是Androidx引用库兼容问题。找到<project_root>/android/app/build.gradle 在最后增加如下代码即可
configurations.all { resolutionStrategy { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'androidx.core') { details.useVersion "1.0.2" } if (details.requested.group == 'androidx.lifecycle') { details.useVersion "2.0.0" } if (details.requested.group == 'androidx.versionedparcelable') { details.useVersion "1.0.0" } if (details.requested.group == 'androidx.fragment') { details.useVersion "1.0.0" } if (details.requested.group == 'androidx.appcompat') { details.useVersion "1.0.1" } } } }