You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
8.5 KiB

import 'dart:convert';
4 years ago
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:intl/intl.dart';
4 years ago
import 'package:path_provider/path_provider.dart';
4 years ago
class AppUtils {
3 years ago
///保留两位小数
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对象
1.动态详情,评论-已显示全部—修改; 2.新增加集换卡详情页面; 3.语言-增加部分文本语言并更改; 4.回乡VIP板块更改;原本的VIP卡逻辑暂时不用,换成用户充值200即成为会员,该页面ui调整;(原来逻辑未删减,暂保留不使用) 5.16进制渐变色值的方法,改变亮度暂定统一为95; 6.首页ui调整;邀请好友图片处,原单一图片现更改为轮播效果,新增集换卡引导也放在此处; 7.积分明细页面更改;共用一个页面根据积分跟集换卡的入口,传对应的值,查看对应的数据; 8.积分明细跳转路径更改,新增一个类型带入路径中跳转传入; 9.更改测试版本服务地址; 10.我的页面跟首页更改一致,单一图片改轮播效果; 11.会员中心,会员卡部分更改;将原有写法去掉,重写了会员卡的使用显示,并优化了该页面数据的显示; 12.我的页面,vip等级显示更改; 13.会员卡列表等级接口,实体类新增部分字段;(原定位分数已不用,更改为originScore,后期若用的原字段,需要修改) 14.订单结算页,支付类型更换时queryOrderInfo()接口中。优惠类型做了调整更改;(改修改需要多测试下,尝试多种情况看下,是否有问题;有可能部分情况没有预判到) 15.点单页结算跳转,vipLevelName,vipLevelName字段做了非空判断; 16.UserInfo实体类新增isVip字段;(主要用于查看用户数据是查询该用户是否已是vip卡状态) 17.福利兑换中心页面,我的信息部分ui做调整,新增集换卡数量的显示;
2 years ago
Color color = Color(int.parse(hexColor.replaceAll("#", "FF"), radix: 16));
// 计算颜色的RGB值
1.动态详情,评论-已显示全部—修改; 2.新增加集换卡详情页面; 3.语言-增加部分文本语言并更改; 4.回乡VIP板块更改;原本的VIP卡逻辑暂时不用,换成用户充值200即成为会员,该页面ui调整;(原来逻辑未删减,暂保留不使用) 5.16进制渐变色值的方法,改变亮度暂定统一为95; 6.首页ui调整;邀请好友图片处,原单一图片现更改为轮播效果,新增集换卡引导也放在此处; 7.积分明细页面更改;共用一个页面根据积分跟集换卡的入口,传对应的值,查看对应的数据; 8.积分明细跳转路径更改,新增一个类型带入路径中跳转传入; 9.更改测试版本服务地址; 10.我的页面跟首页更改一致,单一图片改轮播效果; 11.会员中心,会员卡部分更改;将原有写法去掉,重写了会员卡的使用显示,并优化了该页面数据的显示; 12.我的页面,vip等级显示更改; 13.会员卡列表等级接口,实体类新增部分字段;(原定位分数已不用,更改为originScore,后期若用的原字段,需要修改) 14.订单结算页,支付类型更换时queryOrderInfo()接口中。优惠类型做了调整更改;(改修改需要多测试下,尝试多种情况看下,是否有问题;有可能部分情况没有预判到) 15.点单页结算跳转,vipLevelName,vipLevelName字段做了非空判断; 16.UserInfo实体类新增isVip字段;(主要用于查看用户数据是查询该用户是否已是vip卡状态) 17.福利兑换中心页面,我的信息部分ui做调整,新增集换卡数量的显示;
2 years ago
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);
}
3 years ago
///版本号
3 years ago
static bool versionCompare(String localVersion, String serverVersion) {
try {
3 years ago
var localVersionArr = localVersion.split(".");
var serverVersionArr = serverVersion.split(".");
3 years ago
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]))
3 years ago
return false;
} catch (ex) {}
return true;
}
static String trimEnd(String resString, String char) {
while (resString.endsWith(char)) {
resString = resString.substring(0, resString.length - 1);
3 years ago
}
return resString;
}
///金额保留整数/后两位
3 years ago
static String calculateDouble(double res) {
3 years ago
String resString = res.toStringAsFixed(2);
resString = trimEnd(resString, "0");
resString = trimEnd(resString, ".");
return resString;
}
3 years ago
3 years ago
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);
}
3 years ago
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;
3 years ago
// print("textScaleFactor: $textScaleFactor");
4 years ago
return textScaleFactor > 1.15 ? 1.15 : textScaleFactor;
}
4 years ago
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;
4 years ago
DateTime brt = DateFormat("yyyy-MM-dd").parse(birth);
4 years ago
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();
}
4 years ago
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);
4 years ago
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
brt.millisecondsSinceEpoch + 30 * 60 * 1000);
4 years ago
return DateFormat("HH:mm").format(dateTime); //分钟
}
4 years ago
/// 获取缓存大小
static Future<int> total() async {
Directory tempDir = await getTemporaryDirectory();
if (tempDir == null) return 0;
int total = await _reduce(tempDir);
return total;
}
/// 清除缓存
4 years ago
static Future<void> clear() async {
4 years ago
Directory tempDir = await getTemporaryDirectory();
if (tempDir == null) return 0;
await _delete(tempDir);
}
/// 递归缓存目录,计算缓存大小
static Future<int> _reduce(FileSystemEntity file) async {
/// 如果是一个文件,则直接返回文件大小
if (file is File) {
int length = await file.length();
return length;
}
4 years ago
4 years ago
/// 如果是目录,则遍历目录并累计大小
if (file is Directory) {
final List<FileSystemEntity> 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;
}
4 years ago
4 years ago
/// 递归删除缓存目录和文件
4 years ago
static Future<void> _delete(FileSystemEntity file) async {
4 years ago
if (file is Directory) {
final List<FileSystemEntity> children = file.listSync();
for (final FileSystemEntity child in children) {
await child.delete(recursive: true);
}
} else {
await file.delete();
}
}
/// 递归删除缓存目录和文件
4 years ago
static Future<void> deleteDirectory(FileSystemEntity file) async {
4 years ago
if (file is Directory) {
final List<FileSystemEntity> children = file.listSync();
for (final FileSystemEntity child in children) {
child.deleteSync(recursive: true);
}
}
}
3 years ago
static String dioErrorTypeToString(DioErrorType type) {
String errorStr = "";
3 years ago
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:
3 years ago
default:
{
errorStr = "请求异常";
break;
}
}
errorStr += ",请检查手机网络是否正常";
return errorStr;
}
static String textSubStr(String input, int length) {
length -= 1;
3 years ago
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;
}
}
4 years ago
}