Files
AutoJudge-Flutter/lib/screens/login_screen.dart
2025-11-13 09:14:49 +08:00

253 lines
9.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
import 'home_screen.dart';
/// Login screen for user authentication
///
/// Provides input fields for student ID, EC password, and UAAP password
/// Integrates with AuthProvider for authentication
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _formKey = GlobalKey<FormState>();
final _userIdController = TextEditingController();
final _ecPasswordController = TextEditingController();
final _passwordController = TextEditingController();
bool _obscureEcPassword = true;
bool _obscurePassword = true;
@override
void dispose() {
_userIdController.dispose();
_ecPasswordController.dispose();
_passwordController.dispose();
super.dispose();
}
Future<void> _handleLogin() async {
if (!_formKey.currentState!.validate()) {
return;
}
final authProvider = Provider.of<AuthProvider>(context, listen: false);
final success = await authProvider.login(
userId: _userIdController.text.trim(),
ecPassword: _ecPasswordController.text,
password: _passwordController.text,
);
if (!mounted) return;
if (success) {
// Navigate to home screen
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
} else {
// Show error message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(authProvider.errorMessage ?? '登录失败'),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// App logo/title
Icon(
Icons.school,
size: 80,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(height: 16),
Text(
'自动评教系统',
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Text(
'安徽财经大学',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 48),
// Student ID field
TextFormField(
controller: _userIdController,
decoration: const InputDecoration(
labelText: '学号',
hintText: '请输入学号',
prefixIcon: Icon(Icons.person),
),
keyboardType: TextInputType.number,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return '请输入学号';
}
return null;
},
),
const SizedBox(height: 16),
// EC password field
TextFormField(
controller: _ecPasswordController,
decoration: InputDecoration(
labelText: 'EC密码',
hintText: '请输入EC系统密码',
prefixIcon: const Icon(Icons.lock),
suffixIcon: IconButton(
icon: Icon(
_obscureEcPassword
? Icons.visibility_off
: Icons.visibility,
),
onPressed: () {
setState(() {
_obscureEcPassword = !_obscureEcPassword;
});
},
),
),
obscureText: _obscureEcPassword,
validator: (value) {
if (value == null || value.isEmpty) {
return '请输入EC密码';
}
return null;
},
),
const SizedBox(height: 16),
// UAAP password field
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'UAAP密码',
hintText: '请输入UAAP系统密码',
prefixIcon: const Icon(Icons.vpn_key),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility_off
: Icons.visibility,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
obscureText: _obscurePassword,
validator: (value) {
if (value == null || value.isEmpty) {
return '请输入UAAP密码';
}
return null;
},
),
const SizedBox(height: 32),
// Login button
Consumer<AuthProvider>(
builder: (context, authProvider, child) {
final isLoading = authProvider.state == AuthState.loading;
return ElevatedButton(
onPressed: isLoading ? null : _handleLogin,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Theme.of(
context,
).colorScheme.primary,
foregroundColor: Theme.of(
context,
).colorScheme.onPrimary,
),
child: isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
: const Text('登录', style: TextStyle(fontSize: 16)),
);
},
),
const SizedBox(height: 16),
// Help text
Text(
'首次登录需要输入EC和UAAP系统密码\n登录信息将被安全加密存储',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 32),
// Signature
Column(
children: [
Text(
'❤ Created By LoveACE Team',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(
context,
).colorScheme.onSurfaceVariant.withOpacity(0.6),
),
textAlign: TextAlign.center,
),
const SizedBox(height: 4),
Text(
'🌧 Powered By Sibuxiangx & Flutter',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(
context,
).colorScheme.onSurfaceVariant.withOpacity(0.6),
),
textAlign: TextAlign.center,
),
],
),
],
),
),
),
),
),
);
}
}