|
|
|
@ -8,14 +8,20 @@ import '../../controller/message/chat_controller.dart'; |
|
|
|
import '../../controller/message/voice_player_manager.dart'; |
|
|
|
// import '../../controller/message/call_manager.dart'; // 暂时注释 |
|
|
|
import '../../generated/assets.dart'; |
|
|
|
import '../../model/home/marriage_data.dart'; |
|
|
|
import '../../../widget/message/chat_input_bar.dart'; |
|
|
|
import '../../../widget/message/message_item.dart'; |
|
|
|
import 'chat_settings_page.dart'; |
|
|
|
|
|
|
|
class ChatPage extends StatefulWidget { |
|
|
|
final String userId; |
|
|
|
final String userId; // 对应 MarriageData.userId |
|
|
|
final MarriageData? userData; // 可选:从外部传入的用户数据模型 |
|
|
|
|
|
|
|
const ChatPage({required this.userId, super.key}); |
|
|
|
const ChatPage({ |
|
|
|
required this.userId, |
|
|
|
this.userData, |
|
|
|
super.key, |
|
|
|
}); |
|
|
|
|
|
|
|
@override |
|
|
|
State<ChatPage> createState() => _ChatPageState(); |
|
|
|
@ -29,8 +35,19 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
super.initState(); |
|
|
|
// 初始化 controller |
|
|
|
_controller = Get.put(ChatController(userId: widget.userId)); |
|
|
|
// 初始化 controller,直接传入用户信息参数 |
|
|
|
// 如果已存在相同 userId 的 controller,先删除再创建新的,确保参数正确 |
|
|
|
final tag = 'chat_${widget.userId}'; |
|
|
|
if (Get.isRegistered<ChatController>(tag: tag)) { |
|
|
|
Get.delete<ChatController>(tag: tag); |
|
|
|
} |
|
|
|
_controller = Get.put( |
|
|
|
ChatController( |
|
|
|
userId: widget.userId, |
|
|
|
userData: widget.userData, |
|
|
|
), |
|
|
|
tag: tag, |
|
|
|
); |
|
|
|
|
|
|
|
// 监听滚动,当滚动到顶部时加载更多消息 |
|
|
|
_scrollController.addListener(() { |
|
|
|
@ -79,7 +96,7 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
child: Scaffold( |
|
|
|
backgroundColor: Color(0xffF5F5F5), |
|
|
|
appBar: AppBar( |
|
|
|
title: Text(controller.userInfo.value?.nickName ?? ''), |
|
|
|
title: Text(controller.userNickName ?? widget.userData?.nickName ?? ''), |
|
|
|
centerTitle: true, |
|
|
|
actions: [ |
|
|
|
Container( |
|
|
|
@ -124,7 +141,7 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
itemBuilder: (context, index) { |
|
|
|
// 第一个是用户信息卡片 |
|
|
|
if (index == 0) { |
|
|
|
return _buildUserInfoCard(controller); |
|
|
|
return _buildUserInfoCard(controller, widget.userData); |
|
|
|
} |
|
|
|
|
|
|
|
final message = controller.messages[index - 1]; |
|
|
|
@ -141,6 +158,7 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
message: message, |
|
|
|
isSentByMe: isSentByMe, |
|
|
|
previousMessage: previousMessage, |
|
|
|
chatController: controller, // 传递 controller 避免使用 Get.find |
|
|
|
); |
|
|
|
}, |
|
|
|
), |
|
|
|
@ -204,55 +222,44 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
} |
|
|
|
|
|
|
|
// 构建用户信息卡片 |
|
|
|
Widget _buildUserInfoCard(ChatController controller) { |
|
|
|
final userInfo = controller.userInfo.value; |
|
|
|
Widget _buildUserInfoCard(ChatController controller, MarriageData? userData) { |
|
|
|
// 优先使用传入的 MarriageData,如果没有则使用环信用户信息 |
|
|
|
final marriageData = userData ?? _getMarriageDataFromController(controller); |
|
|
|
|
|
|
|
// 如果用户信息还未加载,显示占位符 |
|
|
|
if (userInfo == null) { |
|
|
|
if (marriageData == null) { |
|
|
|
return SizedBox.shrink(); |
|
|
|
} |
|
|
|
|
|
|
|
return Container( |
|
|
|
margin: EdgeInsets.only(bottom: 16.w), |
|
|
|
decoration: BoxDecoration( |
|
|
|
borderRadius: BorderRadius.circular(16.w), |
|
|
|
), |
|
|
|
child: LayoutBuilder( |
|
|
|
builder: (context, constraints) { |
|
|
|
// 计算按710:351比例的高度 |
|
|
|
// 宽度需要考虑 ListView 的 padding(左右各16.w) |
|
|
|
final screenWidth = MediaQuery.of(context).size.width; |
|
|
|
final availableWidth = screenWidth - 32.w; // 减去左右 padding |
|
|
|
final width = availableWidth; |
|
|
|
final height = width * (351 / 710); |
|
|
|
return Container( |
|
|
|
height: height, |
|
|
|
decoration: BoxDecoration( |
|
|
|
borderRadius: BorderRadius.circular(16.w), |
|
|
|
image: DecorationImage( |
|
|
|
image: AssetImage(Assets.imagesChatUserBg), |
|
|
|
fit: BoxFit.cover, |
|
|
|
return IntrinsicHeight( |
|
|
|
child: Container( |
|
|
|
margin: EdgeInsets.only(bottom: 16.w), |
|
|
|
decoration: BoxDecoration( |
|
|
|
borderRadius: BorderRadius.circular(16.w), |
|
|
|
image: DecorationImage( |
|
|
|
image: AssetImage(Assets.imagesChatUserBg), |
|
|
|
fit: BoxFit.cover, |
|
|
|
), |
|
|
|
), |
|
|
|
child: Stack( |
|
|
|
children: [ |
|
|
|
// 顶层背景图(chat_user_bg_bottom,覆盖整个卡片) |
|
|
|
Positioned.fill( |
|
|
|
child: ClipRRect( |
|
|
|
borderRadius: BorderRadius.circular(16.w), |
|
|
|
child: Image.asset( |
|
|
|
Assets.imagesChatUserBgBottom, |
|
|
|
fit: BoxFit.cover, // 覆盖整个区域,保持宽高比 |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
child: Stack( |
|
|
|
children: [ |
|
|
|
// 顶层背景图(chat_user_bg_bottom,按710x351比例覆盖整个卡片) |
|
|
|
Positioned.fill( |
|
|
|
child: ClipRRect( |
|
|
|
borderRadius: BorderRadius.circular(16.w), |
|
|
|
child: Image.asset( |
|
|
|
Assets.imagesChatUserBgBottom, |
|
|
|
fit: BoxFit.cover, |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
// 内容 |
|
|
|
Padding( |
|
|
|
padding: EdgeInsets.all(16.w), |
|
|
|
child: Column( |
|
|
|
mainAxisSize: MainAxisSize.min, |
|
|
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
|
|
children: [ |
|
|
|
// 内容(自适应高度) |
|
|
|
Padding( |
|
|
|
padding: EdgeInsets.all(16.w), |
|
|
|
child: Column( |
|
|
|
mainAxisSize: MainAxisSize.min, |
|
|
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
|
|
children: [ |
|
|
|
// 头部:名称、标签、箭头 |
|
|
|
Row( |
|
|
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
|
|
@ -268,7 +275,7 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
crossAxisAlignment: WrapCrossAlignment.center, |
|
|
|
children: [ |
|
|
|
Text( |
|
|
|
userInfo.nickName ?? '用户', |
|
|
|
marriageData.nickName, |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 16.sp, |
|
|
|
fontWeight: FontWeight.w600, |
|
|
|
@ -276,47 +283,48 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
), |
|
|
|
), |
|
|
|
// 实名认证标签(和主页一样) |
|
|
|
Container( |
|
|
|
padding: EdgeInsets.symmetric( |
|
|
|
horizontal: 8.w, |
|
|
|
vertical: 2.h, |
|
|
|
), |
|
|
|
decoration: BoxDecoration( |
|
|
|
color: Color(0xFFF3E9FF), |
|
|
|
borderRadius: BorderRadius.circular(12.w), |
|
|
|
border: Border.all( |
|
|
|
width: 1, |
|
|
|
color: Color.fromRGBO(117, 98, 249, 0.32), |
|
|
|
if (marriageData.isRealNameCertified) |
|
|
|
Container( |
|
|
|
padding: EdgeInsets.symmetric( |
|
|
|
horizontal: 8.w, |
|
|
|
vertical: 4.h, |
|
|
|
), |
|
|
|
), |
|
|
|
constraints: BoxConstraints( |
|
|
|
minWidth: 40.w, |
|
|
|
), |
|
|
|
child: Row( |
|
|
|
mainAxisSize: MainAxisSize.min, |
|
|
|
children: [ |
|
|
|
Image.asset( |
|
|
|
Assets.imagesVerifiedIcon, |
|
|
|
width: 14.w, |
|
|
|
height: 12.w, |
|
|
|
decoration: BoxDecoration( |
|
|
|
color: Color(0xFFF3E9FF), |
|
|
|
borderRadius: BorderRadius.circular(12.w), |
|
|
|
border: Border.all( |
|
|
|
width: 1, |
|
|
|
color: Color.fromRGBO(117, 98, 249, 0.32), |
|
|
|
), |
|
|
|
SizedBox(width: 4.w), |
|
|
|
Text( |
|
|
|
'实名', |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 9.sp, |
|
|
|
color: Color.fromRGBO(160, 92, 255, 1), |
|
|
|
fontWeight: FontWeight.w500, |
|
|
|
), |
|
|
|
constraints: BoxConstraints( |
|
|
|
minWidth: 40.w, |
|
|
|
), |
|
|
|
child: Row( |
|
|
|
mainAxisSize: MainAxisSize.min, |
|
|
|
children: [ |
|
|
|
Image.asset( |
|
|
|
Assets.imagesVerifiedIcon, |
|
|
|
width: 14.w, |
|
|
|
height: 12.w, |
|
|
|
), |
|
|
|
), |
|
|
|
], |
|
|
|
SizedBox(width: 4.w), |
|
|
|
Text( |
|
|
|
'实名', |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 9.sp, |
|
|
|
color: Color.fromRGBO(160, 92, 255, 1), |
|
|
|
fontWeight: FontWeight.w500, |
|
|
|
), |
|
|
|
), |
|
|
|
], |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
// 在线状态标签(和主页一样) |
|
|
|
Container( |
|
|
|
padding: EdgeInsets.symmetric( |
|
|
|
horizontal: 8.w, |
|
|
|
vertical: 2.h, |
|
|
|
vertical: 4.h, |
|
|
|
), |
|
|
|
decoration: BoxDecoration( |
|
|
|
color: Color.fromRGBO(234, 255, 219, 1), |
|
|
|
@ -338,10 +346,10 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
], |
|
|
|
), |
|
|
|
SizedBox(height: 8.h), |
|
|
|
// 用户信息:性别图标、年龄、位置、身高 |
|
|
|
// 用户信息:性别图标、年龄、位置(根据 MarriageData 显示) |
|
|
|
Row( |
|
|
|
children: [ |
|
|
|
// 性别图标(默认显示女性图标,可以根据实际数据调整) |
|
|
|
// 性别图标(根据实际数据调整,这里暂时默认显示女性图标) |
|
|
|
Image.asset( |
|
|
|
Assets.imagesFemale, |
|
|
|
width: 14.w, |
|
|
|
@ -349,28 +357,22 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
), |
|
|
|
SizedBox(width: 4.w), |
|
|
|
Text( |
|
|
|
'19', |
|
|
|
'${marriageData.age}', |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 12.sp, |
|
|
|
color: Colors.grey[700], |
|
|
|
), |
|
|
|
), |
|
|
|
SizedBox(width: 12.w), |
|
|
|
Text( |
|
|
|
'北京', |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 12.sp, |
|
|
|
color: Colors.grey[700], |
|
|
|
), |
|
|
|
), |
|
|
|
SizedBox(width: 12.w), |
|
|
|
Text( |
|
|
|
'160cm', |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 12.sp, |
|
|
|
color: Colors.grey[700], |
|
|
|
if (marriageData.cityName.isNotEmpty) ...[ |
|
|
|
SizedBox(width: 12.w), |
|
|
|
Text( |
|
|
|
marriageData.cityName, |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 12.sp, |
|
|
|
color: Colors.grey[700], |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
], |
|
|
|
], |
|
|
|
), |
|
|
|
], |
|
|
|
@ -394,48 +396,70 @@ class _ChatPageState extends State<ChatPage> { |
|
|
|
), |
|
|
|
SizedBox(height: 8.h), // 减小间距 |
|
|
|
// 个人简介(单行) |
|
|
|
Text( |
|
|
|
'喜欢在广州的早茶里抢最后一个虾饺,也能在深夜的猎德大桥...', |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 13.sp, |
|
|
|
color: Colors.grey[800], |
|
|
|
height: 1.4, |
|
|
|
if (marriageData.describeInfo.isNotEmpty) |
|
|
|
Text( |
|
|
|
marriageData.describeInfo, |
|
|
|
style: TextStyle( |
|
|
|
fontSize: 13.sp, |
|
|
|
color: Colors.grey[800], |
|
|
|
height: 1.4, |
|
|
|
), |
|
|
|
maxLines: 1, |
|
|
|
overflow: TextOverflow.ellipsis, |
|
|
|
), |
|
|
|
maxLines: 1, |
|
|
|
overflow: TextOverflow.ellipsis, |
|
|
|
), |
|
|
|
SizedBox(height: 8.h), // 减小间距 |
|
|
|
// 图片画廊 |
|
|
|
Row( |
|
|
|
children: List.generate(4, (index) { |
|
|
|
return Expanded( |
|
|
|
child: Container( |
|
|
|
margin: EdgeInsets.only( |
|
|
|
right: index < 3 ? 8.w : 0, |
|
|
|
), |
|
|
|
height: 50.w, // 进一步减小高度避免被裁剪 |
|
|
|
decoration: BoxDecoration( |
|
|
|
borderRadius: BorderRadius.circular(8.w), |
|
|
|
image: DecorationImage( |
|
|
|
image: AssetImage(Assets.imagesAvatarsExample), |
|
|
|
fit: BoxFit.cover, |
|
|
|
if (marriageData.photoList.isNotEmpty) ...[ |
|
|
|
SizedBox(height: 8.h), // 图片上方间距 |
|
|
|
// 图片画廊(正方形) |
|
|
|
Row( |
|
|
|
children: marriageData.photoList.take(4).toList().asMap().entries.map((entry) { |
|
|
|
final index = entry.key; |
|
|
|
final photo = entry.value; |
|
|
|
final photoUrl = photo.photoUrl.trim().replaceAll('`', ''); // 移除URL中的反引号 |
|
|
|
return Expanded( |
|
|
|
child: AspectRatio( |
|
|
|
aspectRatio: 1, // 设置为正方形 |
|
|
|
child: Container( |
|
|
|
margin: EdgeInsets.only( |
|
|
|
right: index < 3 ? 8.w : 0, |
|
|
|
), |
|
|
|
decoration: BoxDecoration( |
|
|
|
borderRadius: BorderRadius.circular(8.w), |
|
|
|
image: DecorationImage( |
|
|
|
image: NetworkImage(photoUrl), |
|
|
|
fit: BoxFit.cover, |
|
|
|
onError: (exception, stackTrace) { |
|
|
|
// 加载失败时显示占位图 |
|
|
|
return; |
|
|
|
}, |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
}), |
|
|
|
), |
|
|
|
], |
|
|
|
), |
|
|
|
), |
|
|
|
], |
|
|
|
); |
|
|
|
}).toList(), |
|
|
|
), |
|
|
|
SizedBox(height: 4.h), // 图片下方间距 |
|
|
|
], |
|
|
|
], |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
}, |
|
|
|
], |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
// 从 Controller 获取 MarriageData(如果没有传入) |
|
|
|
MarriageData? _getMarriageDataFromController(ChatController controller) { |
|
|
|
// 优先从 controller 获取已保存的 userData |
|
|
|
if (controller.userData != null) { |
|
|
|
return controller.userData; |
|
|
|
} |
|
|
|
|
|
|
|
// 如果 controller 也没有,返回 null,不显示卡片 |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
// 加载更多消息 |
|
|
|
Future<void> _loadMoreMessages() async { |
|
|
|
if (_isLoadingMore) return; |
|
|
|
|