import 'package:flutter/material.dart'; import 'package:fluttergamebox/api/api_client.dart'; import 'package:fluttergamebox/api/api_service.dart'; import 'package:fluttergamebox/common/theme.dart'; import 'package:fluttergamebox/widgets/game_card.dart'; class FavoritesPage extends StatefulWidget { const FavoritesPage({super.key}); @override State createState() => FavoritesPageState(); } class FavoritesPageState extends State with AutomaticKeepAliveClientMixin { List games = []; bool _loading = true; @override bool get wantKeepAlive => true; @override void initState() { super.initState(); _load(); } Future _load() async { final g = await ApiService.favorites(); if (!mounted) return; setState(() { games = g; _loading = false; }); } /// 供主Shell刷新(切换到收藏页/关闭详情页时重新拉取) void refresh() => _load(); @override Widget build(BuildContext context) { super.build(context); return Container( color: AppColors.bg, padding: const EdgeInsets.fromLTRB(70, 20, 70, 0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ const Icon(Icons.star, color: AppColors.yellow, size: 18), const SizedBox(width: 8), const Text('我的收藏', style: TextStyle( color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500)), const SizedBox(width: 12), Text('共 ${games.length} 款', style: const TextStyle( color: AppColors.textGray, fontSize: 13)), ], ), const SizedBox(height: 16), Expanded( child: _loading ? const Center( child: CircularProgressIndicator(color: AppColors.accent)) : games.isEmpty ? const Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.star_border, size: 56, color: AppColors.textDim), SizedBox(height: 12), Text('暂无收藏游戏', style: TextStyle( color: AppColors.textGray, fontSize: 14)), ], ), ) : GridView.builder( padding: const EdgeInsets.only(bottom: 40), gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 4, mainAxisSpacing: 18, crossAxisSpacing: 16, childAspectRatio: 0.62, ), itemCount: games.length, itemBuilder: (context, i) => GameCard(game: games[i]), ), ), ], ), ); } }