|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import '../../core/theme/app_colors.dart'; |
| 3 | +import '../../core/theme/auth_layout_template.dart'; |
| 4 | +import '../../core/theme/custom_text_field.dart'; |
| 5 | +import '../../viewmodels/auth_viewmodels.dart'; |
| 6 | +import 'register_view.dart'; |
| 7 | +import 'forgot_password_view.dart'; |
| 8 | + |
| 9 | +class LoginView extends StatefulWidget { |
| 10 | + const LoginView({super.key}); |
| 11 | + |
| 12 | + @override |
| 13 | + State<LoginView> createState() => _LoginViewState(); |
| 14 | +} |
| 15 | + |
| 16 | +class _LoginViewState extends State<LoginView> { |
| 17 | + final _vm = LoginViewModel(); |
| 18 | + |
| 19 | + @override |
| 20 | + Widget build(BuildContext context) { |
| 21 | + return AnimatedBuilder( |
| 22 | + animation: _vm, |
| 23 | + builder: (context, _) => |
| 24 | + AuthLayoutTemplate( |
| 25 | + title: 'Task Management', |
| 26 | + subtitle: 'Chào mừng trở lại!', |
| 27 | + submitText: 'Đăng nhập', |
| 28 | + isLoading: _vm.isLoading, |
| 29 | + showSocial: true, |
| 30 | + onSubmit: () async { |
| 31 | + FocusScope.of(context).unfocus(); // Hide keyboard |
| 32 | + final success = await _vm.login(); |
| 33 | + if (!context.mounted) return; |
| 34 | + |
| 35 | + if (success) { |
| 36 | + ScaffoldMessenger.of(context).showSnackBar( |
| 37 | + const SnackBar( |
| 38 | + content: Text('Đăng nhập thành công!'), |
| 39 | + backgroundColor: AppColors.success, |
| 40 | + ), |
| 41 | + ); |
| 42 | + // TODO: Navigator.pushReplacementNamed(context, '/home'); |
| 43 | + } else { |
| 44 | + ScaffoldMessenger.of(context).showSnackBar( |
| 45 | + const SnackBar( |
| 46 | + content: Text('Vui lòng điền đầy đủ thông tin!'), |
| 47 | + backgroundColor: AppColors.error, |
| 48 | + ), |
| 49 | + ); |
| 50 | + } |
| 51 | + }, |
| 52 | + formContent: Column( |
| 53 | + crossAxisAlignment: CrossAxisAlignment.end, |
| 54 | + children: [ |
| 55 | + CustomTextField( |
| 56 | + label: 'Email', |
| 57 | + |
| 58 | + icon: Icons.mail, |
| 59 | + controller: _vm.emailCtrl, |
| 60 | + ), |
| 61 | + CustomTextField( |
| 62 | + label: 'Mật khẩu', |
| 63 | + hint: '••••••••', |
| 64 | + icon: Icons.lock, |
| 65 | + controller: _vm.passCtrl, |
| 66 | + isPassword: true, |
| 67 | + obscureText: _vm.obscurePass, |
| 68 | + onToggleVisibility: _vm.togglePass, |
| 69 | + ), |
| 70 | + TextButton( |
| 71 | + onPressed: () => |
| 72 | + Navigator.push( |
| 73 | + context, |
| 74 | + MaterialPageRoute( |
| 75 | + builder: (_) => const ForgotPasswordView()), |
| 76 | + ), |
| 77 | + child: const Text( |
| 78 | + 'Quên mật khẩu?', |
| 79 | + style: TextStyle( |
| 80 | + color: AppColors.primary, |
| 81 | + fontWeight: FontWeight.bold, |
| 82 | + ), |
| 83 | + ), |
| 84 | + ), |
| 85 | + ], |
| 86 | + ), |
| 87 | + footerContent: _buildFooter( |
| 88 | + 'Chưa có tài khoản? ', 'Đăng ký ngay', () { |
| 89 | + Navigator.push( |
| 90 | + context, |
| 91 | + MaterialPageRoute(builder: (_) => const RegisterView()), |
| 92 | + ); |
| 93 | + }), |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + Widget _buildFooter(String text, String action, VoidCallback onTap) { |
| 99 | + return Padding( |
| 100 | + // Position this block 24dp above the bottom of the screen |
| 101 | + padding: const EdgeInsets.only(bottom: 24.0), |
| 102 | + child: Row( |
| 103 | + mainAxisAlignment: MainAxisAlignment.center, |
| 104 | + children: [ |
| 105 | + Text(text, style: const TextStyle(color: Colors.grey, fontSize: 16)), |
| 106 | + |
| 107 | + TextButton( |
| 108 | + onPressed: onTap, |
| 109 | + style: TextButton.styleFrom( |
| 110 | + padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 12), |
| 111 | + minimumSize: const Size(50, 48), |
| 112 | + ), |
| 113 | + child: Text( |
| 114 | + action, |
| 115 | + style: const TextStyle(color: Color(0xFF5A8DF3), |
| 116 | + fontWeight: FontWeight.bold, |
| 117 | + fontSize: 16), |
| 118 | + ), |
| 119 | + ), |
| 120 | + ], |
| 121 | + ), |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
0 commit comments