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'; 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 activeGift; final ValueNotifier giftNum; final List giftList; final void Function(int) changeActive; final Future Function(GiftProductModel, int) onSendGift; @override State createState() => _ChatGiftPopupState(); } class _ChatGiftPopupState extends State { @override void initState() { super.initState(); // 默认选择第一个礼物 if (widget.giftList.isNotEmpty && widget.activeGift.value == null) { widget.activeGift.value = 0; } } // 处理赠送礼物 Future _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( 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 Container( padding: EdgeInsets.symmetric(horizontal: 10.w), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // 数量选择(暂时不实现,固定为1) SizedBox(width: 1.w), ValueListenableBuilder( 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), ), ), ), ), ], ); }, ), ], ), ); } }