login_screen.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../themes/themes.dart';
  4. import '../../viewmodels/viewmodels.dart';
  5. import '../../widgets/widgets.dart';
  6. class LoginScreen extends StatefulWidget {
  7. const LoginScreen({Key? key}) : super(key: key);
  8. @override
  9. State<LoginScreen> createState() => _LoginScreenState();
  10. }
  11. class _LoginScreenState extends State<LoginScreen> {
  12. final _correo = TextEditingController();
  13. final _pass = TextEditingController();
  14. @override
  15. void dispose() {
  16. super.dispose();
  17. _correo.dispose();
  18. _pass.dispose();
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. final size = MediaQuery.sizeOf(context);
  23. final loginViewModel = Provider.of<LoginViewModel>(context);
  24. final obscureText = loginViewModel.obscureText;
  25. final errores = loginViewModel.errores;
  26. return Scaffold(
  27. backgroundColor: Color.fromRGBO(245, 245, 245, 1),
  28. body: SingleChildScrollView(
  29. child: Padding(
  30. padding: const EdgeInsets.all(8.0),
  31. child: Column(
  32. mainAxisAlignment: MainAxisAlignment.center,
  33. children: [
  34. SizedBox(height: size.width < 1200 ? 20 : 100),
  35. const Image(
  36. image: AssetImage('assets/JoshiLogoHorizontal.png'),
  37. height: 250,
  38. ),
  39. const Text(
  40. 'Inicie sesión para acceder al sistema',
  41. style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold),
  42. textAlign: TextAlign.center,
  43. ),
  44. const SizedBox(height: 20),
  45. SizedBox(
  46. width: size.width < 1200 ? size.width : size.width * .35,
  47. child: Card(
  48. elevation: 5,
  49. shape: RoundedRectangleBorder(
  50. borderRadius: BorderRadius.circular(15)),
  51. child: Padding(
  52. padding: const EdgeInsets.all(15),
  53. child: Column(
  54. children: [
  55. //CORREO ELECTRONICO
  56. AppTextField(
  57. autofillHints: [AutofillHints.username],
  58. prefixIcon: const Icon(Icons.mail),
  59. etiqueta: 'Correo electrónico',
  60. hintText: 'Introduzca su correo electrónico',
  61. errorText: errores?['correo'],
  62. controller: _correo,
  63. keyboardType: TextInputType.emailAddress,
  64. ),
  65. const SizedBox(height: 15),
  66. //CONTRASEÑA
  67. AppTextField(
  68. autofillHints: [AutofillHints.newPassword],
  69. maxLines: 1,
  70. obscureText: obscureText,
  71. prefixIcon: const Icon(Icons.lock),
  72. suffixIcon: IconButton(
  73. onPressed: () {
  74. loginViewModel.showPassword();
  75. },
  76. icon: obscureText
  77. ? const Icon(Icons.remove_red_eye_outlined)
  78. : const Icon(Icons.remove_red_eye),
  79. ),
  80. onSubmitted: (v) async {
  81. if (v.isEmpty) return;
  82. await loginViewModel.login(
  83. _correo.text, _pass.text);
  84. },
  85. etiqueta: 'Contraseña',
  86. hintText: 'Introduzca su contraseña',
  87. errorText: errores?['clave'],
  88. controller: _pass,
  89. keyboardType: TextInputType.visiblePassword,
  90. ),
  91. ],
  92. ),
  93. ),
  94. ),
  95. ),
  96. const SizedBox(height: 20),
  97. Align(
  98. alignment: Alignment.center,
  99. child: SizedBox(
  100. height: 75,
  101. width: 380,
  102. child: ElevatedButton(
  103. style: ButtonStyle(
  104. shape: MaterialStatePropertyAll(
  105. RoundedRectangleBorder(
  106. borderRadius: BorderRadius.circular(10),
  107. ),
  108. ),
  109. backgroundColor:
  110. MaterialStatePropertyAll(AppTheme.secondary),
  111. ),
  112. onPressed: () async {
  113. await loginViewModel.login(_correo.text, _pass.text);
  114. String mensaje = "";
  115. if (loginViewModel.errores!["correo"] != null) {
  116. mensaje += "\n${loginViewModel.errores!["correo"]}";
  117. }
  118. if (loginViewModel.errores!["pass"] != null) {
  119. mensaje += "\n${loginViewModel.errores!["pass"]}";
  120. }
  121. if (mensaje.isNotEmpty && context.mounted) {
  122. return showDialog(
  123. context: context,
  124. builder: (context) {
  125. return AlertDialog(
  126. title: const Text("Alerta"),
  127. content: Text(mensaje),
  128. actions: [
  129. Row(children: [
  130. Expanded(
  131. child: TextButton(
  132. onPressed: () async {
  133. Navigator.pop(context);
  134. },
  135. child: const Text('Continuar'),
  136. ))
  137. ])
  138. ],
  139. );
  140. },
  141. );
  142. }
  143. },
  144. child: const Row(
  145. mainAxisAlignment: MainAxisAlignment.center,
  146. children: [
  147. Text(
  148. 'Iniciar Sesión',
  149. style: TextStyle(fontSize: 18, color: Colors.white),
  150. ),
  151. ],
  152. ),
  153. ),
  154. ),
  155. ),
  156. const SizedBox(height: 20),
  157. ],
  158. ),
  159. ),
  160. ),
  161. );
  162. }
  163. }