123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'dart:math';
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'log_utils.dart';
- class Utils {
- /// 是否字符串为空
- static bool isEmpty(String? value) {
- if (value == null) {
- return true;
- }
- return value.isEmpty || value == 'null';
- }
- /// 字符串是否不为空
- static bool isNotEmpty(String? value) {
- return !isEmpty(value);
- }
- /// 判断邮箱
- static isEmail(dynamic value, {String? country = "sg"}) {
- switch (country) {
- case "sg":
- RegExp regExp = RegExp(r"^\w{1,}(.\w+)*@[A-z0-9]+(\.[A-z]{2,6}){1,2}$");
- if (regExp.hasMatch(value.toString())) {
- return true;
- } else {
- return false;
- }
- default:
- return false;
- }
- }
- /// 判断中国的11位手机号码
- static bool isChinaPhoneNumber(dynamic value) {
- RegExp regExp = RegExp(r"^1\d{10}$"); // 匹配以1开头的11位数字
- if (regExp.hasMatch(value.toString())) {
- return true;
- } else {
- return false;
- }
- }
- /// 全局的路由key
- static final navKey = GlobalKey<NavigatorState>();
- /// 全局的组件key
- static final widgetKey = GlobalKey<State>();
- //页面缓存key
- static dynamic pageStorageKey(dynamic keyname) {
- return PageStorageKey<dynamic>(keyname);
- }
- /// 关闭软键盘
- static void hideKeyboard(BuildContext context) {
- FocusScopeNode currentFocus = FocusScope.of(context);
- if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
- FocusManager.instance.primaryFocus!.unfocus();
- }
- }
- /// map 转 json 字符串
- static String encodeMap(Map<String, dynamic> mapData) {
- return json.encode(mapData);
- }
- /// json 字符串转 map
- static Map<String, dynamic> decodeJson(jsonStr) {
- return json.decode(jsonStr.toString());
- }
- /// 判断颜色是否是暗色还是亮色
- static num getComputeLuminance(Color color) {
- return color.computeLuminance();
- }
- /// 随机获取指定返回内的数值
- static int getRandomRangeInt(int min, int max) {
- final Random random = Random();
- return min + random.nextInt(max + 1 - min);
- }
- /// 无参数的防抖函数
- static VoidCallback debounce(
- VoidCallback func, [
- Duration delay = const Duration(milliseconds: 400),
- ]) {
- late Timer timer;
- return () {
- if (timer.isActive == true) {
- timer.cancel();
- }
- timer = Timer(delay, () {
- func.call();
- });
- };
- }
- /// 带参数的防抖函数
- static Function debounceArg<T>(
- Function func, [
- Duration delay = const Duration(milliseconds: 2000),
- ]) {
- late Timer timer;
- return (T arg) {
- if (timer.isActive == true) {
- timer.cancel();
- }
- timer = Timer(delay, () {
- func.call(arg);
- });
- };
- }
- /// 将时间转化为 多久之前
- static String getTimeAgo(String? dateTimeStr) {
- if (dateTimeStr == null || dateTimeStr.isEmpty) {
- return 'Unknown time'; // 或者返回其他默认值
- }
- DateTime? dateTime;
- try {
- dateTime = DateTime.parse(dateTimeStr);
- } catch (e) {
- return 'Invalid date format'; // 或者返回其他默认值
- }
- if (dateTime == null) {
- return 'Unknown time'; // 或者返回其他默认值
- }
- final now = DateTime.now();
- final difference = now.difference(dateTime);
- if (difference.inHours < 1) {
- return "${difference.inMinutes} minutes ago";
- } else if (difference.inHours < 24) {
- return "${difference.inHours} hours ago";
- } else if (difference.inDays < 7) {
- return "${difference.inDays} days ago";
- } else if (difference.inDays < 30) {
- return "${(difference.inDays / 7).floor()} weeks ago";
- } else if (difference.inDays < 365) {
- return "${(difference.inDays / 30).floor()} months ago";
- } else {
- return "${(difference.inDays / 365).floor()} years ago";
- }
- }
- /// 从 Url 中找到对应的宽高
- static List<double>? extractWidthHeight(String? url) {
- if (isEmpty(url)) return null;
- // 使用正则表达式来查找形如 "宽x高" 的部分
- final RegExp regex = RegExp(r'-(\d+)x(\d+)\.');
- final matches = regex.allMatches(url!);
- if (matches.isNotEmpty) {
- // 获取最后一个匹配项
- final lastMatch = matches.last;
- final width = double.parse(lastMatch.group(1)!);
- final height = double.parse(lastMatch.group(2)!);
- return [width, height]; // 返回宽高列表
- }
- return null; // 未找到宽高
- }
- /// 将字符串的前三位和后两位显示,中间用 * 替代。
- static String maskString(String input) {
- // 检查字符串长度
- if (input.length <= 5) {
- // 如果字符串长度小于等于5,直接返回原字符串
- return input;
- }
- // 获取前三位和后两位
- String start = input.substring(0, 3);
- String end = input.substring(input.length - 2);
- // 计算中间需要的 * 的数量
- int asterisksCount = input.length - 5; // 计算需要的 * 的数量
- String asterisks = '*' * asterisksCount; // 创建 * 字符串
- // 拼接成新的字符串
- return '$start$asterisks$end';
- }
- }
|