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';
|
|
/// 首页游戏卡片
|
class GameCard extends StatelessWidget {
|
final Game game;
|
const GameCard({super.key, required this.game});
|
|
@override
|
Widget build(BuildContext context) {
|
return GestureDetector(
|
onTap: () => _openDetail(context),
|
child: MouseRegion(
|
cursor: SystemMouseCursors.click,
|
child: Container(
|
decoration: BoxDecoration(
|
color: AppColors.bgLight,
|
borderRadius: BorderRadius.circular(6),
|
),
|
child: Column(
|
crossAxisAlignment: CrossAxisAlignment.start,
|
children: [
|
// 封面
|
AspectRatio(
|
aspectRatio: 1.42,
|
child: Stack(
|
fit: StackFit.expand,
|
children: [
|
ClipRRect(
|
borderRadius: const BorderRadius.vertical(
|
top: Radius.circular(6)),
|
child: Image.network(
|
ApiClient.baseUrl + game.cover,
|
fit: BoxFit.cover,
|
errorBuilder: (_, __, ___) => Container(
|
color: const Color(0xFF2A2E45),
|
child: Center(
|
child: Text(
|
game.title.characters.first,
|
style: const TextStyle(
|
color: AppColors.textDim, fontSize: 32),
|
),
|
),
|
),
|
loadingBuilder: (context, child, progress) {
|
if (progress == null) return child;
|
return Container(
|
color: const Color(0xFF2A2E45),
|
child: const Center(
|
child: SizedBox(
|
width: 20,
|
height: 20,
|
child: CircularProgressIndicator(
|
strokeWidth: 2,
|
color: AppColors.accent),
|
),
|
),
|
);
|
},
|
),
|
),
|
// PC资源 标签
|
Positioned(
|
left: 0,
|
top: 8,
|
child: Container(
|
padding: const EdgeInsets.symmetric(
|
horizontal: 8, vertical: 3),
|
decoration: const BoxDecoration(
|
color: AppColors.accent,
|
borderRadius:
|
BorderRadius.horizontal(right: Radius.circular(3)),
|
),
|
child: Text(game.platformLabel,
|
style: const TextStyle(
|
color: Colors.white, fontSize: 11)),
|
),
|
),
|
// 大小标签
|
Positioned(
|
right: 6,
|
top: 8,
|
child: Container(
|
padding: const EdgeInsets.symmetric(
|
horizontal: 6, vertical: 2),
|
decoration: BoxDecoration(
|
color: Colors.black.withOpacity(0.6),
|
borderRadius: BorderRadius.circular(3),
|
),
|
child: Text(game.size,
|
style: const TextStyle(
|
color: Colors.white, fontSize: 11)),
|
),
|
),
|
],
|
),
|
),
|
// 信息区
|
Expanded(
|
child: Padding(
|
padding: const EdgeInsets.fromLTRB(10, 8, 10, 8),
|
child: Column(
|
crossAxisAlignment: CrossAxisAlignment.start,
|
children: [
|
if (game.versionNote.isNotEmpty)
|
Padding(
|
padding: const EdgeInsets.only(bottom: 2),
|
child: Text(
|
game.versionNote,
|
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
style: const TextStyle(
|
color: AppColors.textDim, fontSize: 11),
|
),
|
),
|
Text(
|
game.title,
|
maxLines: 2,
|
overflow: TextOverflow.ellipsis,
|
style: const TextStyle(
|
color: Colors.white, fontSize: 13, height: 1.3),
|
),
|
const Spacer(),
|
Row(
|
children: [
|
Expanded(
|
child: Text(
|
game.tags.split(',').first,
|
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
style: const TextStyle(
|
color: AppColors.textDim, fontSize: 11),
|
),
|
),
|
_AddButton(game: game),
|
],
|
),
|
],
|
),
|
),
|
),
|
],
|
),
|
),
|
),
|
);
|
}
|
|
void _openDetail(BuildContext context) {
|
// 通过主Shell打开详情页,保留首页顶部导航栏
|
ShellNav.instance.openDetail?.call(game.id);
|
}
|
}
|
|
class _AddButton extends StatefulWidget {
|
final Game game;
|
const _AddButton({required this.game});
|
@override
|
State<_AddButton> createState() => _AddButtonState();
|
}
|
|
class _AddButtonState extends State<_AddButton> {
|
bool added = false;
|
|
@override
|
void initState() {
|
super.initState();
|
_check();
|
}
|
|
Future<void> _check() async {
|
try {
|
final res = await ApiService.gameDetail(widget.game.id);
|
if (res['code'] == 0 && res['data']['in_library'] == true) {
|
if (mounted) setState(() => added = true);
|
}
|
} catch (_) {}
|
}
|
|
Future<void> _add() async {
|
if (added) return;
|
final res = await ApiService.addToLibrary(widget.game.id);
|
if (res['code'] == 0 && mounted) {
|
setState(() => added = true);
|
_toast('已加入资源库');
|
} else if (mounted) {
|
_toast(res['msg'] ?? '添加失败', error: true);
|
}
|
}
|
|
void _toast(String msg, {bool error = false}) {
|
final overlay = Overlay.of(context);
|
final entry = OverlayEntry(
|
builder: (context) => Positioned(
|
top: 60,
|
left: MediaQuery.of(context).size.width / 2 - 140,
|
child: Material(
|
color: Colors.transparent,
|
child: Container(
|
width: 280,
|
padding:
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
decoration: BoxDecoration(
|
color: error ? AppColors.red : AppColors.green,
|
borderRadius: BorderRadius.circular(6),
|
),
|
child: Row(
|
children: [
|
Icon(error ? Icons.error_outline : Icons.check_circle,
|
color: Colors.white, size: 18),
|
const SizedBox(width: 8),
|
Expanded(
|
child: Text(msg,
|
style: const TextStyle(
|
color: Colors.white, fontSize: 13)),
|
),
|
],
|
),
|
),
|
),
|
),
|
);
|
overlay.insert(entry);
|
Future.delayed(const Duration(seconds: 2), () => entry.remove());
|
}
|
|
@override
|
Widget build(BuildContext context) {
|
return GestureDetector(
|
onTap: _add,
|
child: Container(
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 4),
|
decoration: BoxDecoration(
|
color: added ? const Color(0xFF3A3F52) : AppColors.accent,
|
borderRadius: BorderRadius.circular(3),
|
),
|
child: Text(
|
added ? '已添加' : '添加',
|
style: const TextStyle(color: Colors.white, fontSize: 11),
|
),
|
),
|
);
|
}
|
}
|