prompt_box.dart 3.8 KB

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