login_screen.dart 7.0 KB

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