| | |
| | | import 'dart:convert'; |
| | | |
| | | import 'package:flutter/material.dart'; |
| | | 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/utils/util.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}); |
| | |
| | | bool _loading = false; |
| | | String? _error; |
| | | |
| | | @override |
| | | void initState() { |
| | | super.initState(); |
| | | _fillSavedCredentials(); |
| | | } |
| | | |
| | | /// 自动填充上次登录成功的账号密码 |
| | | Future<void> _fillSavedCredentials() async { |
| | | final (phone, pwd) = await AppPrefs.getLoginCredentials(); |
| | | if (!mounted || phone.isEmpty) return; |
| | | setState(() { |
| | | _phoneCtrl.text = phone; |
| | | _pwdCtrl.text = pwd; |
| | | }); |
| | | } |
| | | |
| | | // 检查更新提示 |
| | | void _checkUpdate() { |
| | | showToast(context, '当前已是最新版本!'); |
| | |
| | | if (!mounted) return; |
| | | if (res['code'] == 0) { |
| | | await ApiClient.saveToken(res['data']['token']); |
| | | // 登录成功后记住账号密码,下次打开自动填充 |
| | | await AppPrefs.saveLoginCredentials(phone, pwd); |
| | | await AppState.instance.refreshUserInfo(); |
| | | if (mounted) { |
| | | // 登录成功后弹出公告 |
| | | final ann = await ApiService.announcements(); |
| | | if (mounted && ann.isNotEmpty) { |
| | | showAnnouncementDialog(context, ann); |
| | | } |
| | | } |
| | | // 公告改为进入首页时统一弹出(见 main_shell) |
| | | } else { |
| | | setState(() => _error = res['msg'] ?? '登录失败'); |
| | | } |
| | |
| | | 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('知道了'), |
| | | ), |
| | | ), |
| | | ], |
| | | ), |
| | | ), |
| | | ), |
| | | ); |
| | | } |