common_util.dart 2.8 KB

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