import 'dart:io'; import 'package:cached_network_image/cached_network_image.dart'; 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 'package:sk_base_mobile/widgets/fade_in_cache_image.dart'; import '../util/util.dart'; class MyAvatarWidget extends StatelessWidget { final _controller = Get.put(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 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(); } }); } }