mobile_skt/lib/widgets/my_avatar.dart

92 lines
2.8 KiB
Dart
Raw Normal View History

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';
2024-04-09 08:31:17 +08:00
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: [
2024-04-09 08:31:17 +08:00
Obx(() => _buildAvatar()),
Positioned(
bottom: 0,
right: 0,
child: InkWell(
onTap: () {
_controller.photoPicker();
},
child: Image(
height: ScreenAdaper.width(40),
2024-03-27 11:06:01 +08:00
image: const AssetImage('assets/images/company_logo.png'),
)))
],
);
}
2024-04-09 08:31:17 +08:00
dynamic _buildAvatar() {
return _controller.uploadImgFilePath.value.isNotEmpty
? FileImage(File(_controller.uploadImgFilePath.value))
2024-04-09 08:31:17 +08:00
: 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 {
2024-03-19 10:02:20 +08:00
await LoadingUtil.to.show(status: 'Uploading...');
try {
2024-03-26 11:35:39 +08:00
// 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);
2024-04-12 09:56:00 +08:00
// await RouterUtil.back();
} catch (e) {
SnackBarUtil().error('Update avatar failed.');
} finally {
2024-03-19 10:02:20 +08:00
await LoadingUtil.to.dismiss();
}
});
}
}