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,
|
),
|
),
|
);
|
}
|
}
|