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.
 
 
 
 
 

242 lines
7.1 KiB

import 'package:dating_touchme_app/model/live/gift_product_model.dart';
import 'package:dating_touchme_app/widget/message/chat_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:get/get.dart';
import 'package:dating_touchme_app/controller/mine/rose_controller.dart';
import 'package:dating_touchme_app/generated/assets.dart';
class ChatGiftPopup extends StatefulWidget {
const ChatGiftPopup({
super.key,
required this.activeGift,
required this.giftNum,
required this.giftList,
required this.changeActive,
required this.onSendGift,
});
final ValueNotifier<int?> activeGift;
final ValueNotifier<int> giftNum;
final List<GiftProductModel> giftList;
final void Function(int) changeActive;
final Future<void> Function(GiftProductModel, int) onSendGift;
@override
State<ChatGiftPopup> createState() => _ChatGiftPopupState();
}
class _ChatGiftPopupState extends State<ChatGiftPopup> {
@override
void initState() {
super.initState();
// 默认选择第一个礼物
if (widget.giftList.isNotEmpty && widget.activeGift.value == null) {
widget.activeGift.value = 0;
}
// 刷新玫瑰余额
_refreshRoseBalance();
}
// 刷新玫瑰余额
void _refreshRoseBalance() {
try {
// 确保 RoseController 已注册,如果未注册则注册它
final roseController = Get.isRegistered<RoseController>()
? Get.find<RoseController>()
: Get.put(RoseController());
// 刷新玫瑰余额
roseController.getRoseNum();
print('✅ ChatGiftPopup 刷新玫瑰余额: ${roseController.roseNum.value}');
} catch (e) {
print('❌ ChatGiftPopup 刷新玫瑰余额失败: $e');
}
}
// 构建玫瑰余额显示(参考 LiveGiftPopup 的实现)
Widget _buildRoseBalance() {
// 确保 RoseController 已注册,如果未注册则注册它
final roseController = Get.isRegistered<RoseController>()
? Get.find<RoseController>()
: Get.put(RoseController());
return Row(
children: [
Image.asset(Assets.imagesRoseGift, width: 21.w, height: 21.w),
SizedBox(width: 8.w),
Obx(() {
// 直接访问可观察变量,确保 GetX 能够正确追踪
final roseCount = roseController.roseNum.value;
print('🔄 Obx 更新玫瑰余额: $roseCount');
return Text(
roseCount.toString(),
style: TextStyle(
fontSize: 13.w,
color: Colors.black87, // ChatGiftPopup 背景是白色,使用深色文字
),
);
}),
SizedBox(width: 12.w),
],
);
}
// 处理赠送礼物
Future<void> _handleSendGift() async {
// 检查是否选中了礼物
final activeIndex = widget.activeGift.value;
if (activeIndex == null ||
activeIndex < 0 ||
activeIndex >= widget.giftList.length) {
SmartDialog.showToast('请先选择礼物');
return;
}
// 获取选中的礼物
final gift = widget.giftList[activeIndex];
final quantity = widget.giftNum.value;
// 发送礼物
await widget.onSendGift(gift, quantity);
SmartDialog.dismiss();
}
@override
Widget build(BuildContext context) {
return Material(
color: Colors.transparent,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(
top: Radius.circular(9.w),
),
color: Colors.white,
),
height: 363.w,
child: Column(
children: [
_buildTab(),
_buildGiftSwiper(),
_buildBottomBar(),
],
),
),
);
}
Widget _buildTab() {
return Container(
height: 47.w,
padding: EdgeInsets.only(left: 29.w),
child: Row(
children: [
Text(
"礼物",
style: TextStyle(
fontSize: 13.w,
color: const Color.fromRGBO(117, 98, 249, 1),
fontWeight: FontWeight.w700,
),
),
],
),
);
}
Widget _buildGiftSwiper() {
if (widget.giftList.isEmpty) {
return Expanded(
child: Center(
child: Text(
'暂无礼物',
style: TextStyle(fontSize: 14.w, color: Colors.grey),
),
),
);
}
return Expanded(
child: ValueListenableBuilder<int?>(
valueListenable: widget.activeGift,
builder: (context, active, _) {
return GridView.builder(
padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.w),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, // 每行4个
crossAxisSpacing: 7.w,
mainAxisSpacing: 7.w,
childAspectRatio: 0.85, // 调整宽高比
),
itemCount: widget.giftList.length,
itemBuilder: (context, index) {
return ChatGiftItem(
item: widget.giftList[index],
active: active ?? 0,
index: index,
changeActive: widget.changeActive,
);
},
);
},
),
);
}
Widget _buildBottomBar() {
return SafeArea(
top: false,
child: Container(
padding: EdgeInsets.only(
left: 10.w,
right: 10.w,
bottom: MediaQuery.of(context).padding.bottom > 0
? MediaQuery.of(context).padding.bottom
: 10.h,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// 左下角:显示玫瑰余额
_buildRoseBalance(),
ValueListenableBuilder<int>(
valueListenable: widget.giftNum,
builder: (context, num, _) {
return Row(
children: [
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),
],
),
),
child: Center(
child: Text(
"赠送",
style: TextStyle(fontSize: 13.w, color: Colors.white),
),
),
),
),
],
);
},
),
],
),
),
);
}
}