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"
}
}
}
}
本文介绍如何在Flutter中实现头像选择、裁剪及上传功能。通过使用image_picker和image_cropper插件,可以轻松地从相机或相册选择图片,并进行裁剪调整。文章还解决了Androidx兼容性问题,确保应用正常运行。

6486

被折叠的 条评论
为什么被折叠?



