common_util.dart 2.9 KB

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