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<FavoritesPage> createState() => FavoritesPageState();
|
}
|
|
class FavoritesPageState extends State<FavoritesPage>
|
with AutomaticKeepAliveClientMixin {
|
List<Game> games = [];
|
bool _loading = true;
|
|
@override
|
bool get wantKeepAlive => true;
|
|
@override
|
void initState() {
|
super.initState();
|
_load();
|
}
|
|
Future<void> _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)),
|
],
|
),
|
)
|
: LayoutBuilder(builder: (context, constraints) {
|
// 卡片高度 = 封面高度 + 信息区实际内容高度,避免出现大段空白
|
const crossAxisSpacing = 16.0;
|
final cardWidth =
|
(constraints.maxWidth - crossAxisSpacing * 3) / 4;
|
final coverHeight = cardWidth / 1.42;
|
const infoHeight = 96.0;
|
return GridView.builder(
|
padding: const EdgeInsets.only(bottom: 40),
|
gridDelegate:
|
SliverGridDelegateWithFixedCrossAxisCount(
|
crossAxisCount: 4,
|
mainAxisSpacing: 18,
|
crossAxisSpacing: crossAxisSpacing,
|
mainAxisExtent: coverHeight + infoHeight,
|
),
|
itemCount: games.length,
|
itemBuilder: (context, i) => GameCard(game: games[i]),
|
);
|
}),
|
),
|
],
|
),
|
);
|
}
|
}
|