diff --git a/lib/core/translations/language/en_us.dart b/lib/core/translations/language/en_us.dart index 572e595af..afb5f6242 100644 --- a/lib/core/translations/language/en_us.dart +++ b/lib/core/translations/language/en_us.dart @@ -893,4 +893,16 @@ const Map enUS = { "乌尔都语": "Urdu", "波斯语": "Persian", "斯瓦希里语": "Swahili", + + // Time related + "justNow": "Just now", + "minutesAgo": "@minutes minutes ago", + "yesterday": "Yesterday", + // "monday": "Monday", + // "tuesday": "Tuesday", + // "wednesday": "Wednesday", + // "thursday": "Thursday", + // "friday": "Friday", + // "saturday": "Saturday", + // "sunday": "Sunday", }; diff --git a/lib/core/translations/language/zh_cn.dart b/lib/core/translations/language/zh_cn.dart index 4c1e73d89..23aab872a 100644 --- a/lib/core/translations/language/zh_cn.dart +++ b/lib/core/translations/language/zh_cn.dart @@ -824,4 +824,9 @@ const Map zhCN = { "乌尔都语": "乌尔都语", "波斯语": "波斯语", "斯瓦希里语": "斯瓦希里语", + + // 时间相关 + "justNow": "刚刚", + "minutesAgo": "@minutes分钟前", + "yesterday": "昨天", }; diff --git a/lib/modules/agent/views/message_bubble.dart b/lib/modules/agent/views/message_bubble.dart index 440dc1b0a..b8903cc5f 100644 --- a/lib/modules/agent/views/message_bubble.dart +++ b/lib/modules/agent/views/message_bubble.dart @@ -33,8 +33,6 @@ class MessageBubble extends GetView { @override Widget build(BuildContext context) { - final timeFormat = DateFormat('HH:mm'); - return Padding( padding: const EdgeInsets.symmetric(vertical: 8), child: Row( @@ -91,7 +89,7 @@ class MessageBubble extends GetView { children: [ // 时间戳(左对齐或右对齐) Text( - timeFormat.format(timestamp), + _formatTimestamp(timestamp), style: TextStyle( fontSize: 10, color: Theme.of(context).brightness == Brightness.dark @@ -105,7 +103,7 @@ class MessageBubble extends GetView { Padding( padding: const EdgeInsets.only(left: 8), child: Text( - "generatedByAi".tr,// 由AI生成 + "generatedByAi".tr, // 由AI生成 style: TextStyle( fontSize: 8, color: Colors.blue[600], @@ -418,6 +416,77 @@ class MessageBubble extends GetView { Widget _buildDot(int milliseconds) { return _AnimatedDot(delayMilliseconds: milliseconds); } + + /// 智能格式化时间戳 + String _formatTimestamp(DateTime messageTime) { + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final yesterday = today.subtract(const Duration(days: 1)); + final messageDate = + DateTime(messageTime.year, messageTime.month, messageTime.day); + + // 计算时间差 + final difference = now.difference(messageTime); + + // 1分钟内显示"刚刚" + if (difference.inMinutes < 1) { + return 'justNow'.tr; // 刚刚 + } + + // 1小时内显示"X分钟前" + if (difference.inHours < 1) { + return 'minutesAgo' + .trParams({'minutes': difference.inMinutes.toString()}); // X分钟前 + } + + // 今天的消息显示时间 + if (messageDate == today) { + return DateFormat('HH:mm').format(messageTime); + } + + // 昨天的消息显示"昨天 HH:mm" + if (messageDate == yesterday) { + return 'yesterday'.tr + + ' ' + + DateFormat('HH:mm').format(messageTime); // 昨天 HH:mm + } + + // 本周内的消息显示"星期X HH:mm" + if (difference.inDays < 7) { + final weekday = _getWeekdayName(messageTime.weekday); + return weekday + ' ' + DateFormat('HH:mm').format(messageTime); + } + + // 今年的消息显示"MM-dd HH:mm" + if (messageTime.year == now.year) { + return DateFormat('MM-dd HH:mm').format(messageTime); + } + + // 其他情况显示完整日期"yyyy-MM-dd HH:mm" + return DateFormat('yyyy-MM-dd HH:mm').format(messageTime); + } + + /// 获取星期名称 + String _getWeekdayName(int weekday) { + switch (weekday) { + case 1: + return 'monday'.tr; // 星期一 + case 2: + return 'tuesday'.tr; // 星期二 + case 3: + return 'wednesday'.tr; // 星期三 + case 4: + return 'thursday'.tr; // 星期四 + case 5: + return 'friday'.tr; // 星期五 + case 6: + return 'saturday'.tr; // 星期六 + case 7: + return 'sunday'.tr; // 星期日 + default: + return ''; + } + } } /// 动画点组件