From 142c8b5f06cd4408bf09050bacba9d823972a222 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Tue, 19 Mar 2024 10:02:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20loadingUtil=20=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/global.dart | 4 + lib/screens/mine/mine.dart | 214 +++++++++++++++++----------------- lib/services/dio.service.dart | 2 +- lib/store/auth.store.dart | 23 ++-- lib/util/loading_util.dart | 73 +++++++----- lib/widgets/my_avatar.dart | 4 +- 6 files changed, 170 insertions(+), 150 deletions(-) diff --git a/lib/global.dart b/lib/global.dart index 55fc531..abaca27 100644 --- a/lib/global.dart +++ b/lib/global.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; +import 'package:sk_base_mobile/util/loading_util.dart'; import 'package:timeago/timeago.dart' as timeago; import 'package:sk_base_mobile/constants/cache_key.dart'; import 'package:sk_base_mobile/services/app_info.service.dart'; @@ -26,6 +27,9 @@ class Global { /// 初始化 几天前 的时区国际化 timeago.setLocaleMessages('en', timeago.EnMessages()); + /// 依赖注入Loading工具 + await Get.putAsync(() => LoadingUtil().init()); + /// 依赖注入持久化 await Get.putAsync(() => StorageService().init()); diff --git a/lib/screens/mine/mine.dart b/lib/screens/mine/mine.dart index 27d37ae..573f619 100644 --- a/lib/screens/mine/mine.dart +++ b/lib/screens/mine/mine.dart @@ -39,113 +39,113 @@ class _MinePageState extends State Widget _buildBody() { return Column(children: [ - Container( - height: ScreenAdaper.height(360), - width: ScreenAdaper.screenWidth(), - decoration: const BoxDecoration( - image: DecorationImage( - fit: BoxFit.cover, - image: AssetImage('assets/images/mine_bg.png'))), - child: Padding( - padding: EdgeInsets.symmetric( - horizontal: ScreenAdaper.width(20), - vertical: ScreenAdaper.height(20)), - child: Column(children: [ - SizedBox( - height: ScreenAdaper.height(30), - ), - Row( - children: [ - const Spacer(), - Container( - height: ScreenAdaper.width(40), - width: ScreenAdaper.width(40), - padding: EdgeInsets.all(ScreenAdaper.width(2)), - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(ScreenAdaper.sp(40)), - color: const Color(0xFF000000).withOpacity(0.3)), - child: const SizedBox(), - ) - ], - ), - SizedBox( - height: ScreenAdaper.height(120), - ), - Row( - children: [ - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Row( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Obx(() => Text( - AuthStore.to.userInfo.value.nickname ?? '', - style: TextStyle( - fontSize: ScreenAdaper.sp(20), - fontWeight: FontWeight.w400), - )), - SizedBox( - width: ScreenAdaper.width(3), - ), - InkWell( - onTap: () { - Get.toNamed(RouteConfig.userinfo); - }, - child: Image( - image: - const AssetImage('assets/images/edit_icon.png'), - height: ScreenAdaper.width(20), - ), - ) - ], - ), - SizedBox( - height: ScreenAdaper.height(15), - ), - Text(StorageService.to.getString(CacheKeys.deviceUUID) ?? - ''), - Row( - children: [ - SizedBox( - width: ScreenAdaper.width(210), - child: Text( - 'ID: ${AuthStore.to.userInfo.value.id}', - overflow: TextOverflow.ellipsis, - maxLines: 2, - style: TextStyle( - fontSize: ScreenAdaper.sp(16), - color: Colors.grey[500]), - ), - ), - InkWell( - onTap: () {}, - child: Container( - alignment: Alignment.topCenter, - width: ScreenAdaper.width(50), - height: ScreenAdaper.height(30), - padding: EdgeInsets.symmetric( - horizontal: ScreenAdaper.width(5)), - decoration: BoxDecoration( - color: const Color(0xFF454545), - borderRadius: BorderRadius.circular(5)), - child: Text('copy', - style: TextStyle( - color: AppTheme.white, - fontSize: ScreenAdaper.sp(16)))), - ) - ], - ) - ], - )), - MyAvatarWidget() - ], - ) - ]), - ), - ), + // Container( + // height: ScreenAdaper.height(360), + // width: ScreenAdaper.screenWidth(), + // decoration: const BoxDecoration( + // image: DecorationImage( + // fit: BoxFit.cover, + // image: AssetImage('assets/images/mine_bg.png'))), + // child: Padding( + // padding: EdgeInsets.symmetric( + // horizontal: ScreenAdaper.width(20), + // vertical: ScreenAdaper.height(20)), + // child: Column(children: [ + // SizedBox( + // height: ScreenAdaper.height(30), + // ), + // Row( + // children: [ + // const Spacer(), + // Container( + // height: ScreenAdaper.width(40), + // width: ScreenAdaper.width(40), + // padding: EdgeInsets.all(ScreenAdaper.width(2)), + // decoration: BoxDecoration( + // borderRadius: BorderRadius.circular(ScreenAdaper.sp(40)), + // color: const Color(0xFF000000).withOpacity(0.3)), + // child: const SizedBox(), + // ) + // ], + // ), + // SizedBox( + // height: ScreenAdaper.height(120), + // ), + // Row( + // children: [ + // Expanded( + // child: Column( + // crossAxisAlignment: CrossAxisAlignment.start, + // mainAxisAlignment: MainAxisAlignment.center, + // children: [ + // Row( + // crossAxisAlignment: CrossAxisAlignment.center, + // children: [ + // Obx(() => Text( + // AuthStore.to.userInfo.value.nickname ?? '', + // style: TextStyle( + // fontSize: ScreenAdaper.sp(20), + // fontWeight: FontWeight.w400), + // )), + // SizedBox( + // width: ScreenAdaper.width(3), + // ), + // InkWell( + // onTap: () { + // Get.toNamed(RouteConfig.userinfo); + // }, + // child: Image( + // image: + // const AssetImage('assets/images/edit_icon.png'), + // height: ScreenAdaper.width(20), + // ), + // ) + // ], + // ), + // SizedBox( + // height: ScreenAdaper.height(15), + // ), + // Text(StorageService.to.getString(CacheKeys.deviceUUID) ?? + // ''), + // Row( + // children: [ + // SizedBox( + // width: ScreenAdaper.width(210), + // child: Text( + // 'ID: ${AuthStore.to.userInfo.value.id}', + // overflow: TextOverflow.ellipsis, + // maxLines: 2, + // style: TextStyle( + // fontSize: ScreenAdaper.sp(16), + // color: Colors.grey[500]), + // ), + // ), + // InkWell( + // onTap: () {}, + // child: Container( + // alignment: Alignment.topCenter, + // width: ScreenAdaper.width(50), + // height: ScreenAdaper.height(30), + // padding: EdgeInsets.symmetric( + // horizontal: ScreenAdaper.width(5)), + // decoration: BoxDecoration( + // color: const Color(0xFF454545), + // borderRadius: BorderRadius.circular(5)), + // child: Text('copy', + // style: TextStyle( + // color: AppTheme.white, + // fontSize: ScreenAdaper.sp(16)))), + // ) + // ], + // ) + // ], + // )), + // MyAvatarWidget() + // ], + // ) + // ]), + // ), + // ), Expanded(child: MineSettingsPage()) // Expanded( // child: DefaultTabController( diff --git a/lib/services/dio.service.dart b/lib/services/dio.service.dart index 7b648c0..7ffb7ac 100644 --- a/lib/services/dio.service.dart +++ b/lib/services/dio.service.dart @@ -108,7 +108,7 @@ class DioService extends Get.GetxService { String? token = StorageService.to.getString(CacheKeys.token, isWithUser: false); if (options.headers['Authorization'] == null && token != null) { - options.headers["Authorization"] = "Bearer$token"; + options.headers["Authorization"] = "Bearer $token"; } options.headers['device-id'] = StorageService.to.getString( CacheKeys.deviceUUID, diff --git a/lib/store/auth.store.dart b/lib/store/auth.store.dart index dcb0bd7..ce32540 100644 --- a/lib/store/auth.store.dart +++ b/lib/store/auth.store.dart @@ -47,7 +47,7 @@ class AuthStore extends GetxController { } Future deleteAccount() async { - await LoadingUtil.show(status: 'Deleting...'); + await LoadingUtil.to.show(status: 'Deleting...'); try { final response = await Api.deleteAccount(); if (response.data != null) { @@ -57,24 +57,25 @@ class AuthStore extends GetxController { } catch (e) { await SnackBarUtil().error('Delete account faield. Please try again.'); } finally { - await LoadingUtil.dismiss(); + await LoadingUtil.to.dismiss(); ; } } Future logout({bool force = false}) async { - LoadingUtil.show(status: 'Logout...'); + LoadingUtil.to.show(status: 'Logout...'); await StorageService.to.remove(CacheKeys.token, isWithUser: false); await StorageService.to.remove(CacheKeys.userInfo, isWithUser: false); try { // final response = await Api.logout(); // if (response.data != null) { LoggerUtil().info('[Store-Auth] Logout succeed.'); - if (Get.context != null) Get.offAllNamed(RouteConfig.login); + Get.offAllNamed(RouteConfig.login); // } } catch (e) { + print(e); } finally { - LoadingUtil.dismiss(); + LoadingUtil.to.dismiss(); } } @@ -88,7 +89,7 @@ class AuthStore extends GetxController { // return; // } - LoadingUtil.show(status: 'Login...'); + LoadingUtil.to.show(); TapToDismissKeyboard.dismissOf(context: Get.context!); try { await Future.delayed(const Duration(seconds: 1)); @@ -107,7 +108,7 @@ class AuthStore extends GetxController { } catch (e) { await SnackBarUtil().error('账号密码错误'); } finally { - LoadingUtil.dismiss(); + LoadingUtil.to.dismiss(); } } @@ -118,7 +119,7 @@ class AuthStore extends GetxController { } Future getUserInfo() async { - await LoadingUtil.show(status: 'Loading...'); + await LoadingUtil.to.show(); try { final response = await Api.getUserInfo(); if (response.data != null) { @@ -128,12 +129,12 @@ class AuthStore extends GetxController { } catch (e) { SnackBarUtil().error('$e'); } finally { - LoadingUtil.dismiss(); + LoadingUtil.to.dismiss(); } } Future saveUserInfo(Map data) async { - LoadingUtil.show(status: 'Saving...'); + LoadingUtil.to.show(status: 'Saving...'); try { final res = await Api.saveUserInfo(data); if (res.data != null) { @@ -143,7 +144,7 @@ class AuthStore extends GetxController { } catch (e) { SnackBarUtil().error('$e'); } finally { - LoadingUtil.dismiss(); + LoadingUtil.to.dismiss(); } } diff --git a/lib/util/loading_util.dart b/lib/util/loading_util.dart index 13b4595..08ca671 100644 --- a/lib/util/loading_util.dart +++ b/lib/util/loading_util.dart @@ -4,46 +4,61 @@ import 'package:loading_animation_widget/loading_animation_widget.dart'; import 'package:sk_base_mobile/app_theme.dart'; import 'package:sk_base_mobile/util/screen_adaper_util.dart'; -class LoadingUtil { - LoadingUtil(); +class LoadingUtil extends GetxController { + static LoadingUtil get to => Get.find(); - static Future show({String? status}) async { - return showLoading(status: status ?? 'Loading...'); + OverlayEntry? _loadingOverlay; + + Future init() async { + return this; } - static Future dismiss() async { + Future show({String? status}) async { + return showLoading(status: status); + } + + Future dismiss() async { return hideLoading(); } - static showLoading({String? status}) async { - await showDialog( - context: Get.context!, - barrierColor: AppTheme.barrierColor, - barrierDismissible: false, + showLoading({String? status}) async { + hideLoading(); + + _loadingOverlay = OverlayEntry( builder: (BuildContext context) { return WillPopScope( - onWillPop: () async => false, - child: GestureDetector( - child: Container( - color: Colors.black54, - child: Center( - child: LoadingAnimationWidget.fourRotatingDots( - color: AppTheme.primaryColor, - size: ScreenAdaper.sp(50), - ), - ), - ), - onTap: () { - // 点击是否退出模态框 - // Navigator.of(context).pop(); - }, - ), - ); + onWillPop: () async => false, + child: Stack( + children: [ + ModalBarrier(dismissible: false, color: AppTheme.barrierColor), + Center( + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + LoadingAnimationWidget.fourRotatingDots( + color: AppTheme.primaryColor, + size: ScreenAdaper.sp(50), + ), + if (status != null) + SizedBox(width: ScreenAdaper.width(20)), + if (status != null) + Text(status, + style: TextStyle( + color: AppTheme.primaryColor, + fontSize: ScreenAdaper.sp(20))) + ], + ), + ) + ], + )); }, ); + + Overlay.of(Get.overlayContext!).insert(_loadingOverlay!); } - static hideLoading() { - Navigator.of(Get.context!).pop(); + hideLoading() { + _loadingOverlay?.remove(); + _loadingOverlay = null; } } diff --git a/lib/widgets/my_avatar.dart b/lib/widgets/my_avatar.dart index 9e7107a..f7e8ef8 100644 --- a/lib/widgets/my_avatar.dart +++ b/lib/widgets/my_avatar.dart @@ -81,7 +81,7 @@ class MyAvatarController extends GetxController { final uploadImgFilePath = ''.obs; Future photoPicker() async { await PhotoPickerUtil().showPicker(callback: (XFile pickedFile) async { - await LoadingUtil.show(status: 'Uploading...'); + await LoadingUtil.to.show(status: 'Uploading...'); try { String? filename = await MediaUtil().uploadImg(File(pickedFile.path)); if (filename.isNotEmpty) { @@ -95,7 +95,7 @@ class MyAvatarController extends GetxController { } catch (e) { SnackBarUtil().error('Update avatar failed.'); } finally { - await LoadingUtil.dismiss(); + await LoadingUtil.to.dismiss(); } }); }