login_page.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import 'package:chicken_farm/components/vb_app_bar.dart';
  2. import 'package:chicken_farm/core/utils/logger.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import 'package:go_router/go_router.dart';
  6. import 'package:chicken_farm/core/utils/toast.dart';
  7. import 'package:chicken_farm/modes/auth/login_model.dart';
  8. import 'package:chicken_farm/routes/app_routes.dart';
  9. import 'package:chicken_farm/stores/auth_store.dart';
  10. import 'package:chicken_farm/pages/account/config_dialog.dart';
  11. class LoginPage extends ConsumerStatefulWidget {
  12. const LoginPage({super.key});
  13. @override
  14. ConsumerState<LoginPage> createState() => _LoginPageState();
  15. }
  16. class _LoginPageState extends ConsumerState<LoginPage> {
  17. final _formKey = GlobalKey<FormState>();
  18. final _usernameCtrl = TextEditingController(text: '');
  19. final _passwordCtrl = TextEditingController(text: '');
  20. @override
  21. void dispose() {
  22. _usernameCtrl.dispose();
  23. _passwordCtrl.dispose();
  24. super.dispose();
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. final authState = ref.watch(authStoreProvider);
  29. final authStore = ref.read(authStoreProvider.notifier);
  30. return Scaffold(
  31. appBar: const VberAppBar(title: '用户登录', showLeftButton: false),
  32. body: Stack(
  33. children: [
  34. Container(
  35. decoration: BoxDecoration(
  36. gradient: LinearGradient(
  37. begin: Alignment.topCenter,
  38. end: Alignment.bottomCenter,
  39. colors: [
  40. Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
  41. Theme.of(context).colorScheme.surface,
  42. ],
  43. ),
  44. ),
  45. child: Center(
  46. child: SingleChildScrollView(
  47. padding: const EdgeInsets.all(24.0),
  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) => v!.length < 6 ? '密码不能少于6位' : null,
  96. ),
  97. const SizedBox(height: 24),
  98. SizedBox(
  99. width: double.infinity,
  100. height: 50,
  101. child: ElevatedButton(
  102. onPressed: authState.state == AuthState.loading
  103. ? null
  104. : () {
  105. if (_formKey.currentState!.validate()) {
  106. authStore
  107. .login(
  108. LoginModel(
  109. username: _usernameCtrl.text,
  110. password: _passwordCtrl.text,
  111. ),
  112. )
  113. .then((_) async {
  114. // 登录成功后跳转到主页
  115. if (context.mounted) {
  116. logger.d('登录成功');
  117. context.goNamed(
  118. AppRouteNames.home,
  119. );
  120. }
  121. })
  122. .catchError((error) {
  123. // 处理登录错误
  124. logger.e('登录失败: $error');
  125. if (context.mounted) {
  126. String errorMessage = '登录失败';
  127. if (error is Exception) {
  128. errorMessage = error
  129. .toString();
  130. }
  131. ToastUtil.error(errorMessage);
  132. }
  133. });
  134. }
  135. },
  136. style: ElevatedButton.styleFrom(
  137. shape: RoundedRectangleBorder(
  138. borderRadius: BorderRadius.circular(12),
  139. ),
  140. textStyle: const TextStyle(fontSize: 16),
  141. ),
  142. child: authState.state == AuthState.loading
  143. ? const CircularProgressIndicator(
  144. color: Colors.white,
  145. )
  146. : const Text('登录'),
  147. ),
  148. ),
  149. ],
  150. ),
  151. ),
  152. ),
  153. ),
  154. ),
  155. ),
  156. ),
  157. Positioned(
  158. bottom: 16,
  159. right: 16,
  160. child: IconButton(
  161. icon: const Icon(Icons.settings),
  162. onPressed: () async {
  163. final result = await showDialog(
  164. context: context,
  165. builder: (context) => const ConfigDialog(),
  166. );
  167. // 如果配置发生了变化,显示提示
  168. if (result == true && context.mounted) {
  169. // ScaffoldMessenger.of(context).showSnackBar(
  170. // const SnackBar(
  171. // content: Text('配置已保存'),
  172. // duration: Duration(seconds: 2),
  173. // ),
  174. // );
  175. ToastUtil.success('配置已保存');
  176. }
  177. },
  178. ),
  179. ),
  180. ],
  181. ),
  182. );
  183. }
  184. }