mobile_skt/lib/widgets/my_avatar.dart

97 lines
3.0 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/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(150),
width: ScreenAdaper.width(150),
child: const SizedBox(),
)),
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 _buildImage() {
return _controller.uploadImgFilePath.value.isNotEmpty
? FileImage(File(_controller.uploadImgFilePath.value))
: NetworkImage(
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 PhotoPickerUtil().showPicker(callback: (XFile 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);
// Get.back();
} catch (e) {
SnackBarUtil().error('Update avatar failed.');
} finally {
await LoadingUtil.to.dismiss();
}
});
}
}