智慧水务管理系统 - 精河县供水工程综合管理平台

login_page.dart 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import 'package:water_management_system/services/auth_service.dart';
  4. import 'package:water_management_system/utils/constants.dart';
  5. import 'package:water_management_system/widgets/custom_button.dart';
  6. import 'package:water_management_system/widgets/custom_text_field.dart';
  7. class LoginPage extends StatefulWidget {
  8. const LoginPage({super.key});
  9. @override
  10. State<LoginPage> createState() => _LoginPageState();
  11. }
  12. class _LoginPageState extends State<LoginPage> {
  13. final _formKey = GlobalKey<FormState>();
  14. final _usernameController = TextEditingController();
  15. final _passwordController = TextEditingController();
  16. bool _obscurePassword = true;
  17. @override
  18. void dispose() {
  19. _usernameController.dispose();
  20. _passwordController.dispose();
  21. super.dispose();
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. body: Consumer<AuthService>(
  27. builder: (context, authService, child) {
  28. return SafeArea(
  29. child: SingleChildScrollView(
  30. child: Padding(
  31. padding: const EdgeInsets.all(24.0),
  32. child: Form(
  33. key: _formKey,
  34. child: Column(
  35. crossAxisAlignment: CrossAxisAlignment.stretch,
  36. children: [
  37. SizedBox(height: 60),
  38. // Logo
  39. Icon(
  40. Icons.water_drop,
  41. size: 80,
  42. color: AppConstants.primaryColor,
  43. ),
  44. SizedBox(height: 24),
  45. // 标题
  46. Text(
  47. '智慧水务管理系统',
  48. style: TextStyle(
  49. fontSize: 24,
  50. fontWeight: FontWeight.bold,
  51. color: Colors.black87,
  52. ),
  53. textAlign: TextAlign.center,
  54. ),
  55. SizedBox(height: 8),
  56. Text(
  57. '统一移动端平台',
  58. style: TextStyle(
  59. fontSize: 16,
  60. color: Colors.grey[600],
  61. ),
  62. textAlign: TextAlign.center,
  63. ),
  64. SizedBox(height: 48),
  65. // 用户名输入框
  66. CustomTextField(
  67. controller: _usernameController,
  68. labelText: '用户名',
  69. prefixIcon: Icons.person,
  70. validator: (value) {
  71. if (value == null || value.trim().isEmpty) {
  72. return '请输入用户名';
  73. }
  74. return null;
  75. },
  76. ),
  77. SizedBox(height: 16),
  78. // 密码输入框
  79. CustomTextField(
  80. controller: _passwordController,
  81. labelText: '密码',
  82. prefixIcon: Icons.lock,
  83. obscureText: _obscurePassword,
  84. suffixIcon: IconButton(
  85. icon: Icon(
  86. _obscurePassword ? Icons.visibility : Icons.visibility_off,
  87. ),
  88. onPressed: () {
  89. setState(() {
  90. _obscurePassword = !_obscurePassword;
  91. });
  92. },
  93. ),
  94. validator: (value) {
  95. if (value == null || value.trim().isEmpty) {
  96. return '请输入密码';
  97. }
  98. if (value.length < 6) {
  99. return '密码至少6位';
  100. }
  101. return null;
  102. },
  103. ),
  104. SizedBox(height: 32),
  105. // 登录按钮
  106. authService.isLoading
  107. ? const Center(
  108. child: CircularProgressIndicator(),
  109. )
  110. : CustomButton(
  111. text: '登录',
  112. onPressed: _handleLogin,
  113. ),
  114. SizedBox(height: 16),
  115. // 其他选项
  116. Row(
  117. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  118. children: [
  119. TextButton(
  120. onPressed: () {
  121. // 忘记密码逻辑
  122. },
  123. child: Text(
  124. '忘记密码?',
  125. style: TextStyle(color: AppConstants.primaryColor),
  126. ),
  127. ),
  128. TextButton(
  129. onPressed: () {
  130. // 注册逻辑
  131. },
  132. child: Text(
  133. '注册账号',
  134. style: TextStyle(color: AppConstants.primaryColor),
  135. ),
  136. ),
  137. ],
  138. ),
  139. ],
  140. ),
  141. ),
  142. ),
  143. ),
  144. );
  145. },
  146. ),
  147. );
  148. }
  149. Future<void> _handleLogin() async {
  150. if (!_formKey.currentState!.validate()) {
  151. return;
  152. }
  153. final authService = Provider.of<AuthService>(context, listen: false);
  154. final success = await authService.login(
  155. _usernameController.text.trim(),
  156. _passwordController.text,
  157. );
  158. if (success && mounted) {
  159. ScaffoldMessenger.of(context).showSnackBar(
  160. SnackBar(
  161. content: Text('欢迎回来,${authService.userName}!'),
  162. backgroundColor: Colors.green,
  163. ),
  164. );
  165. // 登录成功后会自动跳转到主页
  166. } else if (mounted) {
  167. ScaffoldMessenger.of(context).showSnackBar(
  168. SnackBar(
  169. content: const Text('登录失败,请检查用户名和密码'),
  170. backgroundColor: Colors.red,
  171. ),
  172. );
  173. }
  174. }
  175. }