|
|
@ -4,6 +4,7 @@ import 'package:im_flutter_sdk/im_flutter_sdk.dart'; |
|
|
|
|
|
|
|
|
import 'text_item.dart'; |
|
|
import 'text_item.dart'; |
|
|
import 'image_item.dart'; |
|
|
import 'image_item.dart'; |
|
|
|
|
|
import 'voice_item.dart'; |
|
|
|
|
|
|
|
|
class MessageItem extends StatelessWidget { |
|
|
class MessageItem extends StatelessWidget { |
|
|
final EMMessage message; |
|
|
final EMMessage message; |
|
|
@ -39,16 +40,23 @@ class MessageItem extends StatelessWidget { |
|
|
formattedTime: formatMessageTime(message.serverTime), |
|
|
formattedTime: formatMessageTime(message.serverTime), |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理语音消息 |
|
|
|
|
|
else if (message.body.type == MessageType.VOICE) { |
|
|
|
|
|
final voiceBody = message.body as EMVoiceMessageBody; |
|
|
|
|
|
return VoiceItem( |
|
|
|
|
|
voiceBody: voiceBody, |
|
|
|
|
|
isSentByMe: isSentByMe, |
|
|
|
|
|
showTime: shouldShowTime(), |
|
|
|
|
|
formattedTime: formatMessageTime(message.serverTime), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 非文本消息显示占位符 |
|
|
// 非文本消息显示占位符 |
|
|
return Column( |
|
|
return Column( |
|
|
children: [ |
|
|
children: [ |
|
|
if (shouldShowTime()) _buildTimeLabel(), |
|
|
if (shouldShowTime()) _buildTimeLabel(), |
|
|
Container( |
|
|
Container( |
|
|
padding: EdgeInsets.symmetric( |
|
|
|
|
|
horizontal: 16.w, |
|
|
|
|
|
vertical: 8.h, |
|
|
|
|
|
), |
|
|
|
|
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h), |
|
|
child: Text('不支持的消息类型'), |
|
|
child: Text('不支持的消息类型'), |
|
|
), |
|
|
), |
|
|
], |
|
|
], |
|
|
@ -60,7 +68,7 @@ class MessageItem extends StatelessWidget { |
|
|
if (previousMessage == null) { |
|
|
if (previousMessage == null) { |
|
|
return true; // 第一条消息显示时间 |
|
|
return true; // 第一条消息显示时间 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断距离上一条消息是否超过20分钟(1200000毫秒) |
|
|
// 判断距离上一条消息是否超过20分钟(1200000毫秒) |
|
|
return (message.serverTime - previousMessage!.serverTime) > 1200000; |
|
|
return (message.serverTime - previousMessage!.serverTime) > 1200000; |
|
|
} |
|
|
} |
|
|
@ -68,22 +76,15 @@ class MessageItem extends StatelessWidget { |
|
|
// 构建时间标签 |
|
|
// 构建时间标签 |
|
|
Widget _buildTimeLabel() { |
|
|
Widget _buildTimeLabel() { |
|
|
final formattedTime = formatMessageTime(message.serverTime); |
|
|
final formattedTime = formatMessageTime(message.serverTime); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Container( |
|
|
return Container( |
|
|
alignment: Alignment.center, |
|
|
alignment: Alignment.center, |
|
|
padding: EdgeInsets.symmetric( |
|
|
|
|
|
horizontal: 16.w, |
|
|
|
|
|
), |
|
|
|
|
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.w), |
|
|
child: Container( |
|
|
child: Container( |
|
|
padding: EdgeInsets.symmetric( |
|
|
|
|
|
horizontal: 12.w, |
|
|
|
|
|
), |
|
|
|
|
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 12.w), |
|
|
child: Text( |
|
|
child: Text( |
|
|
formattedTime, |
|
|
formattedTime, |
|
|
style: TextStyle( |
|
|
|
|
|
fontSize: 12.sp, |
|
|
|
|
|
color: Colors.grey, |
|
|
|
|
|
), |
|
|
|
|
|
|
|
|
style: TextStyle(fontSize: 12.sp, color: Colors.grey), |
|
|
), |
|
|
), |
|
|
), |
|
|
), |
|
|
); |
|
|
); |
|
|
@ -96,7 +97,7 @@ class MessageItem extends StatelessWidget { |
|
|
final today = DateTime(now.year, now.month, now.day); |
|
|
final today = DateTime(now.year, now.month, now.day); |
|
|
final yesterday = DateTime(now.year, now.month, now.day - 1); |
|
|
final yesterday = DateTime(now.year, now.month, now.day - 1); |
|
|
final messageDate = DateTime(date.year, date.month, date.day); |
|
|
final messageDate = DateTime(date.year, date.month, date.day); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (messageDate.isAtSameMomentAs(today)) { |
|
|
if (messageDate.isAtSameMomentAs(today)) { |
|
|
// 今天的消息显示时间 |
|
|
// 今天的消息显示时间 |
|
|
return '今天 ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}'; |
|
|
return '今天 ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}'; |
|
|
@ -108,6 +109,4 @@ class MessageItem extends StatelessWidget { |
|
|
return '${date.month}月${date.day}日 ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}'; |
|
|
return '${date.month}月${date.day}日 ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}'; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |