二次开发源码提交

This commit is contained in:
2026-05-15 08:52:48 +08:00
parent 4131f7321a
commit eb35a1d067
191 changed files with 34566 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
import 'package:intl/intl.dart';
/// 日期工具类
class DateUtils {
/// 格式化日期
static String formatDate(DateTime? date, {String pattern = 'yyyy-MM-dd'}) {
if (date == null) return '-';
final localDate = date.isUtc ? date.toLocal() : date;
return DateFormat(pattern).format(localDate);
}
/// 格式化日期时间
static String formatDateTime(DateTime? dateTime) {
return formatDate(dateTime, pattern: 'yyyy-MM-dd HH:mm:ss');
}
/// 格式化时间
static String formatTime(DateTime? dateTime) {
return formatDate(dateTime, pattern: 'HH:mm:ss');
}
/// 格式化文件大小
static String formatFileSize(int? bytes) {
if (bytes == null || bytes < 0) return '0 B';
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
double size = bytes.toDouble();
int unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return '${size.toStringAsFixed(2)} ${units[unitIndex]}';
}
/// 获取相对时间描述
static String getRelativeTime(DateTime? dateTime) {
if (dateTime == null) return '-';
final now = DateTime.now();
final difference = now.difference(dateTime);
if (difference.inSeconds < 60) {
return '${difference.inSeconds}秒前';
} else if (difference.inMinutes < 60) {
return '${difference.inMinutes}分钟前';
} else if (difference.inHours < 24) {
return '${difference.inHours}小时前';
} else if (difference.inDays < 30) {
return '${difference.inDays}天前';
} else if (difference.inDays < 365) {
return '${(difference.inDays / 30).floor()}个月前';
} else {
return '${(difference.inDays / 365).floor()}年前';
}
}
/// 判断是否为今天
static bool isToday(DateTime? dateTime) {
if (dateTime == null) return false;
final now = DateTime.now();
return dateTime.year == now.year &&
dateTime.month == now.month &&
dateTime.day == now.day;
}
/// 判断是否为昨天
static bool isYesterday(DateTime? dateTime) {
if (dateTime == null) return false;
final now = DateTime.now();
final yesterday = now.subtract(const Duration(days: 1));
return dateTime.year == yesterday.year &&
dateTime.month == yesterday.month &&
dateTime.day == yesterday.day;
}
}