feat: loadingUtil 封装

This commit is contained in:
louis 2024-03-19 10:02:20 +08:00
parent 916272bf3b
commit 142c8b5f06
6 changed files with 170 additions and 150 deletions

View File

@ -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>(() => LoadingUtil().init());
///
await Get.putAsync<StorageService>(() => StorageService().init());

View File

@ -39,113 +39,113 @@ class _MinePageState extends State<MinePage>
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(

View File

@ -47,7 +47,7 @@ class AuthStore extends GetxController {
}
Future<void> 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<void> 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<void> 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<void> saveUserInfo(Map<String, dynamic> 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();
}
}

View File

@ -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<void> show({String? status}) async {
return showLoading(status: status ?? 'Loading...');
OverlayEntry? _loadingOverlay;
Future<LoadingUtil> init() async {
return this;
}
static Future<void> dismiss() async {
Future<void> show({String? status}) async {
return showLoading(status: status);
}
Future<void> 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(
child: Stack(
children: <Widget>[
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)))
],
),
),
onTap: () {
// 退
// Navigator.of(context).pop();
},
),
);
)
],
));
},
);
Overlay.of(Get.overlayContext!).insert(_loadingOverlay!);
}
static hideLoading() {
Navigator.of(Get.context!).pop();
hideLoading() {
_loadingOverlay?.remove();
_loadingOverlay = null;
}
}

View File

@ -81,7 +81,7 @@ class MyAvatarController extends GetxController {
final uploadImgFilePath = ''.obs;
Future<void> 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();
}
});
}