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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import 'dart:async';
 
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/pages/main_shell.dart';
import 'package:fluttergamebox/widgets/game_card.dart';
 
class StorePage extends StatefulWidget {
  const StorePage({super.key});
  @override
  State<StorePage> createState() => StorePageState();
}
 
class StorePageState extends State<StorePage>
    with AutomaticKeepAliveClientMixin {
  List<Game> banners = [];
  List<Game> games = [];
  List<Category> categories = [];
  int bannerIndex = 0;
  Timer? _bannerTimer;
 
  String platform = 'all'; // all / pc / switch
  int category = 0;
  String keyword = '';
  String sort = 'updated';
  final _searchCtrl = TextEditingController();
  bool _loading = true;
 
  @override
  bool get wantKeepAlive => true;
 
  @override
  void initState() {
    super.initState();
    _load();
  }
 
  @override
  void dispose() {
    _bannerTimer?.cancel();
    super.dispose();
  }
 
  Future<void> _load() async {
    final b = await ApiService.banners();
    final c = await ApiService.categories();
    if (!mounted) return;
    setState(() {
      banners = b;
      categories = c.where((e) => e.name != '常用工具').toList();
    });
    _bannerTimer?.cancel();
    if (banners.length > 1) {
      _bannerTimer = Timer.periodic(const Duration(seconds: 6), (_) {
        if (mounted) {
          setState(() => bannerIndex = (bannerIndex + 1) % banners.length);
        }
      });
    }
    await _reloadGames();
  }
 
  Future<void> _reloadGames() async {
    setState(() => _loading = true);
    final g = await ApiService.games(
        platform: platform,
        category: category,
        keyword: keyword,
        sort: sort);
    if (mounted) {
      setState(() {
        games = g;
        _loading = false;
      });
    }
  }
 
  /// 供主Shell在详情页关闭后刷新(更新"添加"按钮状态等)
  void refresh() => _reloadGames();
 
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      color: AppColors.bg,
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (banners.isNotEmpty) _buildBanner(),
            _buildFilters(),
            _buildGrid(),
            const SizedBox(height: 40),
          ],
        ),
      ),
    );
  }
 
  Widget _buildBanner() {
    final game = banners[bannerIndex];
    return Padding(
      padding: const EdgeInsets.fromLTRB(70, 12, 70, 0),
      child: Column(
        children: [
          GestureDetector(
            onTap: () => _openDetail(game),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: SizedBox(
                height: 280,
                width: double.infinity,
                child: Stack(
                  fit: StackFit.expand,
                  children: [
                    Image.network(
                      ApiClient.baseUrl + game.cover,
                      fit: BoxFit.cover,
                      errorBuilder: (_, __, ___) => Container(
                        color: const Color(0xFF2A2E45),
                        child: Center(
                          child: Text(game.title,
                              style: const TextStyle(
                                  color: AppColors.textDim, fontSize: 24)),
                        ),
                      ),
                    ),
                    Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.centerLeft,
                          end: Alignment.center,
                          colors: [
                            Colors.black.withOpacity(0.75),
                            Colors.transparent,
                          ],
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(30),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const Spacer(),
                          SizedBox(
                            width: 420,
                            child: Text(game.title,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(
                                    color: Colors.white,
                                    fontSize: 26,
                                    fontWeight: FontWeight.bold,
                                    shadows: [
                                      Shadow(color: Colors.black, blurRadius: 8)
                                    ])),
                          ),
                          const SizedBox(height: 10),
                          Text(game.versionNote,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                  color: Colors.white70, fontSize: 13)),
                          const SizedBox(height: 14),
                          Row(
                            children: [
                              _bannerTag(game.platformLabel),
                              const SizedBox(width: 8),
                              _bannerTag(game.size),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          const SizedBox(height: 8),
          Row(
            children: [
              for (var i = 0; i < banners.length; i++)
                GestureDetector(
                  onTap: () => setState(() => bannerIndex = i),
                  child: Container(
                    width: 110,
                    margin: const EdgeInsets.only(right: 8),
                    padding: const EdgeInsets.symmetric(
                        horizontal: 8, vertical: 6),
                    decoration: BoxDecoration(
                      color: i == bannerIndex
                          ? AppColors.bgLight
                          : Colors.transparent,
                      borderRadius: BorderRadius.circular(4),
                      border: Border(
                        bottom: BorderSide(
                          width: 2,
                          color: i == bannerIndex
                              ? AppColors.accent
                              : Colors.transparent,
                        ),
                      ),
                    ),
                    child: Text(banners[i].title,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            color: i == bannerIndex
                                ? Colors.white
                                : AppColors.textDim,
                            fontSize: 12)),
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }
 
  Widget _bannerTag(String text) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
      decoration: BoxDecoration(
        color: AppColors.accent,
        borderRadius: BorderRadius.circular(3),
      ),
      child: Text(text,
          style: const TextStyle(color: Colors.white, fontSize: 11)),
    );
  }
 
  Widget _buildFilters() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(70, 16, 70, 12),
      child: Column(
        children: [
          Row(
            children: [
              _tab('全部资源', platform == 'all'),
              const SizedBox(width: 20),
              _tab('单机资源', platform == 'pc'),
              const SizedBox(width: 20),
              _tab('Switch资源', platform == 'switch'),
              const Spacer(),
              _CategoryDropdown(
                categories: categories,
                value: category,
                onChanged: (v) {
                  setState(() => category = v ?? 0);
                  _reloadGames();
                },
              ),
              const SizedBox(width: 10),
              SizedBox(
                width: 200,
                height: 32,
                child: TextField(
                  controller: _searchCtrl,
                  onSubmitted: (v) {
                    keyword = v;
                    _reloadGames();
                  },
                  style: const TextStyle(color: Colors.white, fontSize: 13),
                  decoration: InputDecoration(
                    hintText: '输入关键字',
                    hintStyle: const TextStyle(
                        color: AppColors.textDim, fontSize: 12),
                    prefixIcon: const Icon(Icons.search,
                        size: 16, color: AppColors.textGray),
                    filled: true,
                    fillColor: AppColors.bgLight,
                    contentPadding: EdgeInsets.zero,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(4),
                      borderSide: BorderSide.none,
                    ),
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 10),
          Row(
            children: [
              _sortItem('最新更新', 'updated', Icons.update),
              const SizedBox(width: 24),
              _sortItem('安装排行', 'rank', Icons.bar_chart),
              const SizedBox(width: 24),
              _sortItem('安装大小', 'size', Icons.sd_storage),
              const SizedBox(width: 24),
              _sortItem('热门大作', 'hot', Icons.local_fire_department),
            ],
          ),
        ],
      ),
    );
  }
 
  Widget _tab(String label, bool active) {
    return GestureDetector(
      onTap: () {
        setState(() {
          platform = label == '单机资源'
              ? 'pc'
              : label == 'Switch资源'
                  ? 'switch'
                  : 'all';
        });
        _reloadGames();
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 6),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
                width: 2,
                color: active ? AppColors.accent : Colors.transparent),
          ),
        ),
        child: Text(label,
            style: TextStyle(
                color: active ? Colors.white : AppColors.textGray,
                fontSize: 14,
                fontWeight: active ? FontWeight.w500 : FontWeight.normal)),
      ),
    );
  }
 
  Widget _sortItem(String label, String key, IconData icon) {
    final active = sort == key;
    return GestureDetector(
      onTap: () {
        setState(() => sort = key);
        _reloadGames();
      },
      child: Row(
        children: [
          Icon(icon,
              size: 14,
              color: active ? AppColors.accent : AppColors.textDim),
          const SizedBox(width: 4),
          Text(label,
              style: TextStyle(
                  color: active ? AppColors.accent : AppColors.textDim,
                  fontSize: 12)),
        ],
      ),
    );
  }
 
  Widget _buildGrid() {
    if (_loading) {
      return const SizedBox(
        height: 300,
        child: Center(
          child: CircularProgressIndicator(color: AppColors.accent),
        ),
      );
    }
    if (games.isEmpty) {
      return const SizedBox(
        height: 300,
        child: Center(
          child: Text('暂无游戏资源',
              style: TextStyle(color: AppColors.textGray)),
        ),
      );
    }
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 70),
      child: LayoutBuilder(builder: (context, constraints) {
        // 卡片高度 = 封面高度 + 信息区实际内容高度,避免信息区出现大段空白
        const crossAxisSpacing = 16.0;
        final cardWidth =
            (constraints.maxWidth - crossAxisSpacing * 3) / 4;
        final coverHeight = cardWidth / 1.42; // 与 GameCard 封面 AspectRatio 一致
        const infoHeight = 96.0; // 版本行 + 标题两行 + 类别/添加按钮行 + 内边距
        return GridView.builder(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
            mainAxisSpacing: 18,
            crossAxisSpacing: crossAxisSpacing,
            mainAxisExtent: coverHeight + infoHeight,
          ),
          itemCount: games.length,
          itemBuilder: (context, i) => GameCard(game: games[i]),
        );
      }),
    );
  }
 
  void _openDetail(Game game) {
    // 通过主Shell打开详情页,保留首页顶部导航栏
    ShellNav.instance.openDetail?.call(game.id);
  }
}
 
class _CategoryDropdown extends StatelessWidget {
  final List<Category> categories;
  final int value;
  final ValueChanged<int?> onChanged;
  const _CategoryDropdown({
    required this.categories,
    required this.value,
    required this.onChanged,
  });
 
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 32,
      padding: const EdgeInsets.symmetric(horizontal: 12),
      decoration: BoxDecoration(
        color: AppColors.bgLight,
        borderRadius: BorderRadius.circular(4),
      ),
      child: DropdownButtonHideUnderline(
        child: DropdownButton<int>(
          value: value,
          dropdownColor: const Color(0xFF232739),
          style: const TextStyle(color: AppColors.textWhite, fontSize: 13),
          icon: const Icon(Icons.arrow_drop_down,
              size: 18, color: AppColors.textGray),
          items: [
            const DropdownMenuItem(
                value: 0,
                child: Text('全部类别',
                    style: TextStyle(color: AppColors.textGray))),
            ...categories
                .map((c) => DropdownMenuItem(value: c.id, child: Text(c.name))),
          ],
          onChanged: onChanged,
        ),
      ),
    );
  }
}