头像剪裁

This commit is contained in:
2022-10-31 17:28:28 +08:00
parent 4a15c7fa88
commit afc58d10a3
6 changed files with 162 additions and 105 deletions

View File

@@ -1,12 +1,9 @@
import 'package:chat/controllers/user_controller.dart';
import 'package:chat/routes/user_routes.dart';
import 'package:chat/services/auth_service.dart';
import 'package:chat/views/user/widgets/link_action_item.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
class UserInfoPage extends StatefulWidget {
const UserInfoPage({Key? key}) : super(key: key);
@@ -24,79 +21,36 @@ class _UserInfoPageState extends State<UserInfoPage> {
),
body: GetX<AuthService>(
builder: (_) {
return SafeArea(
child: Column(
children: [
LinkActionItem(
title: '头像',
onTap: () async {
final result = await AssetPicker.pickAssets(
Get.context!,
pickerConfig: const AssetPickerConfig(
maxAssets: 1,
textDelegate: AssetPickerTextDelegate(),
requestType: RequestType.image,
),
);
if (result == null) {
return;
}
_cropImage((await result.first.file)!.path);
},
isLink: true,
trailing: CustomAvatar(
_.userInfo.value.avatar,
size: 52,
),
return Column(
children: [
LinkActionItem(
title: '头像',
onTap: () async {
Get.toNamed(UserRoutes.infoAvatar);
},
isLink: true,
trailing: CustomAvatar(
_.userInfo.value.avatar,
size: 52,
),
LinkActionItem(
title: '昵称',
trailing: Text(_.userInfo.value.nickname!),
isLink: true,
onTap: () {
Get.toNamed(
UserRoutes.infoNickname,
arguments: {
'nickname': _.userInfo.value.nickname,
},
);
},
),
Expanded(child: Container()),
],
),
),
LinkActionItem(
title: '昵称',
trailing: Text(_.userInfo.value.nickname!),
isLink: true,
onTap: () {
Get.toNamed(
UserRoutes.infoNickname,
arguments: {
'nickname': _.userInfo.value.nickname,
},
);
},
),
],
);
},
),
);
}
Future<void> _cropImage(String imagePath) async {
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: imagePath,
maxHeight: 128,
maxWidth: 128,
compressQuality: 100,
aspectRatio: const CropAspectRatio(
ratioX: 1,
ratioY: 1,
),
compressFormat: ImageCompressFormat.jpg,
uiSettings: [
IOSUiSettings(
title: '头像剪裁',
doneButtonTitle: '完成',
cancelButtonTitle: '取消',
),
AndroidUiSettings(
toolbarTitle: '头像剪裁',
),
],
);
if (croppedFile != null) {
UserController.to.uploadAvatar(croppedFile.path);
}
}
}