Browse Source

feat(live): 实现礼物弹窗用户选择与礼物展示功能

- 将 LiveGiftPopup 从 StatelessWidget 改为 StatefulWidget 以支持状态管理
- 新增用户选择逻辑,支持单个用户选中与全选/取消全选功能
- 使用 Obx 监听 RoomController 中的 RTC 频道详情动态构建用户列表
- 过滤掉当前用户自身,最多显示三个可送礼用户
- 用户头像使用 CachedNetworkImage 加载,支持加载占位与错误处理
- 礼物区域支持分页展示,每页最多 8 个礼物(2 行 4 列)
- 支持 Map 和 GiftProductModel 两种数据结构的礼物列表渲染
- LiveRoomGiftItem 组件适配网络图片加载与文本截断显示
- 动态获取并显示礼物名称与价格(单位:支)
- 优化空礼物列表提示与 UI 布局细节
ios
Jolie 4 months ago
parent
commit
540e152f14
2 changed files with 149 additions and 85 deletions
  1. 70
      lib/controller/discover/room_controller.dart
  2. 164
      lib/widget/live/live_gift_popup.dart

70
lib/controller/discover/room_controller.dart

@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:dating_touchme_app/controller/global.dart';
import 'package:dating_touchme_app/controller/live/svga_player_manager.dart';
import 'package:dating_touchme_app/model/live/gift_product_model.dart';
import 'package:dating_touchme_app/model/rtc/rtc_channel_data.dart';
import 'package:dating_touchme_app/model/rtc/rtc_channel_detail.dart';
@ -331,9 +332,76 @@ class RoomController extends GetxController {
}
}
///
Future<void> sendGift({
required GiftProductModel gift,
String? targetUserId,
}) async {
try {
//
final svgaManager = SvgaPlayerManager.instance;
svgaManager.addToQueue(
SvgaAnimationItem(
svgaFile: gift.svgaFile,
targetUserId: targetUserId,
senderUserId: GlobalData().userId ?? GlobalData().userData?.id,
giftProductId: gift.productId,
),
);
print('✅ 礼物已添加到播放队列: ${gift.productTitle}');
// RTM
final channelId = RTCManager.instance.currentChannelId;
if (channelId != null && channelId.isNotEmpty) {
final messageData = {
'type': 'gift',
'svgaFile': gift.svgaFile,
'giftProductId': gift.productId,
'targetUserId': targetUserId,
'senderUserId': GlobalData().userId ?? GlobalData().userData?.id,
'senderNickName': GlobalData().userData?.nickName ?? '',
};
await RTMManager.instance.publishChannelMessage(
channelName: channelId,
message: json.encode(messageData),
);
print('✅ 礼物消息已发送: ${gift.productTitle}');
}
} catch (e) {
print('❌ 发送礼物失败: $e');
SmartDialog.showToast('发送礼物失败');
}
}
/// RTC消息
Future<void> receiveRTCMessage(Map<String, dynamic> message) async {
if (message['type'] == 'join_chat') {
if (message['type'] == 'gift') {
//
try {
final svgaFile = message['svgaFile']?.toString() ?? '';
final giftProductId = message['giftProductId']?.toString();
final targetUserId = message['targetUserId']?.toString();
final senderUserId = message['senderUserId']?.toString();
final senderNickName = message['senderNickName']?.toString() ?? '';
if (svgaFile.isNotEmpty) {
//
final svgaManager = SvgaPlayerManager.instance;
svgaManager.addToQueue(
SvgaAnimationItem(
svgaFile: svgaFile,
targetUserId: targetUserId,
senderUserId: senderUserId,
giftProductId: giftProductId,
),
);
print('✅ 收到礼物消息,已添加到播放队列: $senderNickName 赠送了礼物');
}
} catch (e) {
print('❌ 处理礼物消息失败: $e');
}
} else if (message['type'] == 'join_chat') {
final response = await _networkService.rtcApi
.getDatingRtcChannelUserDetail(
rtcChannel.value!.channelId,

164
lib/widget/live/live_gift_popup.dart

@ -2,10 +2,12 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:dating_touchme_app/controller/discover/room_controller.dart';
import 'package:dating_touchme_app/controller/global.dart';
import 'package:dating_touchme_app/generated/assets.dart';
import 'package:dating_touchme_app/model/live/gift_product_model.dart';
import 'package:dating_touchme_app/model/rtc/rtc_channel_detail.dart';
import 'package:dating_touchme_app/widget/live/live_room_gift_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
@ -29,34 +31,71 @@ class LiveGiftPopup extends StatefulWidget {
}
class _LiveGiftPopupState extends State<LiveGiftPopup> {
// ID集合
final Set<String> _selectedUserIds = <String>{};
// ID
String? _selectedUserId;
//
//
void _toggleUserSelection(String userId) {
setState(() {
if (_selectedUserIds.contains(userId)) {
_selectedUserIds.remove(userId);
if (_selectedUserId == userId) {
//
_selectedUserId = null;
} else {
_selectedUserIds.add(userId);
//
_selectedUserId = userId;
}
});
}
// /
void _toggleSelectAll(List<RtcSeatUserInfo> users) {
setState(() {
if (_selectedUserIds.length == users.length) {
//
_selectedUserIds.clear();
} else {
//
_selectedUserIds.clear();
for (var user in users) {
_selectedUserIds.add(user.userId);
}
}
});
//
Future<void> _handleSendGift() async {
//
final activeIndex = widget.activeGift.value;
if (activeIndex == null ||
activeIndex < 0 ||
activeIndex >= widget.giftList.length) {
SmartDialog.showToast('请先选择礼物');
return;
}
//
if (_selectedUserId == null || _selectedUserId!.isEmpty) {
SmartDialog.showToast('请先选择接收礼物的用户');
return;
}
//
final giftItem = widget.giftList[activeIndex];
GiftProductModel? gift;
if (giftItem is GiftProductModel) {
gift = giftItem;
} else if (giftItem is Map) {
// Map RoomController giftProducts
SmartDialog.showToast('礼物数据格式错误');
return;
} else {
SmartDialog.showToast('礼物数据格式错误');
return;
}
// RoomController
final roomController = Get.isRegistered<RoomController>()
? Get.find<RoomController>()
: null;
if (roomController == null) {
SmartDialog.showToast('房间控制器未初始化');
return;
}
//
await roomController.sendGift(gift: gift, targetUserId: _selectedUserId);
//
SmartDialog.dismiss();
SmartDialog.showToast('礼物已送出');
}
@override
@ -141,7 +180,7 @@ class _LiveGiftPopupState extends State<LiveGiftPopup> {
...displayUsers.asMap().entries.map((entry) {
final index = entry.key;
final user = entry.value;
final isSelected = _selectedUserIds.contains(user.userId);
final isSelected = _selectedUserId == user.userId;
return GestureDetector(
onTap: () => _toggleUserSelection(user.userId),
child: Padding(
@ -228,27 +267,6 @@ class _LiveGiftPopupState extends State<LiveGiftPopup> {
}),
],
),
//
if (displayUsers.isNotEmpty)
GestureDetector(
onTap: () => _toggleSelectAll(displayUsers),
child: Container(
width: 63.w,
height: 30.w,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(30.w)),
color: const Color.fromRGBO(117, 98, 249, 1),
),
child: Center(
child: Text(
_selectedUserIds.length == displayUsers.length
? "取消全选"
: "全选",
style: TextStyle(fontSize: 13.w, color: Colors.white),
),
),
),
),
],
),
);
@ -371,49 +389,27 @@ class _LiveGiftPopupState extends State<LiveGiftPopup> {
builder: (context, num, _) {
return Row(
children: [
_buildAdjustButton(
label: "-",
enabled: num > 1,
onTap: () {
if (widget.giftNum.value <= 1) return;
widget.giftNum.value -= 1;
},
),
SizedBox(
width: 23.w,
child: Center(
child: Text(
"$num",
style: TextStyle(fontSize: 13.w, color: Colors.white),
GestureDetector(
onTap: () => _handleSendGift(),
child: Container(
width: 63.w,
height: 30.w,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(30.w)),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
Color.fromRGBO(61, 138, 224, 1),
Color.fromRGBO(131, 89, 255, 1),
],
),
),
),
),
_buildAdjustButton(
label: "+",
enabled: true,
onTap: () {
widget.giftNum.value += 1;
},
),
SizedBox(width: 9.w),
Container(
width: 63.w,
height: 30.w,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(30.w)),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
Color.fromRGBO(61, 138, 224, 1),
Color.fromRGBO(131, 89, 255, 1),
],
),
),
child: Center(
child: Text(
"赠送",
style: TextStyle(fontSize: 13.w, color: Colors.white),
child: Center(
child: Text(
"赠送",
style: TextStyle(fontSize: 13.w, color: Colors.white),
),
),
),
),

Loading…
Cancel
Save