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.
112 lines
4.2 KiB
112 lines
4.2 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 '../../controller/message/voice_player_manager.dart';
|
|
import '../../generated/assets.dart';
|
|
import '../../../widget/message/chat_input_bar.dart';
|
|
import '../../../widget/message/message_item.dart';
|
|
|
|
class ChatPage extends StatelessWidget {
|
|
final String userId;
|
|
|
|
const ChatPage({required this.userId, super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GetBuilder<ChatController>(
|
|
init: ChatController(userId: userId),
|
|
builder: (controller) {
|
|
return WillPopScope(
|
|
onWillPop: () async {
|
|
// 退出页面时停止播放并销毁播放器
|
|
await VoicePlayerManager.instance.stop();
|
|
return true;
|
|
},
|
|
child: 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(
|
|
color: Color(0xffF5F5F5),
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
// 点击消息区域收起键盘
|
|
FocusManager.instance.primaryFocus?.unfocus();
|
|
},
|
|
behavior: HitTestBehavior.opaque,
|
|
child: ListView.builder(
|
|
reverse: true,
|
|
padding: EdgeInsets.all(16.w),
|
|
itemCount: controller.messages.length,
|
|
itemBuilder: (context, index) {
|
|
final message = controller.messages[index];
|
|
final isSentByMe =
|
|
message.direction == MessageDirection.SEND;
|
|
|
|
final previousMessage = index > 0
|
|
? controller.messages[index - 1]
|
|
: null;
|
|
|
|
return MessageItem(
|
|
message: message,
|
|
isSentByMe: isSentByMe,
|
|
previousMessage: previousMessage,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
// 使用抽离的聊天输入栏组件
|
|
ChatInputBar(
|
|
onSendMessage: (message) async {
|
|
await controller.sendMessage(message);
|
|
},
|
|
onImageSelected: (imagePaths) async {
|
|
// 为每个图片路径调用控制器的方法发送图片消息
|
|
for (var imagePath in imagePaths) {
|
|
await controller.sendImageMessage(imagePath);
|
|
}
|
|
},
|
|
onVoiceRecorded: (filePath, seconds) async {
|
|
// 处理语音录音完成,回传文件路径和秒数
|
|
await controller.sendVoiceMessage(filePath, seconds);
|
|
},
|
|
onVideoRecorded: (filePath, duration) async {
|
|
print('🎬 [ChatPage] 收到视频录制/选择回调');
|
|
print('文件路径: $filePath');
|
|
print('时长: $duration 秒');
|
|
// 处理视频录制/选择完成,回传文件路径和时长
|
|
await controller.sendVideoMessage(filePath, duration);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|