You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
4.3 KiB
144 lines
4.3 KiB
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';
|
|
import '../../../widget/message/chat_input_bar.dart';
|
|
|
|
class ChatPage extends StatelessWidget {
|
|
final String userId;
|
|
|
|
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>(
|
|
init: ChatController(userId: userId),
|
|
builder: (controller) {
|
|
return Scaffold(
|
|
|
|
|
|
backgroundColor: Color(0xffF5F5F5),
|
|
appBar: AppBar(
|
|
title: Text(controller.userInfo.value?.nickName ?? ''),
|
|
centerTitle: true,
|
|
actions: [
|
|
Container(
|
|
padding: EdgeInsets.only(right: 16.w),
|
|
child: Image.asset(Assets.imagesMore, width: 16.w,),
|
|
).onTap(() {
|
|
|
|
})
|
|
],
|
|
leading: IconButton(
|
|
icon: Icon(Icons.arrow_back_ios),
|
|
onPressed: () {
|
|
Get.back();
|
|
},
|
|
),
|
|
),
|
|
body: Column(
|
|
children: [
|
|
// 消息列表区域
|
|
Expanded(
|
|
child: Container(
|
|
alignment: Alignment.topCenter,
|
|
child: ListView.builder(
|
|
reverse: true,
|
|
itemCount: controller.messages.length,
|
|
itemBuilder: (context, index) {
|
|
final message = controller.messages[index];
|
|
print('message: $message');
|
|
final isSentByMe = message.direction == MessageDirection.SEND;
|
|
return _buildMessageItem(message, isSentByMe);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
// 使用抽离的聊天输入栏组件
|
|
ChatInputBar(
|
|
onSendMessage: (message) async {
|
|
await controller.sendMessage(message);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|