Files
slogan/app/lib/features/auth/login_page.dart
T
admin b53c57e02d Add 'app/' from commit 'a11a66886941bba128d89c1124115e1b1c87a128'
git-subtree-dir: app
git-subtree-mainline: 6ebd902c6b
git-subtree-split: a11a668869
2026-08-04 14:59:55 +08:00

162 lines
5.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:go_router/go_router.dart';
import '../../core/auth/auth_provider.dart';
class LoginPage extends ConsumerStatefulWidget {
const LoginPage({super.key});
@override
ConsumerState<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends ConsumerState<LoginPage> {
final _accountCtrl = TextEditingController();
final _passwordCtrl = TextEditingController();
@override
void dispose() {
_accountCtrl.dispose();
_passwordCtrl.dispose();
super.dispose();
}
Future<void> _submit() async {
final account = _accountCtrl.text.trim();
final password = _passwordCtrl.text;
if (account.isEmpty || password.isEmpty) {
Fluttertoast.showToast(msg: '请输入账号和密码');
return;
}
await ref.read(authProvider.notifier).login(account, password);
final state = ref.read(authProvider);
if (state.hasError) {
Fluttertoast.showToast(
msg: state.error.toString().replaceFirst('Exception: ', ''));
return;
}
if (state.value?.authenticated == true && mounted) {
context.go('/home');
}
}
Future<void> _showRegisterDialog() async {
final account = TextEditingController();
final password = TextEditingController();
final name = TextEditingController();
final result = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('注册新账号'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: account,
decoration: const InputDecoration(labelText: '账号')),
TextField(
controller: password,
obscureText: true,
decoration: const InputDecoration(labelText: '密码(至少6位)')),
TextField(
controller: name,
decoration: const InputDecoration(labelText: '昵称')),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: const Text('取消')),
FilledButton(
onPressed: () => Navigator.pop(ctx, true),
child: const Text('注册')),
],
),
);
if (result != true) return;
try {
final api = ref.read(apiClientProvider);
await api.post<Map<String, dynamic>>('/user/register', {
'account': account.text.trim(),
'password': password.text,
'name': name.text.trim(),
});
Fluttertoast.showToast(msg: '注册成功,正在登录...');
_accountCtrl.text = account.text.trim();
_passwordCtrl.text = password.text;
await _submit();
} catch (e) {
Fluttertoast.showToast(
msg: '注册失败:${e.toString().replaceFirst('Exception: ', '')}');
}
}
@override
Widget build(BuildContext context) {
final auth = ref.watch(authProvider);
final loading = auth.isLoading;
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Icon(Icons.face_retouching_natural,
size: 72, color: Theme.of(context).colorScheme.primary),
const SizedBox(height: 12),
const Text('我的形象穿搭',
textAlign: TextAlign.center,
style:
TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
const Text('拍出你的个人形象,生成专属穿搭方案',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey, fontSize: 13)),
const SizedBox(height: 40),
TextField(
controller: _accountCtrl,
decoration: const InputDecoration(
labelText: '账号',
prefixIcon: Icon(Icons.person_outline),
border: OutlineInputBorder()),
),
const SizedBox(height: 16),
TextField(
controller: _passwordCtrl,
obscureText: true,
decoration: const InputDecoration(
labelText: '密码',
prefixIcon: Icon(Icons.lock_outline),
border: OutlineInputBorder()),
onSubmitted: (_) => _submit(),
),
const SizedBox(height: 32),
FilledButton(
onPressed: loading ? null : _submit,
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14)),
child: loading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2))
: const Text('登录'),
),
const SizedBox(height: 8),
TextButton(
onPressed: loading ? null : _showRegisterDialog,
child: const Text('还没有账号?注册新账号'),
),
],
),
),
),
),
);
}
}