login_page.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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: Card(
  47. elevation: 8,
  48. shape: RoundedRectangleBorder(
  49. borderRadius: BorderRadius.circular(16),
  50. ),
  51. child: Padding(
  52. padding: const EdgeInsets.all(24.0),
  53. child: Form(
  54. key: _formKey,
  55. child: Column(
  56. mainAxisSize: MainAxisSize.min,
  57. children: [
  58. Text(
  59. '养殖场管理系统',
  60. style: TextStyle(
  61. fontSize: 24,
  62. fontWeight: FontWeight.bold,
  63. color: Theme.of(context).colorScheme.primary,
  64. ),
  65. ),
  66. const SizedBox(height: 24),
  67. TextFormField(
  68. controller: _usernameCtrl,
  69. decoration: InputDecoration(
  70. labelText: '用户名',
  71. prefixIcon: const Icon(Icons.person),
  72. border: OutlineInputBorder(
  73. borderRadius: BorderRadius.circular(12),
  74. ),
  75. filled: true,
  76. fillColor: Colors.grey[50],
  77. ),
  78. validator: (v) => v!.isEmpty ? '请输入用户名' : null,
  79. ),
  80. const SizedBox(height: 16),
  81. TextFormField(
  82. controller: _passwordCtrl,
  83. obscureText: true,
  84. decoration: InputDecoration(
  85. labelText: '密码',
  86. prefixIcon: const Icon(Icons.lock),
  87. border: OutlineInputBorder(
  88. borderRadius: BorderRadius.circular(12),
  89. ),
  90. filled: true,
  91. fillColor: Colors.grey[50],
  92. ),
  93. validator: (v) => v!.length < 6 ? '密码不能少于6位' : null,
  94. ),
  95. const SizedBox(height: 24),
  96. SizedBox(
  97. width: double.infinity,
  98. height: 50,
  99. child: ElevatedButton(
  100. onPressed: authState.state == AuthState.loading
  101. ? null
  102. : () {
  103. if (_formKey.currentState!.validate()) {
  104. authStore
  105. .login(
  106. LoginModel(
  107. username: _usernameCtrl.text,
  108. password: _passwordCtrl.text,
  109. ),
  110. )
  111. .then((_) async {
  112. // 登录成功后跳转到主页
  113. if (context.mounted) {
  114. logger.d('登录成功');
  115. context.goNamed(
  116. AppRouteNames.home,
  117. );
  118. }
  119. })
  120. .catchError((error) {
  121. // 处理登录错误
  122. logger.e('登录失败: $error');
  123. if (context.mounted) {
  124. String errorMessage = '登录失败';
  125. if (error is Exception) {
  126. errorMessage = error
  127. .toString();
  128. }
  129. ToastUtil.error(errorMessage);
  130. }
  131. });
  132. }
  133. },
  134. style: ElevatedButton.styleFrom(
  135. shape: RoundedRectangleBorder(
  136. borderRadius: BorderRadius.circular(12),
  137. ),
  138. textStyle: const TextStyle(fontSize: 16),
  139. ),
  140. child: authState.state == AuthState.loading
  141. ? const CircularProgressIndicator(
  142. color: Colors.white,
  143. )
  144. : const Text('登录'),
  145. ),
  146. ),
  147. ],
  148. ),
  149. ),
  150. ),
  151. ),
  152. ),
  153. ),
  154. ),
  155. Positioned(
  156. bottom: 16,
  157. right: 16,
  158. child: IconButton(
  159. icon: const Icon(Icons.settings),
  160. onPressed: () async {
  161. final result = await showDialog(
  162. context: context,
  163. builder: (context) => const ConfigDialog(),
  164. );
  165. // 如果配置发生了变化,显示提示
  166. if (result == true && context.mounted) {
  167. // ScaffoldMessenger.of(context).showSnackBar(
  168. // const SnackBar(
  169. // content: Text('配置已保存'),
  170. // duration: Duration(seconds: 2),
  171. // ),
  172. // );
  173. ToastUtil.success('配置已保存');
  174. }
  175. },
  176. ),
  177. ),
  178. ],
  179. ),
  180. );
  181. }
  182. }