|
|
|
@ -2,6 +2,7 @@ import 'package:dating_touchme_app/extension/ex_widget.dart'; |
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
|
|
import 'package:get/get.dart'; |
|
|
|
import 'package:im_flutter_sdk/im_flutter_sdk.dart'; |
|
|
|
|
|
|
|
import '../../controller/message/chat_controller.dart'; |
|
|
|
import '../../generated/assets.dart'; |
|
|
|
@ -12,6 +13,77 @@ class ChatPage extends StatelessWidget { |
|
|
|
|
|
|
|
const ChatPage({required this.userId, super.key}); |
|
|
|
|
|
|
|
// 构建消息项 |
|
|
|
Widget _buildMessageItem(EMMessage message, bool isSentByMe) { |
|
|
|
// 只处理文本消息,其他类型消息可以根据需要扩展 |
|
|
|
if (message.body.type == MessageType.TXT) { |
|
|
|
final textBody = message.body as EMTextMessageBody; |
|
|
|
return 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), |
|
|
|
padding: EdgeInsets.symmetric( |
|
|
|
horizontal: 12.w, |
|
|
|
vertical: 8.h, |
|
|
|
), |
|
|
|
decoration: BoxDecoration( |
|
|
|
color: isSentByMe ? Color(0xff98E165) : Colors.white, |
|
|
|
borderRadius: BorderRadius.only( |
|
|
|
topLeft: Radius.circular(12.w), |
|
|
|
topRight: Radius.circular(12.w), |
|
|
|
bottomLeft: isSentByMe ? Radius.circular(12.w) : Radius.circular(0), |
|
|
|
bottomRight: isSentByMe ? Radius.circular(0) : Radius.circular(12.w), |
|
|
|
), |
|
|
|
), |
|
|
|
child: Text( |
|
|
|
textBody.content, |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 14.sp, |
|
|
|
color: isSentByMe ? Colors.black : Colors.black, |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
if (isSentByMe) SizedBox(width: 8.w), |
|
|
|
if (isSentByMe) _buildAvatar(), |
|
|
|
], |
|
|
|
), |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
// 非文本消息显示占位符 |
|
|
|
return Container( |
|
|
|
padding: EdgeInsets.symmetric( |
|
|
|
horizontal: 16.w, |
|
|
|
vertical: 8.h, |
|
|
|
), |
|
|
|
child: Text('不支持的消息类型'), |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
// 构建头像 |
|
|
|
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, |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
@override |
|
|
|
Widget build(BuildContext context) { |
|
|
|
return GetBuilder<ChatController>( |
|
|
|
@ -47,9 +119,12 @@ class ChatPage extends StatelessWidget { |
|
|
|
alignment: Alignment.topCenter, |
|
|
|
child: ListView.builder( |
|
|
|
reverse: true, |
|
|
|
itemCount: 10, |
|
|
|
itemCount: controller.messages.length, |
|
|
|
itemBuilder: (context, index) { |
|
|
|
return Container(); |
|
|
|
final message = controller.messages[index]; |
|
|
|
print('message: $message'); |
|
|
|
final isSentByMe = message.direction == MessageDirection.SEND; |
|
|
|
return _buildMessageItem(message, isSentByMe); |
|
|
|
}, |
|
|
|
), |
|
|
|
), |
|
|
|
|