util.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:math';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/widgets.dart';
  7. class Utils {
  8. /// 是否字符串为空
  9. static bool isEmpty(String? value) {
  10. if (value == null) {
  11. return true;
  12. }
  13. return value.isEmpty || value == 'null';
  14. }
  15. /// 字符串是否不为空
  16. static bool isNotEmpty(String? value) {
  17. return !isEmpty(value);
  18. }
  19. /// 判断邮箱
  20. static isEmail(dynamic value, {String? country = "sg"}) {
  21. switch (country) {
  22. case "sg":
  23. RegExp regExp = RegExp(r"^\w{1,}(.\w+)*@[A-z0-9]+(\.[A-z]{2,6}){1,2}$");
  24. if (regExp.hasMatch(value.toString())) {
  25. return true;
  26. } else {
  27. return false;
  28. }
  29. default:
  30. return false;
  31. }
  32. }
  33. /// 判断中国的11位手机号码
  34. static bool isChinaPhoneNumber(dynamic value) {
  35. RegExp regExp = RegExp(r"^1\d{10}$"); // 匹配以1开头的11位数字
  36. if (regExp.hasMatch(value.toString())) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. /// 全局的路由key
  43. static final navKey = GlobalKey<NavigatorState>();
  44. /// 全局的组件key
  45. static final widgetKey = GlobalKey<State>();
  46. //页面缓存key
  47. static dynamic pageStorageKey(dynamic keyname) {
  48. return PageStorageKey<dynamic>(keyname);
  49. }
  50. /// 关闭软键盘
  51. static void hideKeyboard(BuildContext context) {
  52. FocusScopeNode currentFocus = FocusScope.of(context);
  53. if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
  54. FocusManager.instance.primaryFocus!.unfocus();
  55. }
  56. }
  57. /// map 转 json 字符串
  58. static String encodeMap(Map<String, dynamic> mapData) {
  59. return json.encode(mapData);
  60. }
  61. /// json 字符串转 map
  62. static Map<String, dynamic> decodeJson(jsonStr) {
  63. return json.decode(jsonStr.toString());
  64. }
  65. /// 判断颜色是否是暗色还是亮色
  66. static num getComputeLuminance(Color color) {
  67. return color.computeLuminance();
  68. }
  69. /// 随机获取指定返回内的数值
  70. static int getRandomRangeInt(int min, int max) {
  71. final Random random = Random();
  72. return min + random.nextInt(max + 1 - min);
  73. }
  74. /// 无参数的防抖函数
  75. static VoidCallback debounce(VoidCallback func, [
  76. Duration delay = const Duration(milliseconds: 400),
  77. ]) {
  78. late Timer timer;
  79. return () {
  80. if (timer.isActive == true) {
  81. timer.cancel();
  82. }
  83. timer = Timer(delay, () {
  84. func.call();
  85. });
  86. };
  87. }
  88. /// 带参数的防抖函数
  89. static Function debounceArg<T>(Function func, [
  90. Duration delay = const Duration(milliseconds: 2000),
  91. ]) {
  92. late Timer timer;
  93. return (T arg) {
  94. if (timer.isActive == true) {
  95. timer.cancel();
  96. }
  97. timer = Timer(delay, () {
  98. func.call(arg);
  99. });
  100. };
  101. }
  102. /// 根据现在的小时数,返回 早上好,中午好,下午好,晚上好 这四个字符串
  103. static String formatNowGreetingText() {
  104. final now = DateTime.now();
  105. final hour = now.hour;
  106. String greeting;
  107. if (hour < 11) {
  108. greeting = '早上好';
  109. } else if (hour < 13) {
  110. greeting = '中午好';
  111. } else if (hour < 18) {
  112. greeting = '下午好';
  113. } else if (hour < 24) {
  114. greeting = '晚上好';
  115. } else {
  116. greeting = '早上好';
  117. }
  118. return greeting;
  119. }
  120. }