2026-09-21 17:20:43 +08:00
|
|
|
|
/// 登录页(UI-01 §4.5 主题统一,P3 计划 Task 3 基础)。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 账号/密码表单 → [ApiClient.login](会话由客户端自动持久化到
|
|
|
|
|
|
/// SessionStore)→ 成功 pushReplacement /home;失败以内联 StateBadge
|
|
|
|
|
|
/// (图标 + 文案 + 错误色)展示错误码本地化文案(AUTH_RATE_LIMITED 文案
|
|
|
|
|
|
/// 自带「稍后再试」;网络/校验错误各有专属文案)。提交期间按钮进入
|
|
|
|
|
|
/// 加载态防重复提交。
|
|
|
|
|
|
library;
|
|
|
|
|
|
|
2026-09-19 00:22:04 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
2026-09-19 01:19:57 +08:00
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2026-09-19 00:22:04 +08:00
|
|
|
|
|
2026-09-19 01:19:57 +08:00
|
|
|
|
import '../core/api_client.dart';
|
|
|
|
|
|
import '../core/app_exception.dart';
|
|
|
|
|
|
import '../core/error_l10n.dart';
|
2026-09-19 04:47:31 +08:00
|
|
|
|
import '../features/uploads/engine_scope.dart';
|
2026-09-19 00:22:04 +08:00
|
|
|
|
import '../l10n/app_localizations.dart';
|
2026-09-21 17:20:43 +08:00
|
|
|
|
import '../ui/components/state_badge.dart';
|
|
|
|
|
|
import '../ui/tokens.dart';
|
2026-09-19 00:22:04 +08:00
|
|
|
|
|
2026-09-21 17:20:43 +08:00
|
|
|
|
/// 登录页。
|
2026-09-19 01:19:57 +08:00
|
|
|
|
class LoginPage extends ConsumerStatefulWidget {
|
2026-09-19 00:22:04 +08:00
|
|
|
|
const LoginPage({super.key});
|
|
|
|
|
|
|
2026-09-19 01:19:57 +08:00
|
|
|
|
@override
|
|
|
|
|
|
ConsumerState<LoginPage> createState() => _LoginPageState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _LoginPageState extends ConsumerState<LoginPage> {
|
|
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
|
|
final _accountController = TextEditingController();
|
|
|
|
|
|
final _passwordController = TextEditingController();
|
|
|
|
|
|
|
|
|
|
|
|
bool _submitting = false;
|
|
|
|
|
|
|
2026-09-21 17:20:43 +08:00
|
|
|
|
/// 最近一次提交失败文案(内联 StateBadge 展示;null = 无错误)。
|
|
|
|
|
|
String? _errorText;
|
|
|
|
|
|
|
2026-09-19 01:19:57 +08:00
|
|
|
|
@override
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
_accountController.dispose();
|
|
|
|
|
|
_passwordController.dispose();
|
|
|
|
|
|
super.dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _submit() async {
|
|
|
|
|
|
if (!_formKey.currentState!.validate()) return;
|
2026-09-21 17:20:43 +08:00
|
|
|
|
setState(() {
|
|
|
|
|
|
_submitting = true;
|
|
|
|
|
|
_errorText = null;
|
|
|
|
|
|
});
|
2026-09-19 01:19:57 +08:00
|
|
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
|
|
final navigator = Navigator.of(context);
|
|
|
|
|
|
try {
|
|
|
|
|
|
await ref.read(apiClientProvider).login(
|
|
|
|
|
|
_accountController.text.trim(),
|
|
|
|
|
|
_passwordController.text,
|
|
|
|
|
|
);
|
2026-09-19 04:47:31 +08:00
|
|
|
|
// 登录成功:刷新会话账号 → 引擎以新账号 scope start(Task 9 接线)。
|
|
|
|
|
|
await ref.read(sessionAccountProvider.notifier).refresh();
|
2026-09-19 01:19:57 +08:00
|
|
|
|
navigator.pushReplacementNamed('/home');
|
|
|
|
|
|
} on AppException catch (error) {
|
|
|
|
|
|
// 429(AUTH_RATE_LIMITED)等文案经错误码映射;登录页的
|
|
|
|
|
|
// AUTH_INVALID_CREDENTIALS 即「账号或密码错误」。
|
2026-09-21 17:20:43 +08:00
|
|
|
|
if (mounted) setState(() => _errorText = l10n.messageForError(error));
|
2026-09-19 01:19:57 +08:00
|
|
|
|
} on Object {
|
2026-09-21 17:20:43 +08:00
|
|
|
|
if (mounted) setState(() => _errorText = l10n.errorGeneric);
|
2026-09-19 01:19:57 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
if (mounted) setState(() => _submitting = false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-19 00:22:04 +08:00
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
|
|
return Scaffold(
|
|
|
|
|
|
appBar: AppBar(title: Text(l10n.appName)),
|
|
|
|
|
|
body: Center(
|
|
|
|
|
|
child: ConstrainedBox(
|
|
|
|
|
|
constraints: const BoxConstraints(maxWidth: 420),
|
|
|
|
|
|
child: Padding(
|
2026-09-21 17:20:43 +08:00
|
|
|
|
padding: const EdgeInsets.all(kSpace24),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
child: Form(
|
|
|
|
|
|
key: _formKey,
|
|
|
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
|
|
|
|
child: Column(
|
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
TextFormField(
|
|
|
|
|
|
controller: _accountController,
|
|
|
|
|
|
autofillHints: const [AutofillHints.username],
|
2026-09-21 17:20:43 +08:00
|
|
|
|
decoration: InputDecoration(labelText: l10n.username),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
validator: (value) =>
|
|
|
|
|
|
(value == null || value.trim().isEmpty)
|
|
|
|
|
|
? l10n.fieldRequired
|
|
|
|
|
|
: null,
|
|
|
|
|
|
),
|
2026-09-21 17:20:43 +08:00
|
|
|
|
const SizedBox(height: kSpace16),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
TextFormField(
|
|
|
|
|
|
controller: _passwordController,
|
|
|
|
|
|
obscureText: true,
|
|
|
|
|
|
enableSuggestions: false,
|
|
|
|
|
|
autocorrect: false,
|
|
|
|
|
|
autofillHints: const [AutofillHints.password],
|
|
|
|
|
|
onFieldSubmitted: (_) => _submitting ? null : _submit(),
|
2026-09-21 17:20:43 +08:00
|
|
|
|
decoration: InputDecoration(labelText: l10n.password),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
validator: (value) => (value == null || value.isEmpty)
|
|
|
|
|
|
? l10n.fieldRequired
|
|
|
|
|
|
: null,
|
|
|
|
|
|
),
|
2026-09-21 17:20:43 +08:00
|
|
|
|
if (_errorText != null) ...[
|
|
|
|
|
|
const SizedBox(height: kSpace16),
|
|
|
|
|
|
StateBadge(
|
|
|
|
|
|
variant: BadgeVariant.error,
|
|
|
|
|
|
label: _errorText!,
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
const SizedBox(height: kSpace24),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
FilledButton(
|
|
|
|
|
|
onPressed: _submitting ? null : _submit,
|
|
|
|
|
|
child: _submitting
|
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
|
height: 20,
|
|
|
|
|
|
width: 20,
|
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
|
)
|
|
|
|
|
|
: Text(l10n.login),
|
2026-09-19 00:22:04 +08:00
|
|
|
|
),
|
2026-09-21 17:20:43 +08:00
|
|
|
|
const SizedBox(height: kSpace12),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
Text(
|
|
|
|
|
|
l10n.loginHint,
|
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
|
style: Theme.of(context).textTheme.bodySmall,
|
2026-09-19 00:22:04 +08:00
|
|
|
|
),
|
2026-09-19 01:19:57 +08:00
|
|
|
|
],
|
|
|
|
|
|
),
|
2026-09-19 00:22:04 +08:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|