import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:yumi/ui_kit/components/sc_compontent.dart'; import 'package:yumi/ui_kit/components/sc_rotating_dots_loading.dart'; import 'package:yumi/ui_kit/components/text/sc_text.dart'; import 'package:yumi/ui_kit/theme/socialchat_theme.dart'; import 'package:yumi/shared/business_logic/models/res/gift_res.dart'; import 'package:yumi/services/general/sc_app_general_manager.dart'; import 'package:provider/provider.dart'; import 'package:yumi/app_localizations.dart'; import 'package:yumi/app/config/business_logic_strategy.dart'; import 'package:yumi/app/constants/sc_global_config.dart'; enum _GiftGridPageStatus { idle, loading, ready } const Duration _kGiftPageSkeletonMinDuration = Duration(milliseconds: 420); const Duration _kGiftPageSkeletonMaxDuration = Duration(milliseconds: 900); const int _kGiftGridColumnCount = 4; const int _kGiftGridRowCount = 2; const double _kGiftGridCardHeight = 76; const double _kGiftGridVerticalPadding = 5; const double _kGiftGridMainAxisSpacing = 2; const double _kGiftGridCrossAxisSpacing = 8; const double _kGiftCardVerticalPadding = 5; const double _kGiftIconPriceSpacing = 1; const double _kGiftPriceRowHeight = 14; const double _kGiftGridTopGap = 5; const double _kGiftIndicatorTopGap = 5; const double _kGiftPageIndicatorHeight = 6.5; const double _kGiftGridBottomPadding = 6; const double _kGiftGridHeight = _kGiftGridCardHeight * _kGiftGridRowCount + _kGiftGridMainAxisSpacing * (_kGiftGridRowCount - 1) + _kGiftGridVerticalPadding * 2; const double kGiftTabPageHeight = _kGiftGridTopGap + _kGiftGridHeight + _kGiftIndicatorTopGap + _kGiftPageIndicatorHeight + _kGiftGridBottomPadding; class GiftTabPage extends StatefulWidget { final String type; final bool isDark; final ValueChanged checkedCall; const GiftTabPage( this.type, this.checkedCall, { super.key, this.isDark = true, }); @override State createState() => _GiftTabPageState(); } class _GiftTabPageState extends State with AutomaticKeepAliveClientMixin { final PageController _giftPageController = PageController(); final Map _pageStatuses = {}; int _index = 0; int checkedIndex = 0; BusinessLogicStrategy get _strategy => SCGlobalConfig.businessLogicStrategy; @override bool get wantKeepAlive => true; @override void initState() { super.initState(); checkedIndex = widget.type == "CUSTOMIZED" ? 1 : 0; } @override Widget build(BuildContext context) { super.build(context); return Consumer( builder: (context, ref, child) { final gifts = ref.giftByTab[widget.type] ?? []; if (gifts.isNotEmpty) { _schedulePageWarmup(ref, gifts, _index); } return gifts.isEmpty ? (ref.isGiftTabLoading(widget.type) ? _buildGiftPageSkeleton() : mainEmpty( textColor: Colors.white54, msg: SCAppLocalizations.of(context)!.noData, )) : Column( children: [ SizedBox(height: _kGiftGridTopGap.w), SizedBox( height: _kGiftGridHeight.w, child: PageView.builder( controller: _giftPageController, onPageChanged: (i) { setState(() { _index = i; }); _schedulePageWarmup(ref, gifts, i); }, itemBuilder: (c, i) { _schedulePageWarmup(ref, gifts, i); var current = (gifts.length - (i * 8)); int size = current > 8 ? 8 : current % 8 == 0 ? 8 : current % 8; if ((_pageStatuses[i] ?? _GiftGridPageStatus.idle) != _GiftGridPageStatus.ready) { return _buildGiftPageSkeleton(); } return _buildGiftGrid( itemCount: size, itemBuilder: (BuildContext context, int index) { return _bagItem(gifts[(i * 8) + index], ref); }, ); }, itemCount: (gifts.length / 8).ceil(), ), ), SizedBox(height: _kGiftIndicatorTopGap.w), _indicator(ref), SizedBox(height: _kGiftGridBottomPadding.w), ], ); }, ); } void _schedulePageWarmup( SCAppGeneralManager ref, List gifts, int pageIndex, ) { final totalPages = (gifts.length / 8).ceil(); _ensurePageReady(ref, totalPages, pageIndex); _ensurePageReady(ref, totalPages, pageIndex + 1); } void _ensurePageReady( SCAppGeneralManager ref, int totalPages, int pageIndex, ) { if (pageIndex < 0 || pageIndex >= totalPages) { return; } final status = _pageStatuses[pageIndex] ?? _GiftGridPageStatus.idle; if (status == _GiftGridPageStatus.loading || status == _GiftGridPageStatus.ready) { return; } _pageStatuses[pageIndex] = _GiftGridPageStatus.loading; WidgetsBinding.instance.addPostFrameCallback((_) async { final warmupTask = ref.warmGiftPageCovers( widget.type, pageIndex: pageIndex, ); await Future.any([ Future.wait([ warmupTask, Future.delayed(_kGiftPageSkeletonMinDuration), ]), Future.delayed(_kGiftPageSkeletonMaxDuration), ]); if (!mounted) { return; } setState(() { _pageStatuses[pageIndex] = _GiftGridPageStatus.ready; }); }); } Widget _buildGiftPageSkeleton() { return Stack( children: [ Positioned.fill( child: DecoratedBox( decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.w), gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ Colors.white.withValues(alpha: 0.1), Colors.white.withValues(alpha: 0.04), ], ), ), ), ), Positioned.fill( child: Center( child: Container( width: 132.w, height: 132.w, decoration: BoxDecoration( color: Colors.black.withValues(alpha: 0.08), borderRadius: BorderRadius.circular(20.w), border: Border.all( color: Colors.white.withValues(alpha: 0.12), width: 1.w, ), ), alignment: Alignment.center, child: Opacity( opacity: 0.9, child: SCRotatingDotsLoading(size: 72.w), ), ), ), ), Positioned.fill( child: _buildGiftGrid( itemCount: 8, itemBuilder: (context, index) => _buildGiftSkeletonCard(), ), ), ], ); } Widget _buildGiftSkeletonCard() { final baseColor = Colors.white.withValues(alpha: 0.12); return Container( decoration: BoxDecoration(borderRadius: BorderRadius.circular(12.w)), child: Padding( padding: EdgeInsets.symmetric(vertical: _kGiftCardVerticalPadding.w), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Container( width: 48.w, height: 48.w, decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.w), color: baseColor, ), child: Center(child: SCRotatingDotsLoading(size: 24.w)), ), SizedBox(height: _kGiftIconPriceSpacing.w), Container( width: 52.w, height: 8.w, decoration: BoxDecoration( borderRadius: BorderRadius.circular(999.w), color: baseColor, ), ), ], ), ), ); } Widget _buildGiftGrid({ required int itemCount, required IndexedWidgetBuilder itemBuilder, }) { return LayoutBuilder( builder: (context, constraints) { final horizontalPadding = 12.w; final verticalPadding = _kGiftGridVerticalPadding.w; final mainAxisSpacing = _kGiftGridMainAxisSpacing.w; final crossAxisSpacing = _kGiftGridCrossAxisSpacing.w; final itemHeight = _kGiftGridCardHeight.w; final gridWidth = constraints.maxWidth - horizontalPadding * 2 - crossAxisSpacing * (_kGiftGridColumnCount - 1); final itemWidth = (gridWidth / _kGiftGridColumnCount).clamp( 1.0, double.infinity, ); return GridView.builder( padding: EdgeInsets.symmetric( horizontal: horizontalPadding, vertical: verticalPadding, ), physics: const NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: _kGiftGridColumnCount, childAspectRatio: itemWidth / itemHeight, mainAxisSpacing: mainAxisSpacing, crossAxisSpacing: crossAxisSpacing, ), itemCount: itemCount, itemBuilder: itemBuilder, ); }, ); } Widget _buildGiftCoverLoading() { return Container( width: 48.w, height: 48.w, decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.w), color: Colors.white.withValues(alpha: 0.08), ), alignment: Alignment.center, child: SCRotatingDotsLoading(size: 24.w), ); } Widget _buildGiftCoverNoData() { return Container( width: 48.w, height: 48.w, decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.w), color: Colors.white.withValues(alpha: 0.08), ), clipBehavior: Clip.antiAlias, child: Image.asset( "sc_images/general/sc_icon_loading.png", fit: BoxFit.cover, ), ); } Widget _bagItem(SocialChatGiftRes gift, SCAppGeneralManager ref) { return gift.id == "-1000" ? GestureDetector( behavior: HitTestBehavior.opaque, child: Stack( children: [ Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.w), border: Border.all(color: Colors.transparent, width: 1.w), ), child: Padding( padding: EdgeInsets.symmetric( vertical: _kGiftCardVerticalPadding.w, ), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Image.asset( _strategy.getGiftPageCustomizedRuleIcon(), fit: BoxFit.cover, width: 48.w, height: 48.w, ), SizedBox(height: _kGiftIconPriceSpacing.w), Container( height: 12.w, margin: EdgeInsets.symmetric(horizontal: 3.w), alignment: Alignment.center, child: text( SCAppLocalizations.of(context)!.rulesUpload, maxLines: 1, fontSize: 10, letterSpacing: 0.1, lineHeight: 1, textAlign: TextAlign.center, fontWeight: FontWeight.w600, textColor: Colors.white, ), ), ], ), ), ), ], ), onTap: () { SmartDialog.show( tag: "showCustomizedRule", alignment: Alignment.bottomCenter, animationType: SmartAnimationType.fade, builder: (_) { return SafeArea( top: false, child: ClipRect( child: BackdropFilter( filter: ui.ImageFilter.blur(sigmaX: 15, sigmaY: 15), child: Stack( alignment: Alignment.topCenter, children: [ Container( height: ScreenUtil().screenHeight * 0.7, decoration: BoxDecoration( color: Colors.black54, borderRadius: BorderRadius.only( topLeft: Radius.circular(12.w), topRight: Radius.circular(12.w), ), ), child: Column( children: [ SizedBox(height: 10.w), text( SCAppLocalizations.of( context, )!.customizedGiftRules, textColor: Colors.white, fontSize: 14.sp, fontWeight: FontWeight.w600, ), SizedBox(height: 7.w), Expanded( child: SingleChildScrollView( child: Container( margin: EdgeInsets.symmetric( horizontal: 12.w, ), child: Text( SCAppLocalizations.of( context, )!.customizedGiftRulesContent, style: TextStyle( color: Colors.white, fontSize: 12.sp, fontWeight: FontWeight.w600, ), ), ), ), ), SizedBox(height: 10.w), ], ), ), ], ), ), ), ); }, ); }, ) : GestureDetector( behavior: HitTestBehavior.opaque, child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.w), border: Border.all( color: checkedIndex == ref.giftByTab[widget.type]!.indexOf(gift) ? SocialChatTheme.primaryLight : Colors.transparent, width: 1.w, ), ), child: Padding( padding: EdgeInsets.symmetric( vertical: _kGiftCardVerticalPadding.w, ), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ netImage( url: gift.giftPhoto ?? "", fit: BoxFit.cover, width: 48.w, height: 48.w, loadingWidget: _buildGiftCoverLoading(), errorWidget: _buildGiftCoverNoData(), ), SizedBox(height: _kGiftIconPriceSpacing.w), SizedBox( height: _kGiftPriceRowHeight.w, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: _isBackpackTab ? [ text( "x${_formatBackpackQuantity(gift)}", fontSize: 10, lineHeight: 1, fontWeight: FontWeight.w600, textColor: widget.isDark ? SocialChatTheme.primaryLight : SocialChatTheme.primaryColor, ), ] : [ Image.asset( _strategy.getGiftPageGoldCoinIcon(), width: 14.w, height: 14.w, ), SizedBox(width: 3.w), text( "${gift.giftCandy}", fontSize: 10, lineHeight: 1, textColor: widget.isDark ? Colors.white : Colors.black, ), ], ), ), ], ), ), ), onTap: () { setState(() { checkedIndex = ref.giftByTab[widget.type]!.indexOf(gift); widget.checkedCall(gift); }); }, ); } bool get _isBackpackTab => widget.type == SCAppGeneralManager.backpackGiftTab; String _formatBackpackQuantity(SocialChatGiftRes gift) { final quantity = gift.backpackQuantity; if (quantity != null) { if (quantity % 1 == 0) { return quantity.toInt().toString(); } return quantity.toString(); } return gift.quantity ?? '0'; } _indicator(SCAppGeneralManager ref) { var size = (ref.giftByTab[widget.type]!.length) / 8; var list = []; for (int i = 0; i < size; i++) { list.add( Container( width: 6.5.w, height: 6.5.w, margin: EdgeInsets.symmetric(horizontal: 4.w), decoration: BoxDecoration( shape: BoxShape.circle, color: _index == i ? SocialChatTheme.primaryColor : Color(0xffDADADA), ), ), ); } return Row(mainAxisAlignment: MainAxisAlignment.center, children: list); } }