|
|
|
@ -37,6 +37,8 @@ class LiveGiftPopup extends StatefulWidget { |
|
|
|
class _LiveGiftPopupState extends State<LiveGiftPopup> { |
|
|
|
// 选中的用户ID(单选) |
|
|
|
int? _selectedUserId; |
|
|
|
// 标记是否已经尝试过设置默认选中 |
|
|
|
bool _hasTriedSetDefault = false; |
|
|
|
|
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
@ -44,6 +46,9 @@ class _LiveGiftPopupState extends State<LiveGiftPopup> { |
|
|
|
// 如果传入了目标用户ID,直接设置 |
|
|
|
if (widget.targetUserId != null) { |
|
|
|
_selectedUserId = widget.targetUserId; |
|
|
|
} else if (widget.showHeader) { |
|
|
|
// 如果没有传入目标用户ID且显示头部,默认选中主持人 |
|
|
|
_initDefaultSelectedUser(); |
|
|
|
} |
|
|
|
// 默认选择第一个礼物 |
|
|
|
if (widget.giftList.isNotEmpty && widget.activeGift.value == null) { |
|
|
|
@ -51,6 +56,36 @@ class _LiveGiftPopupState extends State<LiveGiftPopup> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 初始化默认选中的用户(主持人) |
|
|
|
void _initDefaultSelectedUser() { |
|
|
|
try { |
|
|
|
final roomController = Get.isRegistered<RoomController>() |
|
|
|
? Get.find<RoomController>() |
|
|
|
: null; |
|
|
|
|
|
|
|
if (roomController != null) { |
|
|
|
final rtcChannelDetail = roomController.rtcChannelDetail.value; |
|
|
|
// 默认选中主持人(anchorInfo) |
|
|
|
if (rtcChannelDetail?.anchorInfo != null) { |
|
|
|
final anchorInfo = rtcChannelDetail!.anchorInfo!; |
|
|
|
// 获取当前用户ID,排除本人 |
|
|
|
final currentUserId = GlobalData().userId ?? GlobalData().userData?.id; |
|
|
|
// 如果主持人不是本人,则默认选中 |
|
|
|
if (anchorInfo.userId != currentUserId && anchorInfo.miId != currentUserId) { |
|
|
|
// 使用 uid 作为选中标识 |
|
|
|
if (anchorInfo.uid != null) { |
|
|
|
setState(() { |
|
|
|
_selectedUserId = anchorInfo.uid; |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
print('初始化默认选中用户失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 切换用户选中状态(单选模式) |
|
|
|
void _toggleUserSelection(int userId) { |
|
|
|
setState(() { |
|
|
|
@ -201,6 +236,21 @@ class _LiveGiftPopupState extends State<LiveGiftPopup> { |
|
|
|
// 最多显示3个用户 |
|
|
|
final displayUsers = filteredUserList.take(3).toList(); |
|
|
|
|
|
|
|
// 如果还没有选中用户,且主持人存在且不是本人,默认选中主持人 |
|
|
|
if (!_hasTriedSetDefault && _selectedUserId == null && rtcChannelDetail?.anchorInfo != null) { |
|
|
|
final anchorInfo = rtcChannelDetail!.anchorInfo!; |
|
|
|
if (anchorInfo.userId != currentUserId && anchorInfo.miId != currentUserId && anchorInfo.uid != null) { |
|
|
|
_hasTriedSetDefault = true; |
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) { |
|
|
|
if (mounted && _selectedUserId == null) { |
|
|
|
setState(() { |
|
|
|
_selectedUserId = anchorInfo.uid; |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return Container( |
|
|
|
height: 53.w, |
|
|
|
padding: EdgeInsets.symmetric(horizontal: 10.w), |
|
|
|
|