From 262ce70998fa52dec3120589f51c47e96fb96df4 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Tue, 03 Nov 2026 17:04:19 +0800
Subject: [PATCH] feat: 完成游戏盒子前后端基础功能开发
---
fluttergamebox/lib/pages/login_page.dart | 431 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 431 insertions(+), 0 deletions(-)
diff --git a/fluttergamebox/lib/pages/login_page.dart b/fluttergamebox/lib/pages/login_page.dart
new file mode 100644
index 0000000..516063a
--- /dev/null
+++ b/fluttergamebox/lib/pages/login_page.dart
@@ -0,0 +1,431 @@
+import 'dart:convert';
+
+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/store/app_state.dart';
+import 'package:fluttergamebox/widgets/auth_dialogs.dart';
+import 'package:fluttergamebox/widgets/window_controls.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+class LoginPage extends StatefulWidget {
+ const LoginPage({super.key});
+ @override
+ State<LoginPage> createState() => _LoginPageState();
+}
+
+class _LoginPageState extends State<LoginPage> {
+ final _phoneCtrl = TextEditingController();
+ final _pwdCtrl = TextEditingController();
+ bool _loading = false;
+ String? _error;
+
+ // 检查更新提示
+ void _checkUpdate() {
+ showToast(context, '当前已是最新版本!');
+ }
+
+ Future<void> _login() async {
+ final phone = _phoneCtrl.text.trim();
+ final pwd = _pwdCtrl.text;
+ if (phone.isEmpty || pwd.isEmpty) {
+ setState(() => _error = '请输入账号和密码');
+ return;
+ }
+ setState(() {
+ _loading = true;
+ _error = null;
+ });
+ try {
+ final res = await ApiService.login(phone, pwd);
+ if (!mounted) return;
+ if (res['code'] == 0) {
+ await ApiClient.saveToken(res['data']['token']);
+ await AppState.instance.refreshUserInfo();
+ if (mounted) {
+ // 登录成功后弹出公告
+ final ann = await ApiService.announcements();
+ if (mounted && ann.isNotEmpty) {
+ showAnnouncementDialog(context, ann);
+ }
+ }
+ } else {
+ setState(() => _error = res['msg'] ?? '登录失败');
+ }
+ } catch (e) {
+ setState(() => _error = '网络连接失败,请检查网络');
+ } finally {
+ if (mounted) setState(() => _loading = false);
+ }
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ body: Column(
+ children: [
+ // 顶部窗口控制条(透明悬浮在右上角)
+ SizedBox(
+ height: 36,
+ child: Row(
+ children: [
+ const Spacer(),
+ const WindowControls(dark: false),
+ ],
+ ),
+ ),
+ Expanded(
+ child: Row(
+ children: [
+ // 左侧宣传图
+ Expanded(
+ flex: 3,
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ Image.asset(
+ 'assets/images/login_art.png',
+ fit: BoxFit.cover,
+ errorBuilder: (_, __, ___) => Container(
+ color: const Color(0xFF1B1E2E),
+ child: const Center(
+ child: Text('99游戏盒子',
+ style: TextStyle(
+ color: AppColors.pink,
+ fontSize: 40,
+ fontWeight: FontWeight.bold)),
+ ),
+ ),
+ ),
+ // 底部渐变
+ Container(
+ decoration: BoxDecoration(
+ gradient: LinearGradient(
+ begin: Alignment.centerLeft,
+ end: Alignment.centerRight,
+ colors: [
+ Colors.transparent,
+ const Color(0xFF14161F).withOpacity(0.9),
+ ],
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ // 右侧登录面板
+ Container(
+ width: 400,
+ color: const Color(0xFF1B1E2E),
+ padding: const EdgeInsets.symmetric(
+ horizontal: 44, vertical: 30),
+ child: Column(
+ children: [
+ const Spacer(flex: 2),
+ // Logo
+ Container(
+ width: 88,
+ height: 88,
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ boxShadow: [
+ BoxShadow(
+ color: AppColors.pink.withOpacity(0.4),
+ blurRadius: 40,
+ spreadRadius: 5,
+ ),
+ ],
+ ),
+ child: ClipOval(
+ child: Image.asset(
+ 'assets/images/logo.png',
+ fit: BoxFit.cover,
+ errorBuilder: (_, __, ___) => Container(
+ color: AppColors.pink,
+ child: const Center(
+ child: Text('99',
+ style: TextStyle(
+ color: Colors.white,
+ fontSize: 32,
+ fontWeight: FontWeight.bold)),
+ ),
+ ),
+ ),
+ ),
+ ),
+ const SizedBox(height: 36),
+ // 账号
+ _InputField(
+ controller: _phoneCtrl,
+ icon: Icons.person_outline,
+ hint: '请输入账号',
+ ),
+ const SizedBox(height: 16),
+ // 密码
+ _InputField(
+ controller: _pwdCtrl,
+ icon: Icons.lock_outline,
+ hint: '请输入密码',
+ obscure: true,
+ ),
+ const SizedBox(height: 10),
+ if (_error != null)
+ Padding(
+ padding: const EdgeInsets.only(bottom: 8),
+ child: Text(_error!,
+ style: const TextStyle(
+ color: AppColors.red, fontSize: 13)),
+ ),
+ // 找回密码 / 注册账号
+ Row(
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
+ children: [
+ _LinkText('找回密码', onTap: () => showForgotDialog(context)),
+ _LinkText('注册账号', onTap: () => showRegisterDialog(context)),
+ ],
+ ),
+ const SizedBox(height: 22),
+ // 登录按钮
+ SizedBox(
+ width: double.infinity,
+ height: 46,
+ child: ElevatedButton(
+ onPressed: _loading ? null : _login,
+ style: ElevatedButton.styleFrom(
+ backgroundColor: AppColors.accent,
+ foregroundColor: Colors.white,
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(6)),
+ elevation: 0,
+ ),
+ child: _loading
+ ? const SizedBox(
+ width: 20,
+ height: 20,
+ child: CircularProgressIndicator(
+ strokeWidth: 2, color: Colors.white))
+ : const Text('登录',
+ style: TextStyle(fontSize: 16)),
+ ),
+ ),
+ const Spacer(flex: 3),
+ // 版本信息
+ Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ const Text('客户端版本: V2.2.5',
+ style: TextStyle(
+ color: AppColors.textDim, fontSize: 12)),
+ const SizedBox(width: 8),
+ GestureDetector(
+ onTap: _checkUpdate,
+ child: const Text('检查更新',
+ style: TextStyle(
+ color: AppColors.accent, fontSize: 12)),
+ ),
+ ],
+ ),
+ const SizedBox(height: 10),
+ ],
+ ),
+ ),
+ ],
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
+
+class _InputField extends StatefulWidget {
+ final TextEditingController controller;
+ final IconData icon;
+ final String hint;
+ final bool obscure;
+ const _InputField({
+ required this.controller,
+ required this.icon,
+ required this.hint,
+ this.obscure = false,
+ });
+
+ @override
+ State<_InputField> createState() => _InputFieldState();
+}
+
+class _InputFieldState extends State<_InputField> {
+ bool _obscure = false;
+
+ @override
+ void initState() {
+ super.initState();
+ _obscure = widget.obscure;
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Container(
+ height: 46,
+ decoration: BoxDecoration(
+ color: const Color(0xFF14161F),
+ borderRadius: BorderRadius.circular(6),
+ border: Border.all(color: const Color(0xFF333750)),
+ ),
+ child: Row(
+ children: [
+ const SizedBox(width: 12),
+ Icon(widget.icon, color: AppColors.textGray, size: 20),
+ const SizedBox(width: 10),
+ Expanded(
+ child: TextField(
+ controller: widget.controller,
+ obscureText: _obscure,
+ style: const TextStyle(color: Colors.white, fontSize: 14),
+ decoration: InputDecoration(
+ hintText: widget.hint,
+ hintStyle: const TextStyle(
+ color: AppColors.textDim, fontSize: 13),
+ border: InputBorder.none,
+ isDense: true,
+ ),
+ ),
+ ),
+ if (widget.obscure)
+ IconButton(
+ icon: Icon(
+ _obscure
+ ? Icons.visibility_off_outlined
+ : Icons.visibility_outlined,
+ color: AppColors.textDim,
+ size: 18),
+ onPressed: () => setState(() => _obscure = !_obscure),
+ ),
+ const SizedBox(width: 8),
+ ],
+ ),
+ );
+ }
+}
+
+class _LinkText extends StatelessWidget {
+ final String text;
+ final VoidCallback? onTap;
+ const _LinkText(this.text, {this.onTap});
+ @override
+ Widget build(BuildContext context) {
+ return GestureDetector(
+ onTap: onTap,
+ child: Text(text,
+ style: const TextStyle(color: AppColors.textGray, fontSize: 13)),
+ );
+ }
+}
+
+/// 全局 Toast
+void showToast(BuildContext context, 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),
+ boxShadow: [
+ BoxShadow(
+ color: Colors.black.withOpacity(0.4), blurRadius: 10)
+ ],
+ ),
+ 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());
+}
+
+/// 登录成功后的公告弹窗
+void showAnnouncementDialog(BuildContext context, String content) {
+ showDialog(
+ context: context,
+ builder: (context) => Dialog(
+ backgroundColor: Colors.white,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
+ child: Container(
+ width: 420,
+ padding: const EdgeInsets.all(24),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ children: [
+ const Icon(Icons.campaign,
+ color: Color(0xFF2B7FFF), size: 22),
+ const SizedBox(width: 8),
+ const Text('系统公告',
+ style: TextStyle(
+ color: Colors.black,
+ fontSize: 16,
+ fontWeight: FontWeight.bold)),
+ const Spacer(),
+ GestureDetector(
+ onTap: () => Navigator.pop(context),
+ child: const Icon(Icons.close,
+ color: Colors.grey, size: 20),
+ ),
+ ],
+ ),
+ const SizedBox(height: 16),
+ ConstrainedBox(
+ constraints: const BoxConstraints(maxHeight: 320),
+ child: SingleChildScrollView(
+ child: Text(
+ content,
+ style: const TextStyle(
+ color: Color(0xFFE74C3C),
+ fontSize: 13,
+ height: 1.9),
+ ),
+ ),
+ ),
+ const SizedBox(height: 16),
+ Align(
+ alignment: Alignment.centerRight,
+ child: ElevatedButton(
+ onPressed: () => Navigator.pop(context),
+ style: ElevatedButton.styleFrom(
+ backgroundColor: const Color(0xFF2B7FFF),
+ foregroundColor: Colors.white,
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(4)),
+ ),
+ child: const Text('知道了'),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+}
--
Gitblit v1.10.0