Browse Source

feat(message): 完善会话消息内容展示和时间格式化

- 支持展示图片、语音、视频、文件和位置类型的消息内容
-优化消息时间格式化逻辑,区分今天、昨天和其他日期
- 调整推荐用户列表高度并移除冗余间距
ios
Jolie 4 months ago
parent
commit
4d43bb52f2
2 changed files with 36 additions and 27 deletions
  1. 59
      lib/controller/message/conversation_controller.dart
  2. 4
      lib/pages/message/conversation_tab.dart

59
lib/controller/message/conversation_controller.dart

@ -54,7 +54,23 @@ class ConversationController extends GetxController {
///
String getLastMessageContent(EMMessage? message) {
if(message?.body.type == MessageType.TXT){
EMTextMessageBody body = message?.body as EMTextMessageBody;
return body.content;
}else if(message?.body.type == MessageType.IMAGE){
EMImageMessageBody body = message?.body as EMImageMessageBody;
return '[图片]';
}else if(message?.body.type == MessageType.VOICE){
EMVoiceMessageBody body = message?.body as EMVoiceMessageBody;
return '[语音]';
}else if(message?.body.type == MessageType.VIDEO){
EMVideoMessageBody body = message?.body as EMVideoMessageBody;
return '[视频]';
}else if(message?.body.type == MessageType.FILE){
EMFileMessageBody body = message?.body as EMFileMessageBody;
return '[文件]';
}else if(message?.body.type == MessageType.LOCATION){
EMLocationMessageBody body = message?.body as EMLocationMessageBody;
return '[位置]';
}
return '暂无消息';
}
@ -72,34 +88,29 @@ class ConversationController extends GetxController {
}
}
///
String getConversationLastMessageTime(EMConversation conversation) {
try {
//
return '刚刚';
} catch (e) {
if (Get.isLogEnable) {
Get.log('Error getting last message time: $e');
}
return '';
}
}
///
String formatMessageTime(int timestamp) {
DateTime messageTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
DateTime now = DateTime.now();
Duration difference = now.difference(messageTime);
if (difference.inDays > 0) {
return '${difference.inDays}天前';
} else if (difference.inHours > 0) {
return '${difference.inHours}小时前';
} else if (difference.inMinutes > 0) {
return '${difference.inMinutes}分钟前';
} else {
return '刚刚';
//
if (messageTime.year == now.year &&
messageTime.month == now.month &&
messageTime.day == now.day) {
// :
return '${messageTime.hour.toString().padLeft(2, '0')}:${messageTime.minute.toString().padLeft(2, '0')}';
}
//
DateTime yesterday = now.subtract(Duration(days: 1));
if (messageTime.year == yesterday.year &&
messageTime.month == yesterday.month &&
messageTime.day == yesterday.day) {
return '昨天';
}
// -
return '${messageTime.month.toString().padLeft(2, '0')}-${messageTime.day.toString().padLeft(2, '0')}';
}
Future<EMUserInfo> loadContact(String userId) async{

4
lib/pages/message/conversation_tab.dart

@ -74,7 +74,7 @@ class _ConversationTabState extends State<ConversationTab> with AutomaticKeepAli
//
Widget _buildRecommendedUsers() {
return SizedBox(
height: 100,
height: 60,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 16),
@ -124,8 +124,6 @@ class _ConversationTabState extends State<ConversationTab> with AutomaticKeepAli
),
],
),
const SizedBox(height: 4),
Text(user["type"], style: const TextStyle(color: Colors.white)),
],
),
);

Loading…
Cancel
Save