common_util.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // ignore_for_file: non_constant_identifier_names
  2. import 'dart:async';
  3. ///公共帮助类
  4. class CommonUtil {
  5. static const deFaultDurationTime = 1000;
  6. static Timer? timer;
  7. // 邮箱判断
  8. static const String REGEX_EMAIL =
  9. "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}\$";
  10. // 纯数字
  11. static const String DIGIT_REGEX = "[0-9]+";
  12. // 含有数字
  13. static const String CONTAIN_DIGIT_REGEX = ".*[0-9].*";
  14. // 纯字母
  15. static const String LETTER_REGEX = "[a-zA-Z]+";
  16. // 包含字母
  17. static const String SMALL_CONTAIN_LETTER_REGEX = ".*[a-z].*";
  18. // 包含字母
  19. static const String BIG_CONTAIN_LETTER_REGEX = ".*[A-Z].*";
  20. // 包含字母
  21. static const String CONTAIN_LETTER_REGEX = ".*[a-zA-Z].*";
  22. // 纯中文
  23. static const String CHINESE_REGEX = "[\u4e00-\u9fa5]";
  24. // 空格
  25. static const String BLANK_SPACE = "[ ]";
  26. // 仅仅包含字母和数字
  27. static const String LETTER_DIGIT_REGEX = "^[a-z0-9A-Z]+\$";
  28. static const String CHINESE_LETTER_REGEX = "([\u4e00-\u9fa5]+|[a-zA-Z]+)";
  29. static const String CHINESE_LETTER_DIGIT_REGEX =
  30. "^[a-z0-9A-Z\u4e00-\u9fa5]+\$";
  31. /// 节流函数 处理规定在一个单位时间内,只能触发一次函数。如果这个单位时间内触发多次函数,只有一次生效
  32. static const String deFaultThrottleId = 'DeFaultThrottleId';
  33. static Map<String, int> startTimeMap = {deFaultThrottleId: 0};
  34. /// 防抖函数 在函数需要频繁触发的情况时,只有足够空闲时间,才执行一次
  35. ///
  36. /// [doSomething] 处理方法
  37. ///
  38. /// [durationTime] 禁止点击的时间
  39. static debounce(Function doSomething, {durationTime = deFaultDurationTime}) {
  40. timer?.cancel();
  41. timer = Timer(Duration(milliseconds: durationTime), () {
  42. doSomething.call();
  43. timer = null;
  44. });
  45. }
  46. // 含有数字
  47. static bool hasDigit(String input) {
  48. if (input.isEmpty) return false;
  49. return RegExp(CONTAIN_DIGIT_REGEX).hasMatch(input);
  50. }
  51. // 是否包含中文
  52. static bool isChinese(String input) {
  53. if (input.isEmpty) return false;
  54. return RegExp(CHINESE_REGEX).hasMatch(input);
  55. }
  56. // 纯数字
  57. static bool isOnly(String input) {
  58. if (input.isEmpty) return false;
  59. return RegExp(DIGIT_REGEX).hasMatch(input);
  60. }
  61. static throttle(Function doSomething,
  62. {String throttleId = deFaultThrottleId,
  63. durationTime = deFaultDurationTime,
  64. Function? continueClick}) {
  65. int currentTime = DateTime.now().millisecondsSinceEpoch;
  66. if (currentTime - (startTimeMap[throttleId] ?? 0) > durationTime) {
  67. doSomething.call();
  68. startTimeMap[throttleId] = DateTime.now().millisecondsSinceEpoch;
  69. } else {
  70. continueClick?.call();
  71. }
  72. }
  73. }