feat: app 骨架 + 网络层 + 认证(登录/注册)
- flutter create 初始化 + 依赖(riverpod 3/dio/go_router/three_dart 0.0.16) - ApiClient:JWT 拦截器 + 统一响应解包 + 401 登出回调 + multipart 上传 - TokenStorage(shared_preferences)+ AuthNotifier 登录/登出 - 登录页(含注册对话框)+ 4 Tab 主框架 - 6 个单元测试通过(网络层 4 + 认证 2) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
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('还没有账号?注册新账号'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user