ix_snackbar.dart 4.2 KB

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