123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'dart:math';
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.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 formatNowGreetingText() {
- final now = DateTime.now();
- final hour = now.hour;
- String greeting;
- if (hour < 11) {
- greeting = '早上好';
- } else if (hour < 13) {
- greeting = '中午好';
- } else if (hour < 18) {
- greeting = '下午好';
- } else if (hour < 24) {
- greeting = '晚上好';
- } else {
- greeting = '早上好';
- }
- return greeting;
- }
- }
|