ix_snackbar.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../constants/assets.dart';
  4. class IXSnackBar {
  5. static String? _lastMessage;
  6. static DateTime? _lastShowTime;
  7. static bool _shouldShowSnackbar(String message) {
  8. final now = DateTime.now();
  9. if (message == _lastMessage &&
  10. _lastShowTime != null &&
  11. now.difference(_lastShowTime!).inSeconds < 3) {
  12. return false;
  13. }
  14. _lastMessage = message;
  15. _lastShowTime = now;
  16. return true;
  17. }
  18. static showIXSnackBar(
  19. {required String title, required String message, Duration? duration}) {
  20. if (!_shouldShowSnackbar(message)) return;
  21. // Get.snackbar(
  22. // title,
  23. // message,
  24. // duration: duration ?? const Duration(seconds: 3),
  25. // margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
  26. // colorText: Palette.white,
  27. // backgroundColor: Palette.snackbarBgColor,
  28. // borderColor: Palette.snackbarBorderColor,
  29. // borderWidth: 1,
  30. // borderRadius: 16,
  31. // icon: Image.asset(
  32. // Assets.pwdSuccess,
  33. // width: 20,
  34. // height: 20,
  35. // ),
  36. // );
  37. Get.rawSnackbar(
  38. message: message,
  39. backgroundColor: Colors.transparent, // 先让背景透明
  40. snackStyle: SnackStyle.floating, // 悬浮样式
  41. duration: duration ?? const Duration(seconds: 3),
  42. margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
  43. snackPosition: SnackPosition.top,
  44. // backgroundColor: Palette.snackbarBgColor,
  45. barBlur: 7.0,
  46. // borderColor: ,
  47. borderWidth: 1,
  48. borderRadius: 16,
  49. icon: Image.asset(
  50. Assets.success,
  51. width: 20,
  52. height: 20,
  53. ),
  54. );
  55. }
  56. static showIXErrorSnackBar(
  57. {required String title,
  58. required String message,
  59. Color? color,
  60. Duration? duration}) {
  61. if (!_shouldShowSnackbar(message)) return;
  62. // Get.snackbar(
  63. // title,
  64. // message,
  65. // duration: duration ?? const Duration(seconds: 3),
  66. // margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
  67. // colorText: Palette.white,
  68. // backgroundColor: color ?? Palette.snackbarBgColor,
  69. // borderColor: Palette.snackbarBorderColor,
  70. // borderWidth: 1.w,
  71. // borderRadius: 16.r,
  72. // icon: Image.asset(
  73. // Assets.pwdError,
  74. // width: 20.w,
  75. // height: 20.w,
  76. // ),
  77. // );
  78. Get.rawSnackbar(
  79. message: message,
  80. backgroundColor: Colors.transparent, // 先让背景透明
  81. snackStyle: SnackStyle.floating, // 悬浮样式
  82. duration: duration ?? const Duration(seconds: 3),
  83. margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
  84. snackPosition: SnackPosition.top,
  85. // backgroundColor: Palette.snackbarBgColor,
  86. barBlur: 7.0,
  87. // borderColor: Palette.snackbarBorderColor,
  88. borderWidth: 1,
  89. borderRadius: 16,
  90. icon: Image.asset(
  91. Assets.error,
  92. width: 20,
  93. height: 20,
  94. ),
  95. );
  96. }
  97. static showIXToast(
  98. {String? title,
  99. required String message,
  100. Color? color,
  101. Duration? duration}) {
  102. if (!_shouldShowSnackbar(message)) return;
  103. Get.rawSnackbar(
  104. title: title,
  105. duration: duration ?? const Duration(seconds: 3),
  106. snackStyle: SnackStyle.grounded,
  107. backgroundColor: color ?? Colors.green,
  108. onTap: (snack) {
  109. Get.closeAllSnackbars();
  110. },
  111. //overlayBlur: 0.8,
  112. message: message,
  113. );
  114. }
  115. static showIXErrorToast(
  116. {String? title,
  117. required String message,
  118. Color? color,
  119. Duration? duration}) {
  120. if (!_shouldShowSnackbar(message)) return;
  121. Get.rawSnackbar(
  122. title: title,
  123. duration: duration ?? const Duration(seconds: 3),
  124. snackStyle: SnackStyle.grounded,
  125. backgroundColor: color ?? Colors.redAccent,
  126. onTap: (snack) {
  127. Get.closeAllSnackbars();
  128. },
  129. //overlayBlur: 0.8,
  130. message: message,
  131. );
  132. }
  133. }