Siguientes funcionalidades
This commit is contained in:
@@ -33,8 +33,10 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
// ESTADO LOCAL
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
// Controlador para el TextField del ID de usuario
|
||||
final TextEditingController _idController = TextEditingController();
|
||||
// Controladores para los campos de email/registro
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _nameController = TextEditingController();
|
||||
final TextEditingController _direccionController = TextEditingController();
|
||||
|
||||
// Colonia seleccionada en el Dropdown (null = no seleccionada aún)
|
||||
String? _coloniaSeleccionada;
|
||||
@@ -42,6 +44,9 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
// Lista de colonias cargadas desde el backend
|
||||
List<String> _colonias = [];
|
||||
|
||||
// Indica si estamos en modo registro o en modo login
|
||||
bool _esRegistro = false;
|
||||
|
||||
// Estado de carga: mostramos spinner mientras cargamos colonias
|
||||
bool _cargandoColonias = true;
|
||||
|
||||
@@ -68,7 +73,9 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
@override
|
||||
void dispose() {
|
||||
// Siempre liberar controllers para evitar memory leaks
|
||||
_idController.dispose();
|
||||
_emailController.dispose();
|
||||
_nameController.dispose();
|
||||
_direccionController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -131,41 +138,92 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// ACCIÓN: INICIAR SESIÓN
|
||||
// Valida, guarda y navega.
|
||||
// Valida el correo, llama al backend y navega.
|
||||
// ----------------------------------------------------------------
|
||||
Future<void> _iniciarSesion() async {
|
||||
// Validación básica del ID
|
||||
final idTexto = _idController.text.trim();
|
||||
if (idTexto.isEmpty) {
|
||||
_mostrarError('Por favor ingresa tu ID de usuario.');
|
||||
return;
|
||||
}
|
||||
|
||||
final usuarioId = int.tryParse(idTexto);
|
||||
if (usuarioId == null || usuarioId <= 0) {
|
||||
_mostrarError('El ID debe ser un número positivo (ej: 1, 2, 3, 4).');
|
||||
return;
|
||||
}
|
||||
|
||||
if (_coloniaSeleccionada == null) {
|
||||
_mostrarError('Por favor selecciona tu colonia.');
|
||||
final email = _emailController.text.trim();
|
||||
if (email.isEmpty) {
|
||||
_mostrarError('Por favor ingresa tu correo.');
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _logueando = true);
|
||||
|
||||
// Guardar la sesión en shared_preferences para no pedir login de nuevo
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt('usuario_id', usuarioId);
|
||||
await prefs.setString('colonia', _coloniaSeleccionada!);
|
||||
try {
|
||||
final usuarioId = await _apiService.loginConCorreo(email);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt('usuario_id', usuarioId);
|
||||
await prefs.setString('email', email);
|
||||
|
||||
// Navegar a la pantalla principal pasando el usuario_id como argumento
|
||||
if (mounted) {
|
||||
Navigator.pushReplacementNamed(
|
||||
context,
|
||||
'/home',
|
||||
arguments: usuarioId,
|
||||
if (mounted) {
|
||||
Navigator.pushReplacementNamed(
|
||||
context,
|
||||
'/home',
|
||||
arguments: usuarioId,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
_mostrarError('Error iniciando sesión. Revisa tu correo o regístrate.');
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _logueando = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// ACCIÓN: REGISTRARSE
|
||||
// Valida los datos y crea un nuevo usuario en el backend.
|
||||
// ----------------------------------------------------------------
|
||||
Future<void> _registrarse() async {
|
||||
final nombre = _nameController.text.trim();
|
||||
final email = _emailController.text.trim();
|
||||
final direccion = _direccionController.text.trim();
|
||||
|
||||
if (nombre.isEmpty) {
|
||||
_mostrarError('Por favor ingresa tu nombre.');
|
||||
return;
|
||||
}
|
||||
if (email.isEmpty) {
|
||||
_mostrarError('Por favor ingresa tu correo.');
|
||||
return;
|
||||
}
|
||||
if (_coloniaSeleccionada == null) {
|
||||
_mostrarError('Por favor selecciona tu colonia.');
|
||||
return;
|
||||
}
|
||||
if (direccion.isEmpty) {
|
||||
_mostrarError('Por favor ingresa tu dirección.');
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _logueando = true);
|
||||
|
||||
try {
|
||||
final usuarioId = await _apiService.registrarUsuario(
|
||||
nombre,
|
||||
email,
|
||||
direccion,
|
||||
_coloniaSeleccionada!,
|
||||
);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt('usuario_id', usuarioId);
|
||||
await prefs.setString('email', email);
|
||||
await prefs.setString('colonia', _coloniaSeleccionada!);
|
||||
|
||||
if (mounted) {
|
||||
Navigator.pushReplacementNamed(
|
||||
context,
|
||||
'/home',
|
||||
arguments: usuarioId,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
_mostrarError('Error registrando usuario. Intenta con otro correo.');
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _logueando = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,82 +285,102 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
const SizedBox(height: 48),
|
||||
|
||||
// ------------------------------------------------
|
||||
// CAMPO: ID de Usuario
|
||||
// NOTA PARA EL EQUIPO: Para la demo usa IDs 1 al 4
|
||||
// (son los que creó el seed del backend)
|
||||
// FORMULARIO: Correo / Registro
|
||||
// ------------------------------------------------
|
||||
TextField(
|
||||
controller: _idController,
|
||||
keyboardType: TextInputType.number,
|
||||
controller: _emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'ID de Usuario',
|
||||
hintText: 'Ej: 1, 2, 3 ó 4',
|
||||
helperText: 'Usa los IDs del seed del backend (1-4)',
|
||||
prefixIcon: const Icon(Icons.person_outline),
|
||||
labelText: 'Correo electrónico',
|
||||
hintText: 'usuario@ejemplo.com',
|
||||
prefixIcon: const Icon(Icons.email_outlined),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// ------------------------------------------------
|
||||
// DROPDOWN: Selección de Colonia
|
||||
// ------------------------------------------------
|
||||
if (_cargandoColonias)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 16),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
)
|
||||
else ...[
|
||||
// Aviso si se usó el fallback local
|
||||
if (_errorColonias != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
'⚠️ $_errorColonias',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.orange.shade700,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// El DropdownButtonFormField necesita que los items
|
||||
// vengan de _colonias, que se cargó en initState.
|
||||
DropdownButtonFormField<String>(
|
||||
value: _coloniaSeleccionada,
|
||||
hint: const Text('Selecciona tu colonia'),
|
||||
if (_esRegistro) ...[
|
||||
TextField(
|
||||
controller: _nameController,
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: const Icon(Icons.location_city_outlined),
|
||||
labelText: 'Nombre completo',
|
||||
prefixIcon: const Icon(Icons.person_outline),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
// Convierte cada String de _colonias en un DropdownMenuItem
|
||||
items: _colonias.map((colonia) {
|
||||
return DropdownMenuItem(
|
||||
value: colonia,
|
||||
child: Text(colonia),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (valor) {
|
||||
setState(() => _coloniaSeleccionada = valor);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _direccionController,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Dirección',
|
||||
hintText: 'Calle, número, colonia',
|
||||
prefixIcon: const Icon(Icons.home_outlined),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
|
||||
if (_esRegistro)
|
||||
if (_cargandoColonias)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 16),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
)
|
||||
else ...[
|
||||
if (_errorColonias != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
'⚠️ $_errorColonias',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.orange.shade700,
|
||||
),
|
||||
),
|
||||
),
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _coloniaSeleccionada,
|
||||
hint: const Text('Selecciona tu colonia'),
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: const Icon(Icons.location_city_outlined),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
items: _colonias.map((colonia) {
|
||||
return DropdownMenuItem(
|
||||
value: colonia,
|
||||
child: Text(colonia),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (valor) {
|
||||
setState(() => _coloniaSeleccionada = valor);
|
||||
},
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// ------------------------------------------------
|
||||
// BOTÓN: Entrar
|
||||
// BOTÓN: Entrar / Registrarse
|
||||
// Muestra spinner mientras _logueando == true
|
||||
// ------------------------------------------------
|
||||
SizedBox(
|
||||
height: 56,
|
||||
child: ElevatedButton(
|
||||
onPressed: _logueando ? null : _iniciarSesion,
|
||||
onPressed: _logueando
|
||||
? null
|
||||
: _esRegistro
|
||||
? _registrarse
|
||||
: _iniciarSesion,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: colorScheme.primary,
|
||||
foregroundColor: colorScheme.onPrimary,
|
||||
@@ -319,9 +397,9 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text(
|
||||
'Entrar',
|
||||
style: TextStyle(
|
||||
: Text(
|
||||
_esRegistro ? 'Registrarse' : 'Iniciar sesión',
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
@@ -329,17 +407,42 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
TextButton(
|
||||
onPressed: _logueando
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
_esRegistro = !_esRegistro;
|
||||
// Clear fields when switching modes
|
||||
_nameController.clear();
|
||||
_direccionController.clear();
|
||||
_coloniaSeleccionada = null;
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
_esRegistro
|
||||
? '¿Ya tienes cuenta? Inicia sesión'
|
||||
: '¿No tienes cuenta? Regístrate',
|
||||
style: TextStyle(
|
||||
color: colorScheme.primary,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Nota informativa para jueces/demos
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: colorScheme.primaryContainer.withOpacity(0.3),
|
||||
color: colorScheme.primaryContainer.withValues(alpha: 0.3),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'🧪 Demo: Usa IDs del 1 al 4. Corre primero POST /api/seed en el backend.',
|
||||
_esRegistro
|
||||
? 'Regístrate con tu correo, nombre y dirección para recibir avisos de recolección.'
|
||||
: 'Inicia sesión con tu correo para ver el estado del camión y recibir notificaciones.',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: colorScheme.primary,
|
||||
|
||||
Reference in New Issue
Block a user