import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import '../../../data/models/goods.dart'; import '../../../data/models/user_Info.dart'; import '../controllers/goodsvip_controller.dart'; import '../utils/goods_i18n.dart'; import 'widgets/goods_footer_section.dart'; class GoodsvipView extends GetView { const GoodsvipView({super.key}); @override Widget build(BuildContext context) { final isDarkMode = Theme.of(context).brightness == Brightness.dark; return Scaffold( backgroundColor: isDarkMode ? Colors.black : Colors.grey[50], appBar: AppBar( backgroundColor: isDarkMode ? Colors.black : Colors.white, foregroundColor: Colors.white, elevation: 0.5, leading: IconButton( icon: Icon( Icons.arrow_back_ios, size: 20.sp, color: isDarkMode ? Colors.white : Colors.black87, ), onPressed: () => Get.back(), ), title: Text( _pageTitle, style: TextStyle( fontSize: 18.sp, fontWeight: FontWeight.w600, color: isDarkMode ? Colors.white : Colors.black, ), ), centerTitle: true, ), body: SafeArea( top: false, child: Obx(() { final userTick = User.refreshTick.value; Widget content; if (controller.loading.value) { content = const Center(child: CircularProgressIndicator()); } else if (controller.error.isNotEmpty) { content = Center(child: Text(controller.error.value)); } else { final focused = _tier != null; final source = focused ? controller.items.where(_matchesTier).toList() : controller.items .where((g) => g.usagetype == UsageType.Vip) .toList(); final Map> grouped = {}; for (final g in source) { grouped.putIfAbsent(g.usagetype, () => []).add(g); } // 会员页只放月 / 季 / 终生三档。点某一档进来时只留那一档。 final order = [UsageType.Vip]; final children = []; if (!focused) { if (controller.vipExpiringSoon) { children.add(_buildWarnBanner( isDarkMode, 'vipExpiringSoon' .trParams({'days': '${controller.vipDaysLeft}'}), )); children.add(SizedBox(height: 12.h)); } if (controller.computeLow) { children.add(_buildWarnBanner(isDarkMode, 'computeLow'.tr)); children.add(SizedBox(height: 12.h)); } } for (int i = 0; i < order.length; i++) { final t = order[i]; final list = grouped[t]; if (list == null || list.isEmpty) continue; children.add(_buildTypeSection(t, list, isDarkMode)); if (i != order.length - 1) { children.add(SizedBox(height: 16.h)); children.add(Divider( color: isDarkMode ? Colors.white24 : Colors.black12)); children.add(SizedBox(height: 16.h)); } } if (!focused) { children.add(GoodsFooterSection(isDarkMode: isDarkMode)); } content = ListView( key: ValueKey(userTick), padding: EdgeInsets.all(16.w), children: children, ); } return Stack( children: [ content, if (controller.isPaying.value) Positioned.fill( child: AbsorbPointer( absorbing: true, child: Container( color: Colors.black.withOpacity(0.35), child: const Center( child: CircularProgressIndicator(), ), ), ), ), ], ); }), ), ); } /// 进页时记下的档位。不要改回读 Get.arguments。 String? get _tier => controller.vipTier; String get _pageTitle { switch (_tier) { case 'month': return 'vipMonth'.tr; case 'quarter': return 'vipQuarter'.tr; case 'lifetime': return 'vipLifetime'.tr; default: return 'vipRecharge'.tr; } } /// 月度约 30 天、季度约 90 天、终生按一年以上。天数对不上时再看商品名。 bool _matchesTier(DBGoods g) { if (g.usagetype != UsageType.Vip) return false; final tier = _tier; if (g.viptime > 0) { switch (tier) { case 'month': return g.viptime >= 28 && g.viptime <= 31; case 'quarter': return g.viptime >= 70 && g.viptime <= 120; case 'lifetime': return g.viptime >= 365; } } final blob = '${g.id} ${g.name} ${g.localname}'.toLowerCase(); switch (tier) { case 'month': return blob.contains('month') || blob.contains('月度'); case 'quarter': return blob.contains('quarter') || blob.contains('季'); case 'lifetime': return blob.contains('life') || blob.contains('终'); default: return false; } } /// 到期 / 算力不足的提示条。刻意做成温和的黄色而不是红色错误样式—— /// 这是「该续费了」的提醒,不是故障;下面就是购买入口,不需要再额外引导。 Widget _buildWarnBanner(bool isDarkMode, String text) { return Container( width: double.infinity, padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h), decoration: BoxDecoration( color: isDarkMode ? const Color(0xFF3A2F16) : const Color(0xFFFFF7E6), borderRadius: BorderRadius.circular(12.r), border: Border.all( color: isDarkMode ? const Color(0xFF6B551F) : const Color(0xFFFFE0A3), ), ), child: Row( children: [ Icon(Icons.info_outline, size: 18.sp, color: isDarkMode ? const Color(0xFFE0B860) : const Color(0xFFB7791F)), SizedBox(width: 10.w), Expanded( child: Text( text, style: TextStyle( fontSize: 13.sp, color: isDarkMode ? const Color(0xFFE8D5A8) : const Color(0xFF8A6116), ), ), ), ], ), ); } Widget _buildTypeHeader(UsageType type, bool isDarkMode) { final titles = { UsageType.AI: 'aiFunction'.tr, UsageType.Meet: 'conferenceAssistant'.tr, UsageType.Translation: 'simultaneousTranslation'.tr, UsageType.Vip: 'memberService'.tr, UsageType.Compute: 'computePack'.tr, }; final subColor = isDarkMode ? Colors.white70 : Colors.grey[700]; String titleStr = titles[type] ?? type.name; String? vipSubtitle; if (type == UsageType.Compute) { // 算力包分区:副标题显示当前余额(服务端已把设备赠送与账户余额拆好) vipSubtitle = controller.computeSummary; } if (type == UsageType.Vip) { vipSubtitle = _vipExpiryLine(withDeviceSource: true); } return Container( padding: EdgeInsets.all(16.w), decoration: BoxDecoration( color: isDarkMode ? Colors.grey[900] : Colors.white, borderRadius: BorderRadius.circular(12.r), boxShadow: [ BoxShadow( color: isDarkMode ? Colors.black.withOpacity(0.2) : Colors.black.withOpacity(0.05), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Container( // width: 44.w, // height: 44.w, // decoration: BoxDecoration( // color: iconAndColor.background, // borderRadius: BorderRadius.circular(22.r), // ), // child: Icon(iconAndColor.icon, color: iconAndColor.foreground), // ), // SizedBox(width: 12.w), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( titleStr, style: TextStyle( fontSize: 18.sp, fontWeight: FontWeight.w700, color: isDarkMode ? Colors.white : Colors.black, ), ), SizedBox(height: 8.h), // 会员与算力(user_getcompute):服务端已合并自购与设备赋予,这里只展示 if (type == UsageType.Vip && controller.computeSummary != null) ...[ Text( controller.computeSummary!, style: TextStyle(fontSize: 12.sp, color: subColor), ), SizedBox(height: 4.h), ], if (type == UsageType.Vip && vipSubtitle != null) Text( vipSubtitle, style: TextStyle(fontSize: 12.sp, color: subColor), ) else _buildMetricsInline(type, isDarkMode), ], ), ), ], ), ); } Widget _buildTypeSection( UsageType type, List list, bool isDarkMode) { // 三档同页时,到期和算力写在各自的卡片里,不再在上面放一条共用的会员说明。 if (type == UsageType.Vip && _tier == null) { return _buildGoodsGrid(list, type, isDarkMode); } return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildTypeHeader(type, isDarkMode), SizedBox(height: 8.h), _buildGoodsGrid(list, type, isDarkMode), ], ); } Widget _buildMetricsInline(UsageType type, bool isDarkMode) { final textColor = isDarkMode ? Colors.white : Colors.black; final subColor = isDarkMode ? Colors.white70 : Colors.grey[700]; if (type == UsageType.Vip) { int viplv = 0; if (User.isLoggedIn()) { viplv = User.instance.viplv; } return Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Lv.$viplv', style: TextStyle( fontSize: 14.sp, fontWeight: FontWeight.w600, color: textColor, ), ), SizedBox(height: 4.h), Text( _vipExpiryLine(labelKey: 'validityPeriod'), style: TextStyle(fontSize: 12.sp, color: subColor), ), ], ), ), // Container( // padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 4.h), // decoration: BoxDecoration( // color: isDarkMode ? Colors.white10 : Colors.black12, // borderRadius: BorderRadius.circular(16.r), // ), // child: Text( // viplv > 0 ? 'enabled'.tr : 'disabled'.tr, // style: TextStyle(fontSize: 11.sp, color: subColor), // ), // ), ], ); } int total = 0; int remain = 0; if (User.isLoggedIn()) { final u = User.instance; if (type == UsageType.AI) { total = u.aichattotalintegral; remain = u.aichatintegral; } else if (type == UsageType.Meet) { total = u.meettotalintegral; remain = u.meetintegral; } else if (type == UsageType.Translation) { total = u.tradetotalintegral; remain = u.tradeintegral; } } final progress = (total > 0) ? (remain / total).clamp(0.0, 1.0) : 0.0; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ LinearProgressIndicator( value: progress, minHeight: 8.h, backgroundColor: isDarkMode ? Colors.grey[800] : Colors.grey[300], valueColor: AlwaysStoppedAnimation( isDarkMode ? Colors.blue[300]! : Colors.blue[600]!), ), SizedBox(height: 8.h), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( '${'totalQuota'.tr}:$total', style: TextStyle(fontSize: 12.sp, color: subColor), ), Text( '${'remaining'.tr}:$remain', style: TextStyle(fontSize: 12.sp, color: subColor), ), ], ), ], ); } String? _tierKind(DBGoods g) { if (g.usagetype != UsageType.Vip) return null; if (g.viptime > 0) { if (g.viptime >= 28 && g.viptime <= 31) return 'month'; if (g.viptime >= 70 && g.viptime <= 120) return 'quarter'; if (g.viptime >= 365) return 'lifetime'; } final blob = '${g.id} ${g.name} ${g.localname}'.toLowerCase(); if (blob.contains('month') || blob.contains('月度')) return 'month'; if (blob.contains('quarter') || blob.contains('季')) return 'quarter'; if (blob.contains('life') || blob.contains('终')) return 'lifetime'; return null; } Widget _buildGoodsGrid(List list, UsageType type, bool isDarkMode) { if (type == UsageType.Vip && _tier == null) { final ordered = [...list]..sort((a, b) { int rank(DBGoods g) { switch (_tierKind(g)) { case 'month': return 0; case 'quarter': return 1; case 'lifetime': return 2; default: return 3; } } return rank(a).compareTo(rank(b)); }); return Column( children: [ for (final item in ordered) ...[ _buildVipOfferCard(item, isDarkMode), SizedBox(height: 12.h), ], ], ); } return GridView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: _tier != null ? 1 : 2, mainAxisSpacing: 8.h, crossAxisSpacing: 12.w, childAspectRatio: _tier != null ? 2.8 : 1.3, ), itemCount: list.length, itemBuilder: (_, i) => _buildGridItemCard(list[i], type, isDarkMode), ); } Widget _buildVipOfferCard(DBGoods item, bool isDarkMode) { final kind = _tierKind(item) ?? ''; final price = goodsDisplayPrice(item); final title = vipOfferTitle(kind, price); final zh = (Get.locale?.languageCode ?? 'zh') == 'zh'; final facts = vipOfferFacts(item); final subColor = isDarkMode ? Colors.white70 : Colors.grey[700]; return Container( width: double.infinity, padding: EdgeInsets.all(16.w), decoration: BoxDecoration( color: isDarkMode ? Colors.grey[900] : Colors.white, borderRadius: BorderRadius.circular(12.r), boxShadow: [ BoxShadow( color: isDarkMode ? Colors.black.withOpacity(0.2) : Colors.black.withOpacity(0.05), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title, style: TextStyle( fontSize: 17.sp, fontWeight: FontWeight.w700, color: isDarkMode ? Colors.white : Colors.black, ), ), SizedBox(height: 10.h), Text( zh ? '会员服务说明' : 'Membership details', style: TextStyle( fontSize: 13.sp, fontWeight: FontWeight.w600, color: isDarkMode ? Colors.white : Colors.black87, ), ), SizedBox(height: 6.h), for (final line in facts) Padding( padding: EdgeInsets.only(bottom: 4.h), child: Text( line, style: TextStyle(fontSize: 14.sp, height: 1.4, color: subColor), ), ), SizedBox(height: 12.h), Align( alignment: Alignment.centerRight, child: GestureDetector( onTap: () => controller.purchase(item), child: Container( padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.h), decoration: BoxDecoration( color: Colors.blue[600], borderRadius: BorderRadius.circular(8.r), ), child: Text( 'purchase'.tr, style: TextStyle(fontSize: 13.sp, color: Colors.white), ), ), ), ), ], ), ); } Widget _buildGridItemCard(DBGoods item, UsageType type, bool isDarkMode) { final iconColors = _iconColorForType(type, isDarkMode); return Container( padding: EdgeInsets.all(12.w), decoration: BoxDecoration( color: isDarkMode ? Colors.grey[900] : Colors.white, borderRadius: BorderRadius.circular(12.r), boxShadow: [ BoxShadow( color: isDarkMode ? Colors.black.withOpacity(0.2) : Colors.black.withOpacity(0.05), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Container( width: 34.w, height: 20.w, decoration: BoxDecoration( color: iconColors.background, borderRadius: BorderRadius.circular(17.r), ), child: Icon(iconColors.icon, color: iconColors.foreground), ), SizedBox(width: 8.w), Expanded( child: Text( goodsDisplayPrice(item), maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 21.sp, fontWeight: FontWeight.w600, color: isDarkMode ? Colors.white : Colors.black, ), ), ), ], ), SizedBox(height: 6.h), Expanded( child: Text( localizedGoodsName(item), maxLines: 3, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 15.sp, color: isDarkMode ? Colors.white70 : Colors.grey[700], ), ), ), // SizedBox(height: 6.h), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ GestureDetector( onTap: () => controller.purchase(item), child: Container( padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.h), decoration: BoxDecoration( color: Colors.blue[600], borderRadius: BorderRadius.circular(8.r), ), child: Text( 'purchase'.tr, style: TextStyle(fontSize: 12.sp, color: Colors.white), ), ), ), ], ), ], ), ); } DateTime? _parseVipExpiry() { if (!User.isLoggedIn()) return null; var ts = User.instance.vipexptime; final computeVip = controller.compute.value?['vip']; if (computeVip is Map) { final exp = (computeVip['exptime'] as num?)?.toInt() ?? 0; if (exp > 0) ts = exp; } if (ts <= 0) return null; final expiry = ts > 1000000000000 ? DateTime.fromMillisecondsSinceEpoch(ts) : DateTime.fromMillisecondsSinceEpoch(ts * 1000); if (!expiry.isAfter(DateTime.now())) return null; return expiry; } /// 当前有效 VIP 按剩余天数分档:月度约 30 天,季度约 90 天,一年及以上当终生。 String? _activeVipTier(DateTime expiry) { final days = expiry.difference(DateTime.now()).inDays; if (days >= 300) return 'lifetime'; if (days >= 60) return 'quarter'; if (days > 0) return 'month'; return null; } /// 月度 / 季度页只显示对应档位的到期日,避免三条入口共用一条 vipexptime。 /// 终生不展示具体日期,固定为无限。 String _vipExpiryLine({ String labelKey = 'expiryLabel', bool withDeviceSource = false, }) { final suffix = withDeviceSource && controller.vipFromDevice ? ' · ${'vipFromDevice'.tr}' : ''; if (_tier == 'lifetime') { return '${labelKey.tr}${'unlimited'.tr}$suffix'; } final expiry = _parseVipExpiry(); if (expiry == null) return 'notActivated'.tr; final kind = _activeVipTier(expiry); if (kind == 'lifetime') { return '${labelKey.tr}${'unlimited'.tr}$suffix'; } if (_tier != null && kind != _tier) { return 'notActivated'.tr; } return '${labelKey.tr}${_fmtDate(expiry)}$suffix'; } String _fmtDate(DateTime d) { final y = d.year.toString().padLeft(4, '0'); final m = d.month.toString().padLeft(2, '0'); final day = d.day.toString().padLeft(2, '0'); return '$y-$m-$day'; } _IconColors _iconColorForType(UsageType type, bool isDarkMode) { switch (type) { case UsageType.AI: return _IconColors( icon: Icons.smart_toy, background: isDarkMode ? Colors.teal[900]!.withOpacity(0.3) : Colors.teal[100]!, foreground: isDarkMode ? Colors.teal[300]! : Colors.teal[600]!, ); case UsageType.Meet: return _IconColors( icon: Icons.people_alt, background: isDarkMode ? Colors.pink[900]!.withOpacity(0.3) : Colors.pink[100]!, foreground: isDarkMode ? Colors.pink[300]! : Colors.pink[600]!, ); case UsageType.Translation: return _IconColors( icon: Icons.translate, background: isDarkMode ? Colors.indigo[900]!.withOpacity(0.3) : Colors.indigo[100]!, foreground: isDarkMode ? Colors.indigo[300]! : Colors.indigo[600]!, ); case UsageType.Compute: return _IconColors( icon: Icons.bolt, background: isDarkMode ? Colors.amber[900]!.withOpacity(0.3) : Colors.amber[100]!, foreground: isDarkMode ? Colors.amber[300]! : Colors.amber[700]!, ); case UsageType.Vip: case UsageType.UsageNull: return _IconColors( icon: Icons.workspace_premium, background: isDarkMode ? Colors.amber[900]!.withOpacity(0.3) : Colors.amber[100]!, foreground: isDarkMode ? Colors.amber[300]! : Colors.amber[600]!, ); } } } class _IconColors { final IconData icon; final Color background; final Color foreground; const _IconColors({ required this.icon, required this.background, required this.foreground, }); }