103 lines
3.2 KiB
Dart
103 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../../core/theme/app_theme.dart';
|
|
import '../../providers/auth_provider.dart';
|
|
import '../../services/auth_service.dart';
|
|
|
|
/// 登录页:骨架期提供手机号+密码登录,验证码/邮箱登录随迭代补充
|
|
class LoginScreen extends ConsumerStatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
|
final _phone = TextEditingController();
|
|
final _password = TextEditingController();
|
|
bool _loading = false;
|
|
String? _error;
|
|
|
|
Future<void> _submit() async {
|
|
final phone = _phone.text.trim();
|
|
final password = _password.text;
|
|
if (phone.isEmpty || password.isEmpty) {
|
|
setState(() => _error = '请输入手机号和密码');
|
|
return;
|
|
}
|
|
setState(() {
|
|
_loading = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
final result = await AuthService.login(
|
|
type: 'phone_password',
|
|
phone: phone,
|
|
password: password,
|
|
);
|
|
await ref.read(authProvider.notifier).onLoginSuccess(result.user);
|
|
if (mounted) context.go('/home');
|
|
} catch (e) {
|
|
setState(() => _error = e.toString().replaceFirst('ApiException(', '').split(', ').last);
|
|
} finally {
|
|
if (mounted) setState(() => _loading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('登录')),
|
|
body: SafeArea(
|
|
child: ListView(
|
|
padding: const EdgeInsets.all(24),
|
|
children: [
|
|
const SizedBox(height: 32),
|
|
const Text(
|
|
'欢迎回来',
|
|
style: TextStyle(fontSize: 26, fontWeight: FontWeight.w700),
|
|
),
|
|
const SizedBox(height: 32),
|
|
TextField(
|
|
controller: _phone,
|
|
keyboardType: TextInputType.phone,
|
|
decoration: const InputDecoration(hintText: '手机号'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _password,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(hintText: '密码'),
|
|
),
|
|
if (_error != null) ...[
|
|
const SizedBox(height: 12),
|
|
Text(_error!, style: const TextStyle(color: AppColors.primary, fontSize: 13)),
|
|
],
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: _loading ? null : _submit,
|
|
child: _loading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text('登录'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextButton(
|
|
onPressed: () => context.push('/register'),
|
|
child: const Text(
|
|
'没有账号?去注册',
|
|
style: TextStyle(color: AppColors.textSecondary),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|