prompt_box.dart 3.8 KB

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