import 'package:DotLiving/views/Dashboard.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import '../../controllers/HomeController.dart'; import '../../controllers/UserController.dart'; import '../../helpers/Mensajes.dart'; import 'package:DotLiving/helpers/Mensajes.dart' as mensajes; class Login extends StatelessWidget { const Login({Key? key}) : super(key: key); @override Widget build(BuildContext context) { final bool isSmallScreen = MediaQuery.of(context).size.width < 600; return Scaffold( body: Center( child: isSmallScreen ? const Column( mainAxisSize: MainAxisSize.min, children: [ _Logo(), _FormContent(), ], ) : Container( padding: const EdgeInsets.all(32.0), constraints: const BoxConstraints(maxWidth: 800), child: const Row( children: [ Expanded(child: _Logo()), Expanded( child: Center(child: _FormContent()), ), ], ), ))); } } class _Logo extends StatelessWidget { const _Logo({Key? key}) : super(key: key); @override Widget build(BuildContext context) { final bool isSmallScreen = MediaQuery.of(context).size.width < 600; return Column( mainAxisSize: MainAxisSize.min, children: [ //FlutterLogo(size: isSmallScreen ? 100 : 200), Image( image: AssetImage( 'assets/dot_living.png', ), width: isSmallScreen ? 100 : 200, height: isSmallScreen ? 100 : 200, ), Padding( padding: const EdgeInsets.all(16.0), child: Text( "DotLiving", textAlign: TextAlign.center, style: isSmallScreen ? Theme.of(context).textTheme.headlineSmall?.copyWith( color: Colors.purple, fontWeight: FontWeight.bold) : Theme.of(context).textTheme.headlineMedium?.copyWith( color: Colors.purple, fontWeight: FontWeight.bold), ), ) ], ); } } class _FormContent extends StatefulWidget { const _FormContent({Key? key}) : super(key: key); @override State<_FormContent> createState() => __FormContentState(); } class __FormContentState extends State<_FormContent> { bool _isPasswordVisible = false; //final bool _rememberMe = false; final GlobalKey _formKey = GlobalKey(); TextEditingController email = TextEditingController(); TextEditingController password = TextEditingController(); @override void initState() { email.text = 'tester@mail.com'; password.text = '12345678'; super.initState(); } @override Widget build(BuildContext context) { return Container( constraints: const BoxConstraints(maxWidth: 300), child: Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ TextFormField( controller: email, validator: (value) { // add email validation if (value == null || value.isEmpty) { return 'Campo obligatorio'; } bool emailValid = RegExp( r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+") .hasMatch(value); if (!emailValid) { return 'Ingrese un email válido'; } return null; }, decoration: const InputDecoration( labelText: 'Email', hintText: 'Ingrese su email', prefixIcon: Icon(Icons.email_outlined), border: OutlineInputBorder(), ), ), _gap(), TextFormField( controller: password, validator: (value) { if (value == null || value.isEmpty) { return 'Campo obligatorio'; } // if (value.length < 6) { // return 'Password must be at least 6 characters'; // } return null; }, obscureText: !_isPasswordVisible, decoration: InputDecoration( labelText: 'Contraseña', hintText: 'Ingrese su contraseña', prefixIcon: const Icon(Icons.lock_outline_rounded), border: const OutlineInputBorder(), suffixIcon: IconButton( icon: Icon(_isPasswordVisible ? Icons.visibility_off : Icons.visibility), onPressed: () { setState(() { _isPasswordVisible = !_isPasswordVisible; }); }, )), ), _gap(), // CheckboxListTile( // value: _rememberMe, // onChanged: (value) { // if (value == null) return; // setState(() { // _rememberMe = value; // }); // }, // title: const Text('Remember me'), // controlAffinity: ListTileControlAffinity.leading, // dense: true, // contentPadding: const EdgeInsets.all(0), // ), // _gap(), SizedBox( width: double.infinity, child: ElevatedButton( style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(4)), ), child: const Padding( padding: EdgeInsets.all(10.0), child: Text( 'Entrar', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), ), onPressed: () async { if (_formKey.currentState?.validate() ?? false) { UserController controller = UserController(); var respuesta = await controller.login(email.text, password.text); if (respuesta['estatus'] == 1) { Navigator.of(context).push( MaterialPageRoute( builder: (context) => const Dashboard()), ); } else { mensajes.mensajeEmergente( context, "Error", "Información incorrecta"); } } }, ), ), _gap(), TextButton( onPressed: () async { var hayNuevaVersion = await HomeController().hayNuevaVersion() as Map; if (hayNuevaVersion['estatus']) { // ignore: use_build_context_synchronously mensajeNuevaVersion( context, "Versión disponible: ${hayNuevaVersion['ultima_version'].toString().replaceAll("_", ".")}", "Actualmente se ejecuta la versión ${dotenv.env['APP_VERSION'].toString().replaceAll("_", ".")} le sugerimos descargar la versión más reciente.", hayNuevaVersion['ultima_version']); } else { // ignore: use_build_context_synchronously mensajeNuevaVersion( context, "Última versión: ${hayNuevaVersion['ultima_version'].toString().replaceAll("_", ".")}", "Actualmente se ejecuta la versión ${dotenv.env['APP_VERSION'].toString().replaceAll("_", ".")} \n¿Desea dercargarla de todas formas?.", hayNuevaVersion['ultima_version']); } }, child: const Text("Buscar última alcualización")) ], ), ), ); } Widget _gap() => const SizedBox(height: 16); }