905086
2026-11-09 cbb26873e88a7a13e868448c1cc578ce70b8c6ae
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
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';
 
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;
 
  @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, '当前已是最新版本!');
  }
 
  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 AppPrefs.saveLoginCredentials(phone, pwd);
        await AppState.instance.refreshUserInfo();
        // 公告改为进入首页时统一弹出(见 main_shell)
      } 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('游趣电玩',
                                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());
}