| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import 'package:chicken_farm/components/vb_app_bar.dart';
- import 'package:chicken_farm/core/utils/logger.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:go_router/go_router.dart';
- import 'package:chicken_farm/core/utils/toast.dart';
- import 'package:chicken_farm/modes/auth/login_model.dart';
- import 'package:chicken_farm/routes/app_routes.dart';
- import 'package:chicken_farm/stores/auth_store.dart';
- import 'package:chicken_farm/pages/account/config_dialog.dart';
- class LoginPage extends ConsumerStatefulWidget {
- const LoginPage({super.key});
- @override
- ConsumerState<LoginPage> createState() => _LoginPageState();
- }
- class _LoginPageState extends ConsumerState<LoginPage> {
- final _formKey = GlobalKey<FormState>();
- final _usernameCtrl = TextEditingController(text: '');
- final _passwordCtrl = TextEditingController(text: '');
- @override
- void dispose() {
- _usernameCtrl.dispose();
- _passwordCtrl.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- final authState = ref.watch(authStoreProvider);
- final authStore = ref.read(authStoreProvider.notifier);
- return Scaffold(
- appBar: const VberAppBar(title: '用户登录', showLeftButton: false),
- body: Stack(
- children: [
- Container(
- decoration: BoxDecoration(
- gradient: LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [
- Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
- Theme.of(context).colorScheme.surface,
- ],
- ),
- ),
- child: Center(
- child: SingleChildScrollView(
- padding: const EdgeInsets.all(24.0),
- child: Card(
- elevation: 8,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(16),
- ),
- child: Padding(
- padding: const EdgeInsets.all(24.0),
- child: Form(
- key: _formKey,
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(
- '养殖场管理系统',
- style: TextStyle(
- fontSize: 24,
- fontWeight: FontWeight.bold,
- color: Theme.of(context).colorScheme.primary,
- ),
- ),
- const SizedBox(height: 24),
- TextFormField(
- controller: _usernameCtrl,
- decoration: InputDecoration(
- labelText: '用户名',
- prefixIcon: const Icon(Icons.person),
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(12),
- ),
- filled: true,
- fillColor: Colors.grey[50],
- ),
- validator: (v) => v!.isEmpty ? '请输入用户名' : null,
- ),
- const SizedBox(height: 16),
- TextFormField(
- controller: _passwordCtrl,
- obscureText: true,
- decoration: InputDecoration(
- labelText: '密码',
- prefixIcon: const Icon(Icons.lock),
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(12),
- ),
- filled: true,
- fillColor: Colors.grey[50],
- ),
- validator: (v) => v!.length < 6 ? '密码不能少于6位' : null,
- ),
- const SizedBox(height: 24),
- SizedBox(
- width: double.infinity,
- height: 50,
- child: ElevatedButton(
- onPressed: authState.state == AuthState.loading
- ? null
- : () {
- if (_formKey.currentState!.validate()) {
- authStore
- .login(
- LoginModel(
- username: _usernameCtrl.text,
- password: _passwordCtrl.text,
- ),
- )
- .then((_) async {
- // 登录成功后跳转到主页
- if (context.mounted) {
- logger.d('登录成功');
- context.goNamed(
- AppRouteNames.home,
- );
- }
- })
- .catchError((error) {
- // 处理登录错误
- logger.e('登录失败: $error');
- if (context.mounted) {
- String errorMessage = '登录失败';
- if (error is Exception) {
- errorMessage = error
- .toString();
- }
- ToastUtil.error(errorMessage);
- }
- });
- }
- },
- style: ElevatedButton.styleFrom(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(12),
- ),
- textStyle: const TextStyle(fontSize: 16),
- ),
- child: authState.state == AuthState.loading
- ? const CircularProgressIndicator(
- color: Colors.white,
- )
- : const Text('登录'),
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- Positioned(
- bottom: 16,
- right: 16,
- child: IconButton(
- icon: const Icon(Icons.settings),
- onPressed: () async {
- final result = await showDialog(
- context: context,
- builder: (context) => const ConfigDialog(),
- );
- // 如果配置发生了变化,显示提示
- if (result == true && context.mounted) {
- // ScaffoldMessenger.of(context).showSnackBar(
- // const SnackBar(
- // content: Text('配置已保存'),
- // duration: Duration(seconds: 2),
- // ),
- // );
- ToastUtil.success('配置已保存');
- }
- },
- ),
- ),
- ],
- ),
- );
- }
- }
|