login_page.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import 'package:chicken_farm/core/utils/logger.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:chicken_farm/core/utils/toast.dart';
  6. import 'package:chicken_farm/modes/auth/login_model.dart';
  7. import 'package:chicken_farm/routes/app_routes.dart';
  8. import 'package:chicken_farm/stores/auth_store.dart';
  9. import 'package:chicken_farm/pages/account/config_dialog.dart';
  10. class LoginPage extends ConsumerStatefulWidget {
  11. const LoginPage({super.key});
  12. @override
  13. ConsumerState<LoginPage> createState() => _LoginPageState();
  14. }
  15. class _LoginPageState extends ConsumerState<LoginPage> {
  16. final _formKey = GlobalKey<FormState>();
  17. final _usernameCtrl = TextEditingController(text: '');
  18. final _passwordCtrl = TextEditingController(text: '');
  19. @override
  20. void dispose() {
  21. _usernameCtrl.dispose();
  22. _passwordCtrl.dispose();
  23. super.dispose();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. final authState = ref.watch(authStoreProvider);
  28. final authStore = ref.read(authStoreProvider.notifier);
  29. return Scaffold(
  30. body: Stack(
  31. children: [
  32. Container(
  33. decoration: BoxDecoration(
  34. gradient: LinearGradient(
  35. begin: Alignment.topCenter,
  36. end: Alignment.bottomCenter,
  37. colors: [
  38. Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
  39. Theme.of(context).colorScheme.surface,
  40. ],
  41. ),
  42. ),
  43. child: Center(
  44. child: SingleChildScrollView(
  45. padding: const EdgeInsets.all(24.0),
  46. child: ConstrainedBox(
  47. constraints: const BoxConstraints(maxWidth: 400),
  48. child: Card(
  49. elevation: 8,
  50. shape: RoundedRectangleBorder(
  51. borderRadius: BorderRadius.circular(16),
  52. ),
  53. child: Padding(
  54. padding: const EdgeInsets.all(24.0),
  55. child: Form(
  56. key: _formKey,
  57. child: Column(
  58. mainAxisSize: MainAxisSize.min,
  59. children: [
  60. Text(
  61. '养殖场管理系统',
  62. style: TextStyle(
  63. fontSize: 24,
  64. fontWeight: FontWeight.bold,
  65. color: Theme.of(context).colorScheme.primary,
  66. ),
  67. ),
  68. const SizedBox(height: 24),
  69. TextFormField(
  70. controller: _usernameCtrl,
  71. decoration: InputDecoration(
  72. labelText: '用户名',
  73. prefixIcon: const Icon(Icons.person),
  74. border: OutlineInputBorder(
  75. borderRadius: BorderRadius.circular(12),
  76. ),
  77. filled: true,
  78. fillColor: Colors.grey[50],
  79. ),
  80. validator: (v) => v!.isEmpty ? '请输入用户名' : null,
  81. ),
  82. const SizedBox(height: 16),
  83. TextFormField(
  84. controller: _passwordCtrl,
  85. obscureText: true,
  86. decoration: InputDecoration(
  87. labelText: '密码',
  88. prefixIcon: const Icon(Icons.lock),
  89. border: OutlineInputBorder(
  90. borderRadius: BorderRadius.circular(12),
  91. ),
  92. filled: true,
  93. fillColor: Colors.grey[50],
  94. ),
  95. validator: (v) =>
  96. v!.length < 6 ? '密码不能少于6位' : null,
  97. ),
  98. const SizedBox(height: 24),
  99. SizedBox(
  100. width: double.infinity,
  101. height: 50,
  102. child: ElevatedButton(
  103. onPressed: authState.state == AuthState.loading
  104. ? null
  105. : () {
  106. if (_formKey.currentState!.validate()) {
  107. // 关闭当前焦点上的键盘
  108. FocusManager.instance.primaryFocus
  109. ?.unfocus();
  110. authStore
  111. .login(
  112. LoginModel(
  113. username: _usernameCtrl.text,
  114. password: _passwordCtrl.text,
  115. ),
  116. )
  117. .then((_) async {
  118. // 登录成功后跳转到主页
  119. if (context.mounted) {
  120. logger.d('登录成功');
  121. context.goNamed(
  122. AppRouteNames.home,
  123. );
  124. }
  125. })
  126. .catchError((error) {
  127. // 处理登录错误
  128. logger.e('登录失败: $error');
  129. if (context.mounted) {
  130. String errorMessage = '登录失败';
  131. if (error is Exception) {
  132. errorMessage = error
  133. .toString();
  134. }
  135. ToastUtil.error(errorMessage);
  136. }
  137. });
  138. }
  139. },
  140. style: ElevatedButton.styleFrom(
  141. shape: RoundedRectangleBorder(
  142. borderRadius: BorderRadius.circular(12),
  143. ),
  144. textStyle: const TextStyle(fontSize: 16),
  145. ),
  146. child: authState.state == AuthState.loading
  147. ? const CircularProgressIndicator(
  148. color: Colors.white,
  149. )
  150. : const Text('登录'),
  151. ),
  152. ),
  153. ],
  154. ),
  155. ),
  156. ),
  157. ),
  158. ),
  159. ),
  160. ),
  161. ),
  162. Positioned(
  163. bottom: 16,
  164. right: 16,
  165. child: IconButton(
  166. icon: const Icon(Icons.settings),
  167. onPressed: () async {
  168. final result = await showDialog(
  169. context: context,
  170. builder: (context) => const ConfigDialog(),
  171. );
  172. // 如果配置发生了变化,显示提示
  173. if (result == true && context.mounted) {
  174. // ScaffoldMessenger.of(context).showSnackBar(
  175. // const SnackBar(
  176. // content: Text('配置已保存'),
  177. // duration: Duration(seconds: 2),
  178. // ),
  179. // );
  180. ToastUtil.success('配置已保存');
  181. }
  182. },
  183. ),
  184. ),
  185. ],
  186. ),
  187. );
  188. }
  189. }