Browse Source
feat(message): 实现消息页面UI优化与文本消息组件
feat(message): 实现消息页面UI优化与文本消息组件
- 在聊天控制器中添加update()调用以通知UI更新- 调整聊天页面背景颜色并增加列表内边距- 修复消息方向判断逻辑,正确显示发送/接收状态 - 完善IM管理器中的连接事件处理器代码格式 - 移除消息类型处理中的空实现块 -优化发送文本消息的返回逻辑 - 格式化用户信息获取方法参数 - 修正消息气泡圆角样式,统一底部圆角- 新增独立的文本消息展示组件TextItem- 实现带时间戳和头像的文本消息渲染 - 支持根据发送方调整消息布局和样式ios
5 changed files with 167 additions and 64 deletions
Split View
Diff Options
-
3lib/controller/message/chat_controller.dart
-
87lib/im/im_manager.dart
-
32lib/pages/message/chat_page.dart
-
2lib/widget/message/message_item.dart
-
107lib/widget/message/text_item.dart
@ -0,0 +1,107 @@ |
|||
import 'package:flutter/material.dart'; |
|||
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|||
import 'package:im_flutter_sdk/im_flutter_sdk.dart'; |
|||
|
|||
import '../../../generated/assets.dart'; |
|||
|
|||
class TextItem extends StatelessWidget { |
|||
final EMTextMessageBody textBody; |
|||
final bool isSentByMe; |
|||
final bool showTime; |
|||
final String formattedTime; |
|||
|
|||
const TextItem({ |
|||
required this.textBody, |
|||
required this.isSentByMe, |
|||
required this.showTime, |
|||
required this.formattedTime, |
|||
super.key, |
|||
}); |
|||
|
|||
@override |
|||
Widget build(BuildContext context) { |
|||
return Column( |
|||
children: [ |
|||
// 显示时间 |
|||
if (showTime) _buildTimeLabel(), |
|||
Container( |
|||
padding: EdgeInsets.symmetric( |
|||
horizontal: 16.w, |
|||
vertical: 8.h, |
|||
), |
|||
child: Row( |
|||
mainAxisAlignment: isSentByMe ? MainAxisAlignment.end : MainAxisAlignment.start, |
|||
crossAxisAlignment: CrossAxisAlignment.start, |
|||
children: [ |
|||
if (!isSentByMe) _buildAvatar(), |
|||
if (!isSentByMe) SizedBox(width: 8.w), |
|||
Container( |
|||
constraints: BoxConstraints(maxWidth: 240.w), |
|||
margin: EdgeInsets.only(top: 10.h), |
|||
padding: EdgeInsets.symmetric( |
|||
horizontal: 12.w, |
|||
vertical: 8.h, |
|||
), |
|||
decoration: BoxDecoration( |
|||
color: isSentByMe ? Color(0xff8E7BF6) : Colors.white, // 发送方紫色,接收方白色 |
|||
borderRadius: BorderRadius.only( |
|||
topLeft: isSentByMe ? Radius.circular(12.w) : Radius.circular(0), |
|||
topRight: isSentByMe ? Radius.circular(0) : Radius.circular(12.w), |
|||
bottomLeft: Radius.circular(12.w), |
|||
bottomRight: Radius.circular(12.w), |
|||
), |
|||
), |
|||
child: Text( |
|||
textBody.content, |
|||
style: TextStyle( |
|||
fontSize: 14.sp, |
|||
color: isSentByMe ? Colors.white : Colors.black, // 发送方白色文字,接收方黑色文字 |
|||
), |
|||
), |
|||
), |
|||
if (isSentByMe) SizedBox(width: 8.w), |
|||
if (isSentByMe) _buildAvatar(), |
|||
], |
|||
), |
|||
), |
|||
], |
|||
); |
|||
} |
|||
|
|||
// 构建时间标签 |
|||
Widget _buildTimeLabel() { |
|||
return Container( |
|||
alignment: Alignment.center, |
|||
padding: EdgeInsets.symmetric( |
|||
horizontal: 16.w, |
|||
), |
|||
child: Container( |
|||
padding: EdgeInsets.symmetric( |
|||
horizontal: 12.w, |
|||
), |
|||
child: Text( |
|||
formattedTime, |
|||
style: TextStyle( |
|||
fontSize: 12.sp, |
|||
color: Colors.grey, |
|||
), |
|||
), |
|||
), |
|||
); |
|||
} |
|||
|
|||
// 构建头像 |
|||
Widget _buildAvatar() { |
|||
return Container( |
|||
width: 40.w, |
|||
height: 40.w, |
|||
decoration: BoxDecoration( |
|||
borderRadius: BorderRadius.circular(20.w), |
|||
image: DecorationImage( |
|||
image: AssetImage(Assets.imagesAvatarsExample), |
|||
fit: BoxFit.cover, |
|||
), |
|||
), |
|||
); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save