mobile_skt/lib/widgets/my_avatar.dart

103 lines
3.3 KiB
Dart

import 'dart:io';
import 'package:sk_base_mobile/apis/api.dart' as Api;
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/util/media_util.dart';
import 'package:sk_base_mobile/util/photo_picker_util.dart';
import 'package:sk_base_mobile/util/loading_util.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:sk_base_mobile/util/snack_bar.util.dart';
import 'package:sk_base_mobile/app_theme.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(() => Container(
decoration: BoxDecoration(
image:
DecorationImage(fit: BoxFit.cover, image: _buildImage()),
border: Border.all(
color: AppTheme.white, width: ScreenAdaper.sp(2)),
borderRadius: BorderRadius.circular(50)),
height: ScreenAdaper.width(90),
width: ScreenAdaper.width(90),
child: const SizedBox(),
)),
Positioned(
bottom: 0,
right: 0,
child: InkWell(
onTap: () {
_controller.photoPicker();
},
child: Image(
height: ScreenAdaper.width(40),
image: const AssetImage('assets/images/photo_icon.png'),
)))
],
);
}
dynamic _buildImage() {
return _controller.uploadImgFilePath.value.isNotEmpty
? FileImage(File(_controller.uploadImgFilePath.value))
: NetworkImage(
AuthStore.to.userInfo.value.avatarThumbUrl ?? '',
);
}
// 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 PhotoPickerUtil().showPicker(callback: (XFile pickedFile) async {
await LoadingUtil.show(status: 'Uploading...');
try {
String? filename = await MediaUtil().uploadImg(File(pickedFile.path));
if (filename.isNotEmpty) {
final res = await Api.updateAvatar(filename);
if (res.data != null) {
SnackBarUtil().success('Update avatar successfully');
}
}
uploadImgFilePath(pickedFile.path);
Get.back();
} catch (e) {
SnackBarUtil().error('Update avatar failed.');
} finally {
await LoadingUtil.dismiss();
}
});
}
}