| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'package:water_management_system/services/auth_service.dart';
- import 'package:water_management_system/utils/constants.dart';
- import 'package:water_management_system/widgets/custom_button.dart';
- import 'package:water_management_system/widgets/custom_text_field.dart';
-
- class LoginPage extends StatefulWidget {
- const LoginPage({super.key});
-
- @override
- State<LoginPage> createState() => _LoginPageState();
- }
-
- class _LoginPageState extends State<LoginPage> {
- final _formKey = GlobalKey<FormState>();
- final _usernameController = TextEditingController();
- final _passwordController = TextEditingController();
- bool _obscurePassword = true;
-
- @override
- void dispose() {
- _usernameController.dispose();
- _passwordController.dispose();
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Consumer<AuthService>(
- builder: (context, authService, child) {
- return SafeArea(
- child: SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.all(24.0),
- child: Form(
- key: _formKey,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- SizedBox(height: 60),
-
- // Logo
- Icon(
- Icons.water_drop,
- size: 80,
- color: AppConstants.primaryColor,
- ),
-
- SizedBox(height: 24),
-
- // 标题
- Text(
- '智慧水务管理系统',
- style: TextStyle(
- fontSize: 24,
- fontWeight: FontWeight.bold,
- color: Colors.black87,
- ),
- textAlign: TextAlign.center,
- ),
-
- SizedBox(height: 8),
-
- Text(
- '统一移动端平台',
- style: TextStyle(
- fontSize: 16,
- color: Colors.grey[600],
- ),
- textAlign: TextAlign.center,
- ),
-
- SizedBox(height: 48),
-
- // 用户名输入框
- CustomTextField(
- controller: _usernameController,
- labelText: '用户名',
- prefixIcon: Icons.person,
- validator: (value) {
- if (value == null || value.trim().isEmpty) {
- return '请输入用户名';
- }
- return null;
- },
- ),
-
- SizedBox(height: 16),
-
- // 密码输入框
- CustomTextField(
- controller: _passwordController,
- labelText: '密码',
- prefixIcon: Icons.lock,
- obscureText: _obscurePassword,
- suffixIcon: IconButton(
- icon: Icon(
- _obscurePassword ? Icons.visibility : Icons.visibility_off,
- ),
- onPressed: () {
- setState(() {
- _obscurePassword = !_obscurePassword;
- });
- },
- ),
- validator: (value) {
- if (value == null || value.trim().isEmpty) {
- return '请输入密码';
- }
- if (value.length < 6) {
- return '密码至少6位';
- }
- return null;
- },
- ),
-
- SizedBox(height: 32),
-
- // 登录按钮
- authService.isLoading
- ? const Center(
- child: CircularProgressIndicator(),
- )
- : CustomButton(
- text: '登录',
- onPressed: _handleLogin,
- ),
-
- SizedBox(height: 16),
-
- // 其他选项
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- TextButton(
- onPressed: () {
- // 忘记密码逻辑
- },
- child: Text(
- '忘记密码?',
- style: TextStyle(color: AppConstants.primaryColor),
- ),
- ),
- TextButton(
- onPressed: () {
- // 注册逻辑
- },
- child: Text(
- '注册账号',
- style: TextStyle(color: AppConstants.primaryColor),
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- ),
- ),
- );
- },
- ),
- );
- }
-
- Future<void> _handleLogin() async {
- if (!_formKey.currentState!.validate()) {
- return;
- }
-
- final authService = Provider.of<AuthService>(context, listen: false);
-
- final success = await authService.login(
- _usernameController.text.trim(),
- _passwordController.text,
- );
-
- if (success && mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text('欢迎回来,${authService.userName}!'),
- backgroundColor: Colors.green,
- ),
- );
- // 登录成功后会自动跳转到主页
- } else if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: const Text('登录失败,请检查用户名和密码'),
- backgroundColor: Colors.red,
- ),
- );
- }
- }
- }
|