2024-03-18 13:23:58 +08:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:sk_base_mobile/app_theme.dart';
|
2024-03-19 08:59:08 +08:00
|
|
|
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
|
2024-03-18 13:23:58 +08:00
|
|
|
|
|
|
|
// SnackBar 工具
|
|
|
|
class SnackBarUtil {
|
|
|
|
checkIsSnackBarInit() {
|
|
|
|
return Get.overlayContext != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> error(String? title, {dynamic error}) async {
|
|
|
|
if (checkIsSnackBarInit()) {
|
|
|
|
if (error is Exception) {
|
|
|
|
title = error.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error is DioException) {
|
|
|
|
title = error.message;
|
|
|
|
if (error.response?.data is Map) {
|
|
|
|
title = error.response?.data['message'] ?? title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Get.isSnackbarOpen) {
|
|
|
|
await Get.closeCurrentSnackbar();
|
|
|
|
}
|
|
|
|
Get.rawSnackbar(
|
|
|
|
snackPosition: SnackPosition.TOP,
|
|
|
|
backgroundColor: AppTheme.snackbarErrorBackgroudColor,
|
2024-03-19 08:59:08 +08:00
|
|
|
borderRadius: ScreenAdaper.sp(15),
|
|
|
|
messageText: Text(
|
|
|
|
'$title',
|
|
|
|
style: TextStyle(
|
2024-03-28 17:18:46 +08:00
|
|
|
fontSize: ScreenAdaper.height(25), color: AppTheme.nearlyWhite),
|
2024-03-19 08:59:08 +08:00
|
|
|
),
|
|
|
|
margin: EdgeInsets.symmetric(
|
|
|
|
horizontal: ScreenAdaper.width(20), vertical: 0),
|
2024-03-18 13:23:58 +08:00
|
|
|
overlayColor: Colors.white,
|
2024-03-19 08:59:08 +08:00
|
|
|
duration: const Duration(seconds: 3),
|
2024-03-18 13:23:58 +08:00
|
|
|
forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
|
|
|
|
reverseAnimationCurve: Curves.linearToEaseOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> success(String title, {String? message}) async {
|
|
|
|
if (checkIsSnackBarInit()) {
|
|
|
|
if (Get.isSnackbarOpen) {
|
|
|
|
await Get.closeCurrentSnackbar();
|
|
|
|
}
|
|
|
|
Get.rawSnackbar(
|
|
|
|
message: title,
|
|
|
|
snackPosition: SnackPosition.TOP,
|
|
|
|
backgroundColor: AppTheme.snackbarSuccessBackgroudColor,
|
|
|
|
borderRadius: 15,
|
2024-03-31 17:13:29 +08:00
|
|
|
margin: EdgeInsets.symmetric(
|
|
|
|
horizontal: ScreenAdaper.height(15), vertical: 0),
|
2024-03-18 13:23:58 +08:00
|
|
|
overlayColor: Colors.white,
|
2024-03-31 17:13:29 +08:00
|
|
|
duration: const Duration(seconds: 2),
|
2024-03-18 13:23:58 +08:00
|
|
|
dismissDirection: DismissDirection.horizontal,
|
|
|
|
forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
|
|
|
|
reverseAnimationCurve: Curves.linearToEaseOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> info(String title, {String? message}) async {
|
|
|
|
if (checkIsSnackBarInit()) {
|
|
|
|
if (Get.isSnackbarOpen) {
|
|
|
|
await Get.closeCurrentSnackbar();
|
|
|
|
}
|
|
|
|
Get.rawSnackbar(
|
|
|
|
message: title,
|
|
|
|
snackPosition: SnackPosition.TOP,
|
|
|
|
borderRadius: 15,
|
2024-03-31 17:13:29 +08:00
|
|
|
margin: EdgeInsets.symmetric(
|
|
|
|
horizontal: ScreenAdaper.height(15), vertical: 0),
|
2024-04-09 08:31:17 +08:00
|
|
|
duration: const Duration(seconds: 2),
|
2024-03-18 13:23:58 +08:00
|
|
|
dismissDirection: DismissDirection.horizontal,
|
|
|
|
forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
|
|
|
|
reverseAnimationCurve: Curves.linearToEaseOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> warning(String title, {String? message}) async {
|
|
|
|
if (checkIsSnackBarInit()) {
|
|
|
|
if (Get.isSnackbarOpen) {
|
|
|
|
await Get.closeCurrentSnackbar();
|
|
|
|
}
|
|
|
|
Get.rawSnackbar(
|
|
|
|
message: title,
|
|
|
|
snackPosition: SnackPosition.TOP,
|
|
|
|
backgroundColor: AppTheme.snackbarWarningBackgroudColor,
|
|
|
|
borderRadius: 15,
|
2024-03-31 17:13:29 +08:00
|
|
|
margin: EdgeInsets.symmetric(
|
|
|
|
horizontal: ScreenAdaper.height(15), vertical: 0),
|
2024-03-18 13:23:58 +08:00
|
|
|
overlayColor: Colors.white,
|
2024-03-31 17:13:29 +08:00
|
|
|
duration: const Duration(seconds: 2),
|
2024-03-18 13:23:58 +08:00
|
|
|
dismissDirection: DismissDirection.horizontal,
|
|
|
|
forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
|
|
|
|
reverseAnimationCurve: Curves.linearToEaseOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|