prompt_box.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'package:fis_i18n/i18n.dart';
  2. import 'package:fis_ui/index.dart';
  3. import 'package:fis_ui/utils/sizer/sizer.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_easyloading/flutter_easyloading.dart';
  6. import 'package:get/get.dart';
  7. ///弹出框位置
  8. enum PromptBoxSnackbarPosition {
  9. //上部弹出
  10. Top,
  11. //底部弹出
  12. Bottom,
  13. }
  14. ///弹出框
  15. class PromptBox {
  16. PromptBox._();
  17. static void init() {
  18. EasyLoading.instance
  19. ..displayDuration = const Duration(milliseconds: 2000)
  20. ..indicatorType = EasyLoadingIndicatorType.ring
  21. ..loadingStyle = EasyLoadingStyle.custom
  22. ..indicatorSize = 35.0
  23. ..lineWidth = 2
  24. ..radius = 10.0
  25. ..progressColor = Colors.white
  26. ..backgroundColor = Colors.black.withOpacity(0.7)
  27. ..indicatorColor = Colors.white
  28. ..textColor = Colors.white
  29. ..fontSize = 16
  30. ..maskColor = Colors.black.withOpacity(0.6)
  31. ..successWidget = FIcon(Icons.done, color: Colors.green, size: 35)
  32. ..errorWidget = FIcon(Icons.close, color: Colors.red, size: 35)
  33. ..userInteractions = true
  34. ..dismissOnTap = false;
  35. }
  36. static bool _enable = true;
  37. static void $SetEnable(bool val) {
  38. _enable = val;
  39. }
  40. /// 等待提示
  41. ///
  42. /// [text] 自定义提示文案
  43. static void loading([String? text]) {
  44. if (!_enable) return;
  45. _setLoadingStyle();
  46. EasyLoading.show(
  47. indicator: LoadingWidget(text: text ?? '${i18nBook.common.loading.t}...'),
  48. maskType: EasyLoadingMaskType.custom,
  49. status: "",
  50. );
  51. }
  52. /// 轻提示
  53. ///
  54. /// [text] 提示内容
  55. static void toast(String text) {
  56. if (!_enable) return;
  57. _resetStyle();
  58. EasyLoading.showToast(text);
  59. }
  60. /// 成功提示
  61. ///
  62. /// [text] 自定义提示文案
  63. static void success(String text) {
  64. if (!_enable) return;
  65. _resetStyle();
  66. EasyLoading.showSuccess(text);
  67. }
  68. /// 错误提示
  69. ///
  70. /// [text] 自定义提示文案
  71. static void error(String text) {
  72. if (!_enable) return;
  73. _resetStyle();
  74. EasyLoading.showError(text);
  75. }
  76. /// 停止提示
  77. static void dismiss() {
  78. EasyLoading.instance.userInteractions = true;
  79. EasyLoading.dismiss();
  80. }
  81. /// 提示框(带标题)
  82. ///
  83. /// [message] 提示内容
  84. ///
  85. /// [title] 标题
  86. ///
  87. /// [position] 弹出位置
  88. static void snackbar(
  89. String message, {
  90. String? title,
  91. Color? textColor = Colors.black,
  92. PromptBoxSnackbarPosition position = PromptBoxSnackbarPosition.Top,
  93. }) {
  94. final isTop = PromptBoxSnackbarPosition.Top == position;
  95. Get.snackbar(
  96. title ?? i18nBook.common.tip.t, // TODO:
  97. message,
  98. colorText: textColor,
  99. maxWidth: kIsMobile ? Sizer.ins.size.width - 40 : 400,
  100. snackPosition: isTop ? SnackPosition.TOP : SnackPosition.BOTTOM,
  101. margin: isTop
  102. ? const EdgeInsets.only(top: 36)
  103. : const EdgeInsets.only(bottom: 48),
  104. );
  105. }
  106. static void _setLoadingStyle() {
  107. EasyLoading.instance
  108. ..userInteractions = false
  109. ..backgroundColor = Colors.transparent
  110. ..maskColor = Colors.black.withOpacity(0.08)
  111. ..boxShadow = <BoxShadow>[];
  112. }
  113. static void _resetStyle() {
  114. EasyLoading.instance
  115. ..backgroundColor = Colors.black.withOpacity(0.7)
  116. ..maskColor = Colors.black.withOpacity(0.6)
  117. ..boxShadow = null;
  118. }
  119. }