import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:intl/intl.dart'; import 'package:path_provider/path_provider.dart'; class AppUtils { ///保留两位小数 static double stringAsFixedDouble2(double value) { String valueStr = value.toString(); if (!valueStr.contains(".")) return value; if (valueStr.indexOf(".") + 3 >= valueStr.length) return value; else return double.tryParse(valueStr.substring(0, valueStr.indexOf(".") + 3))??0; } ///使用16进制色值并返回对应色值的方法 static Color getLighterColor(String hexColor) { // 将16进制颜色值转换为Color对象 Color color = Color(int.parse(hexColor.replaceAll("#", "FF"), radix: 16)); // 计算颜色的RGB值 int red = color.red + 95; int green = color.green + 95; int blue = color.blue + 95; // 确保RGB值不超过255 red = red > 255 ? 255 : red; green = green > 255 ? 255 : green; blue = blue > 255 ? 255 : blue; // 返回较浅的颜色值 return Color.fromARGB(255, red, green, blue); } ///版本号 static bool versionCompare(String localVersion, String serverVersion) { try { var localVersionArr = localVersion.split("."); var serverVersionArr = serverVersion.split("."); if (int.tryParse(localVersionArr[0]) > int.tryParse(serverVersionArr[0])) return false; if (int.tryParse(localVersionArr[1]) > int.tryParse(serverVersionArr[1])) return false; if (int.tryParse(localVersionArr[2]) >= int.tryParse(serverVersionArr[2])) return false; } catch (ex) {} return true; } static String trimEnd(String resString, String char) { while (resString.endsWith(char)) { resString = resString.substring(0, resString.length - 1); } return resString; } ///金额保留整数/后两位 static String calculateDouble(double res) { String resString = res.toStringAsFixed(2); resString = trimEnd(resString, "0"); resString = trimEnd(resString, "."); return resString; } static bool isPhone(mobile) { RegExp exp = RegExp( r'^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$'); return exp.hasMatch(mobile); } static String phoneEncode(String phone) { if (phone == null || phone.length != 11) return phone; return phone.substring(0, 3) + "****" + phone.substring(phone.length - 4); } static double textScale(context) { double textScaleFactor = MediaQuery.of(context).textScaleFactor; // print("textScaleFactor: $textScaleFactor"); return textScaleFactor > 1.15 ? 1.15 : textScaleFactor; } static String getAge(DateTime brt) { int age = 0; DateTime dateTime = DateTime.now(); if (dateTime.isBefore(brt)) { //出生日期晚于当前时间,无法计算 return '出生日期不正確'; } int yearNow = dateTime.year; //当前年份 int monthNow = dateTime.month; //当前月份 int dayOfMonthNow = dateTime.day; //当前日期 int yearBirth = brt.year; int monthBirth = brt.month; int dayOfMonthBirth = brt.day; age = yearNow - yearBirth; //计算整岁数 if (monthNow <= monthBirth) { if (monthNow == monthBirth) { if (dayOfMonthNow < dayOfMonthBirth) age--; //当前日期在生日之前,年龄减一 } else { age--; //当前月份在生日之前,年龄减一 } } return age.toString(); } static String getAgeByString(String birth) { if (birth == null || birth == "") return ""; int age = 0; DateTime brt = DateFormat("yyyy-MM-dd").parse(birth); DateTime dateTime = DateTime.now(); if (dateTime.isBefore(brt)) { //出生日期晚于当前时间,无法计算 return '出生日期不正確'; } int yearNow = dateTime.year; //当前年份 int monthNow = dateTime.month; //当前月份 int dayOfMonthNow = dateTime.day; //当前日期 int yearBirth = brt.year; int monthBirth = brt.month; int dayOfMonthBirth = brt.day; age = yearNow - yearBirth; //计算整岁数 if (monthNow <= monthBirth) { if (monthNow == monthBirth) { if (dayOfMonthNow < dayOfMonthBirth) age--; //当前日期在生日之前,年龄减一 } else { age--; //当前月份在生日之前,年龄减一 } } return age.toString(); } static String getHourMinuteByString(String birth) { if (birth == null || birth == "") return ""; DateTime brt = DateFormat("yyyy-MM-dd HH:mm:ss").parse(birth); return DateFormat("HH:mm").format(brt); } static String getHourMinuteAfter30mByString(String birth) { if (birth == null || birth == "") return ""; DateTime brt = DateFormat("yyyy-MM-dd HH:mm:ss").parse(birth); DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( brt.millisecondsSinceEpoch + 30 * 60 * 1000); return DateFormat("HH:mm").format(dateTime); //分钟 } /// 获取缓存大小 static Future total() async { Directory tempDir = await getTemporaryDirectory(); if (tempDir == null) return 0; int total = await _reduce(tempDir); return total; } /// 清除缓存 static Future clear() async { Directory tempDir = await getTemporaryDirectory(); if (tempDir == null) return 0; await _delete(tempDir); } /// 递归缓存目录,计算缓存大小 static Future _reduce(FileSystemEntity file) async { /// 如果是一个文件,则直接返回文件大小 if (file is File) { int length = await file.length(); return length; } /// 如果是目录,则遍历目录并累计大小 if (file is Directory) { final List children = file.listSync(); int total = 0; if (children != null && children.isNotEmpty) for (final FileSystemEntity child in children) total += await _reduce(child); return total; } return 0; } /// 递归删除缓存目录和文件 static Future _delete(FileSystemEntity file) async { if (file is Directory) { final List children = file.listSync(); for (final FileSystemEntity child in children) { await child.delete(recursive: true); } } else { await file.delete(); } } /// 递归删除缓存目录和文件 static Future deleteDirectory(FileSystemEntity file) async { if (file is Directory) { final List children = file.listSync(); for (final FileSystemEntity child in children) { child.deleteSync(recursive: true); } } } static String dioErrorTypeToString(DioErrorType type) { String errorStr = ""; switch (type) { case DioErrorType.CANCEL: { errorStr = "取消请求"; break; } case DioErrorType.CONNECT_TIMEOUT: { errorStr = "链接超时"; break; } case DioErrorType.RECEIVE_TIMEOUT: { errorStr = "响应超时"; break; } case DioErrorType.SEND_TIMEOUT: { errorStr = "请求超时"; break; } case DioErrorType.RESPONSE: { errorStr = "服务器异常"; break; } case DioErrorType.DEFAULT: default: { errorStr = "请求异常"; break; } } errorStr += ",请检查手机网络是否正常"; return errorStr; } static String textSubStr(String input, int length) { length -= 1; if (length < 0 || input.length < length) return input; return "${input.substring(0, length)}..."; } ///计算时间差/比较时间 static bool compareTime(String time1, String time2) { if (time1 == null || time2 == null) { return false; } var time1List = time1.split(":"); var time2List = time2.split(":"); if (time1List.length != 3 || time2List.length != 3) { return false; } var hour1 = int.parse(time1List[0]); var hour2 = int.parse(time2List[0]); var minute1 = int.parse(time1List[1]); var minute2 = int.parse(time2List[1]); var second1 = int.parse(time1List[2]); var second2 = int.parse(time2List[2]); if (hour1 > hour2) { return false; } else if (hour1 == hour2) { if (minute1 > minute2) { return false; }else if (minute1 == minute2) { if (second1 > second2) { return false; } else { return true; } } else { return true; } } else { return true; } } ///im列表时间显示 static String timeFormatter(DateTime time) { final now = DateTime.now(); final diff = now.difference(time).inHours; final diff2 = now.difference(time).inDays; if (diff < 24) { return DateFormat('HH:mm').format(time); // 24小时内显示为今天发送信息的时间点 } else if (diff < 48) { return '昨天 ${DateFormat('HH:mm').format(time)}'; // 昨天+时间点 } else if (diff < 72 && diff2<7) { return DateFormat('${DateFormat('EEEE').format(time)}').format(time);// 超过72小时显示为星期几 } else if (time.year == now.year) { return DateFormat('MM月dd日').format(time); // 今年内的日期 } else { return DateFormat('yyyy年MM月dd日').format(time); // 其他年份的日期 } } ///聊天框显示时间 static String milliTimeFormatter(DateTime time) { final now = DateTime.now(); final diff = now.difference(time).inHours; if (diff < 24) { return DateFormat('HH:mm').format(time); // 24小时内显示为今天发送信息的时间点 } else if (diff < 48) { return '昨天 ${DateFormat('HH:mm').format(time)}'; // 昨天+时间点 } else if (diff < 72) { return DateFormat('${DateFormat('EEEE').format(time)}').format(time) + " ${DateFormat('HH:mm').format(time)}";// 超过72小时显示为星期几+时间点 } else if (time.year == now.year) { return DateFormat('MM月dd日').format(time) + " ${DateFormat('HH:mm').format(time)}"; // 今年内的日期+时间点 } else { return DateFormat('yyyy年MM月dd日').format(time) + " ${DateFormat('HH:mm').format(time)}"; // 其他年份的日期+时间点 } } ///聊天框时间间隔显示 static String getTimeDisplay(DateTime previousTime, DateTime currentTime) { final diffInMinutes = (currentTime.difference(previousTime)).inMinutes; if (diffInMinutes < 5) { // 时间间隔小于5分钟,不显示时间 return ''; } else if (currentTime.year == DateTime.now().year) { // 同一年内 if (currentTime.day == DateTime.now().day) { // 当天 return DateFormat('HH:mm').format(currentTime); } else if (currentTime.day == DateTime.now().day - 1) { // 昨天 return '昨天 ${DateFormat('HH:mm').format(currentTime)}'; } else if ((currentTime.day - DateTime.now().day).abs() < 7) { // 最近七天 return '${DateFormat('EEEE').format(currentTime)} ${DateFormat('HH:mm').format(currentTime)}'; } else { // 超过一周 return DateFormat('MM/dd').format(currentTime); } } else { // 不同年份 return DateFormat('yyyy/MM/dd').format(currentTime); } } }