92 lines
2.8 KiB
Dart
92 lines
2.8 KiB
Dart
|
import 'dart:io';
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:get/get.dart';
|
||
|
import 'package:image_picker/image_picker.dart';
|
||
|
import 'package:sk_base_mobile/store/auth.store.dart';
|
||
|
import 'package:sk_base_mobile/widgets/fade_in_cache_image.dart';
|
||
|
|
||
|
import '../util/util.dart';
|
||
|
|
||
|
class MyAvatarWidget extends StatelessWidget {
|
||
|
final _controller = Get.put<MyAvatarController>(MyAvatarController());
|
||
|
MyAvatarWidget({super.key});
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Stack(
|
||
|
children: [
|
||
|
Obx(() => _buildAvatar()),
|
||
|
Positioned(
|
||
|
bottom: 0,
|
||
|
right: 0,
|
||
|
child: InkWell(
|
||
|
onTap: () {
|
||
|
_controller.photoPicker();
|
||
|
},
|
||
|
child: Image(
|
||
|
height: ScreenAdaper.width(40),
|
||
|
image: const AssetImage('assets/images/company_logo.png'),
|
||
|
)))
|
||
|
],
|
||
|
);
|
||
|
}
|
||
|
|
||
|
dynamic _buildAvatar() {
|
||
|
return _controller.uploadImgFilePath.value.isNotEmpty
|
||
|
? FileImage(File(_controller.uploadImgFilePath.value))
|
||
|
: ClipRRect(
|
||
|
borderRadius: BorderRadius.circular(ScreenAdaper.sp(120)),
|
||
|
child: FadeInCacheImage(
|
||
|
height: ScreenAdaper.height(120),
|
||
|
width: ScreenAdaper.height(120),
|
||
|
url: MediaUtil.getMediaUrl(AuthStore.to.userInfo.value.avatar),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
// Widget getShowImg() {
|
||
|
// // 1.选择的内存照片
|
||
|
// if (uploadImg != null) {
|
||
|
// return Image(fit: BoxFit.cover, image: FileImage(uploadImg!));
|
||
|
// }
|
||
|
// // 2.显示回显的照片url地址
|
||
|
// if (createDto['content'] != null) {
|
||
|
// return FadeInCacheImage(
|
||
|
// url: createDto['content'],
|
||
|
// height: 100,
|
||
|
// width: 100,
|
||
|
// );
|
||
|
// }
|
||
|
|
||
|
// // 3.添加图
|
||
|
// return Image(
|
||
|
// height: 100,
|
||
|
// width: 100,
|
||
|
// fit: BoxFit.cover,
|
||
|
// image: AssetImage('assets/images/common/icon_camera.png'));
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
class MyAvatarController extends GetxController {
|
||
|
final uploadImgFilePath = ''.obs;
|
||
|
Future<void> photoPicker() async {
|
||
|
await MediaUtil().showPicker(callback: (String? pickedFile) async {
|
||
|
await LoadingUtil.to.show(status: 'Uploading...');
|
||
|
try {
|
||
|
// UploadResultModel? res = await MediaUtil().uploadImg(File(pickedFile.path));
|
||
|
// if (res.pa.isNotEmpty) {
|
||
|
// final res = await Api.updateAvatar(filename);
|
||
|
// if (res.data != null) {
|
||
|
// SnackBarUtil().success('Update avatar successfully');
|
||
|
// }
|
||
|
// }
|
||
|
// uploadImgFilePath(pickedFile.path);
|
||
|
// await RouterUtil.back();
|
||
|
} catch (e) {
|
||
|
SnackBarUtil().error('Update avatar failed.');
|
||
|
} finally {
|
||
|
await LoadingUtil.to.dismiss();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|