Browse Source

feat(消息气泡): 添加智能时间戳格式化功能

在消息气泡组件中实现智能时间显示逻辑,根据时间差显示"刚刚"、"X分钟前"、"昨天"等友好格式
同时添加中英文时间相关翻译字段
weicu
lxm 1 year ago
parent
commit
a6d2277983
  1. 12
      lib/core/translations/language/en_us.dart
  2. 5
      lib/core/translations/language/zh_cn.dart
  3. 77
      lib/modules/agent/views/message_bubble.dart

12
lib/core/translations/language/en_us.dart

@ -893,4 +893,16 @@ const Map<String, String> 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",
};

5
lib/core/translations/language/zh_cn.dart

@ -824,4 +824,9 @@ const Map<String, String> zhCN = {
"乌尔都语": "乌尔都语",
"波斯语": "波斯语",
"斯瓦希里语": "斯瓦希里语",
// 时间相关
"justNow": "刚刚",
"minutesAgo": "@minutes分钟前",
"yesterday": "昨天",
};

77
lib/modules/agent/views/message_bubble.dart

@ -33,8 +33,6 @@ class MessageBubble extends GetView<AgentController> {
@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<AgentController> {
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<AgentController> {
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<AgentController> {
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 '';
}
}
}
/// 动画点组件

Loading…
Cancel
Save