905086
2026-11-07 7c8e509c95c2af0c0d076b13fbbc5aa63a06cb69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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]),
                        );
                      }),
          ),
        ],
      ),
    );
  }
}