util.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /// 判断是网络 地址
  34. static bool isNetworkUrl(String url) {
  35. return url.startsWith('http://') || url.startsWith('https://');
  36. }
  37. /// 判断中国的11位手机号码
  38. static bool isChinaPhoneNumber(dynamic value) {
  39. RegExp regExp = RegExp(r"^1\d{10}$"); // 匹配以1开头的11位数字
  40. if (regExp.hasMatch(value.toString())) {
  41. return true;
  42. } else {
  43. return false;
  44. }
  45. }
  46. /// 全局的路由key
  47. static final navKey = GlobalKey<NavigatorState>();
  48. /// 全局的组件key
  49. static final widgetKey = GlobalKey<State>();
  50. //页面缓存key
  51. static dynamic pageStorageKey(dynamic keyname) {
  52. return PageStorageKey<dynamic>(keyname);
  53. }
  54. /// 关闭软键盘
  55. static void hideKeyboard(BuildContext context) {
  56. FocusScopeNode currentFocus = FocusScope.of(context);
  57. if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
  58. FocusManager.instance.primaryFocus!.unfocus();
  59. }
  60. }
  61. /// map 转 json 字符串
  62. static String encodeMap(Map<String, dynamic> mapData) {
  63. return json.encode(mapData);
  64. }
  65. /// json 字符串转 map
  66. static Map<String, dynamic> decodeJson(jsonStr) {
  67. return json.decode(jsonStr.toString());
  68. }
  69. /// 判断颜色是否是暗色还是亮色
  70. static num getComputeLuminance(Color color) {
  71. return color.computeLuminance();
  72. }
  73. /// 随机获取指定返回内的数值
  74. static int getRandomRangeInt(int min, int max) {
  75. final Random random = Random();
  76. return min + random.nextInt(max + 1 - min);
  77. }
  78. /// 无参数的防抖函数
  79. static VoidCallback debounce(VoidCallback func, [
  80. Duration delay = const Duration(milliseconds: 400),
  81. ]) {
  82. late Timer timer;
  83. return () {
  84. if (timer.isActive == true) {
  85. timer.cancel();
  86. }
  87. timer = Timer(delay, () {
  88. func.call();
  89. });
  90. };
  91. }
  92. /// 带参数的防抖函数
  93. static Function debounceArg<T>(Function func, [
  94. Duration delay = const Duration(milliseconds: 2000),
  95. ]) {
  96. late Timer timer;
  97. return (T arg) {
  98. if (timer.isActive == true) {
  99. timer.cancel();
  100. }
  101. timer = Timer(delay, () {
  102. func.call(arg);
  103. });
  104. };
  105. }
  106. /// 根据现在的小时数,返回 早上好,中午好,下午好,晚上好 这四个字符串
  107. static String formatNowGreetingText() {
  108. final now = DateTime.now();
  109. final hour = now.hour;
  110. String greeting;
  111. if (hour < 11) {
  112. greeting = '早上好';
  113. } else if (hour < 13) {
  114. greeting = '中午好';
  115. } else if (hour < 18) {
  116. greeting = '下午好';
  117. } else if (hour < 24) {
  118. greeting = '晚上好';
  119. } else {
  120. greeting = '早上好';
  121. }
  122. return greeting;
  123. }
  124. }