correcciones #2
@@ -1,25 +1,22 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:flutter_application_1/features/auth/presentation/screens/register_screen.dart';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
import '../../features/auth/presentation/bloc/auth_bloc.dart';
|
import '../../features/auth/presentation/bloc/auth_bloc.dart';
|
||||||
import '../../features/auth/presentation/bloc/auth_state.dart';
|
import '../../features/auth/presentation/bloc/auth_state.dart';
|
||||||
import '../../features/auth/presentation/screens/login_screen.dart';
|
import '../../features/auth/presentation/screens/login_screen.dart';
|
||||||
|
import '../../features/auth/presentation/screens/register_screen.dart';
|
||||||
import '../../features/auth/presentation/screens/home_screen_placeholder.dart';
|
import '../../features/auth/presentation/screens/home_screen_placeholder.dart';
|
||||||
|
|
||||||
/// Rutas nombradas de la aplicación.
|
/// Rutas nombradas de la aplicación.
|
||||||
abstract final class AppRoutes {
|
abstract final class AppRoutes {
|
||||||
static const String splash = '/';
|
static const String splash = '/';
|
||||||
static const String login = '/login';
|
static const String login = '/login';
|
||||||
|
static const String register = '/register'; // 📍 Agregada constante oficial
|
||||||
static const String home = '/home';
|
static const String home = '/home';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configuración central de navegación con go_router.
|
/// Configuración central de navegación con go_router.
|
||||||
///
|
|
||||||
/// La redirección basada en estado de autenticación garantiza que
|
|
||||||
/// rutas protegidas sean inaccesibles sin sesión válida.
|
|
||||||
GoRouter createRouter(AuthBloc authBloc) {
|
GoRouter createRouter(AuthBloc authBloc) {
|
||||||
return GoRouter(
|
return GoRouter(
|
||||||
initialLocation: AppRoutes.splash,
|
initialLocation: AppRoutes.splash,
|
||||||
@@ -27,8 +24,7 @@ GoRouter createRouter(AuthBloc authBloc) {
|
|||||||
redirect: (BuildContext context, GoRouterState state) {
|
redirect: (BuildContext context, GoRouterState state) {
|
||||||
final authState = authBloc.state;
|
final authState = authBloc.state;
|
||||||
final isAuthenticated = authState is AuthAuthenticated;
|
final isAuthenticated = authState is AuthAuthenticated;
|
||||||
final isCheckingSession =
|
final isCheckingSession = authState is AuthCheckingSession || authState is AuthInitial;
|
||||||
authState is AuthCheckingSession || authState is AuthInitial;
|
|
||||||
final currentLocation = state.uri.path;
|
final currentLocation = state.uri.path;
|
||||||
|
|
||||||
// Mientras se verifica la sesión, mostrar splash.
|
// Mientras se verifica la sesión, mostrar splash.
|
||||||
@@ -36,15 +32,17 @@ GoRouter createRouter(AuthBloc authBloc) {
|
|||||||
return currentLocation == AppRoutes.splash ? null : AppRoutes.splash;
|
return currentLocation == AppRoutes.splash ? null : AppRoutes.splash;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si no está autenticado, ir a login.
|
// 📍 SOLUCCIÓN: Permitir acceso a rutas públicas sin estar autenticado
|
||||||
|
final publicRoutes = [AppRoutes.login, AppRoutes.register];
|
||||||
|
final isPublicRoute = publicRoutes.contains(currentLocation);
|
||||||
|
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
return currentLocation == AppRoutes.login ? null : AppRoutes.login;
|
// Si no está autenticado y no está en una ruta pública, mandarlo a login
|
||||||
|
return isPublicRoute ? null : AppRoutes.login;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si está autenticado y en splash o login, ir a home.
|
// Si está autenticado e intenta ir a login o registro, mandarlo a home
|
||||||
if (isAuthenticated &&
|
if (isAuthenticated && isPublicRoute) {
|
||||||
(currentLocation == AppRoutes.login ||
|
|
||||||
currentLocation == AppRoutes.splash)) {
|
|
||||||
return AppRoutes.home;
|
return AppRoutes.home;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,21 +58,13 @@ GoRouter createRouter(AuthBloc authBloc) {
|
|||||||
builder: (context, state) => const LoginScreen(),
|
builder: (context, state) => const LoginScreen(),
|
||||||
),
|
),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: AppRoutes.home,
|
path: AppRoutes.register, // 📍 Usando la constante limpia
|
||||||
builder: (context, state) => const HomeScreenPlaceholder(),
|
|
||||||
),
|
|
||||||
// 📍 Agrega el import arriba si te lo pide:
|
|
||||||
// import 'package:flutter_application_1/features/auth/presentation/screens/register_screen.dart';
|
|
||||||
|
|
||||||
GoRoute(
|
|
||||||
path: AppRoutes.home,
|
|
||||||
builder: (context, state) => const HomeScreenPlaceholder(),
|
|
||||||
),
|
|
||||||
// 📍 CORRECCIÓN: Usamos la constante oficial del proyecto en lugar del texto a mano
|
|
||||||
GoRoute(
|
|
||||||
path: '/register', // 📍 CORRECCIÓN: Texto plano limpio entre comillas
|
|
||||||
builder: (context, state) => const RegisterScreen(),
|
builder: (context, state) => const RegisterScreen(),
|
||||||
), // GoRoute
|
),
|
||||||
|
GoRoute(
|
||||||
|
path: AppRoutes.home,
|
||||||
|
builder: (context, state) => const HomeScreenPlaceholder(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -91,11 +81,7 @@ class _SplashScreen extends StatelessWidget {
|
|||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
const Icon(
|
const Icon(Icons.recycling_rounded, size: 72, color: Colors.white),
|
||||||
Icons.recycling_rounded,
|
|
||||||
size: 72,
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
const SizedBox(height: 24),
|
const SizedBox(height: 24),
|
||||||
Text(
|
Text(
|
||||||
'WasteNotify',
|
'WasteNotify',
|
||||||
@@ -117,7 +103,6 @@ class _SplashScreen extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Notificador que conecta el estado del BLoC con go_router.
|
/// Notificador que conecta el estado del BLoC con go_router.
|
||||||
/// Permite que el router reaccione automáticamente a cambios de sesión.
|
|
||||||
class GoRouterAuthNotifier extends ChangeNotifier {
|
class GoRouterAuthNotifier extends ChangeNotifier {
|
||||||
final AuthBloc _authBloc;
|
final AuthBloc _authBloc;
|
||||||
late final StreamSubscription<AuthState> _subscription;
|
late final StreamSubscription<AuthState> _subscription;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
import '../../../../core/router/app_router.dart'; // 📍 Importación de rutas verificada
|
||||||
|
|
||||||
import '../../../../core/theme/app_theme.dart';
|
import '../../../../core/theme/app_theme.dart';
|
||||||
import '../bloc/auth_bloc.dart';
|
import '../bloc/auth_bloc.dart';
|
||||||
@@ -36,7 +37,8 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
);
|
);
|
||||||
_fadeAnim = CurvedAnimation(parent: _animController, curve: Curves.easeOut);
|
_fadeAnim = CurvedAnimation(parent: _animController, curve: Curves.easeOut);
|
||||||
_slideAnim = Tween<Offset>(begin: const Offset(0, 0.06), end: Offset.zero)
|
_slideAnim = Tween<Offset>(begin: const Offset(0, 0.06), end: Offset.zero)
|
||||||
.animate(CurvedAnimation(parent: _animController, curve: Curves.easeOutCubic));
|
.animate(CurvedAnimation(
|
||||||
|
parent: _animController, curve: Curves.easeOutCubic));
|
||||||
_animController.forward();
|
_animController.forward();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +70,8 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
SnackBar(
|
SnackBar(
|
||||||
content: Row(
|
content: Row(
|
||||||
children: [
|
children: [
|
||||||
const Icon(Icons.error_outline, color: Colors.white, size: 18),
|
const Icon(Icons.error_outline,
|
||||||
|
color: Colors.white, size: 18),
|
||||||
const SizedBox(width: 10),
|
const SizedBox(width: 10),
|
||||||
Expanded(child: Text(state.message)),
|
Expanded(child: Text(state.message)),
|
||||||
],
|
],
|
||||||
@@ -104,7 +107,8 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
_buildDemoHint(),
|
_buildDemoHint(),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Text(
|
Center(
|
||||||
|
child: Text(
|
||||||
'Sistema Municipal de Recolección Residencial\nCelaya, Guanajuato',
|
'Sistema Municipal de Recolección Residencial\nCelaya, Guanajuato',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
@@ -112,6 +116,7 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
color: Colors.grey.shade600,
|
color: Colors.grey.shade600,
|
||||||
height: 1.3),
|
height: 1.3),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
const SizedBox(height: 48),
|
const SizedBox(height: 48),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -163,7 +168,6 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
: null,
|
: null,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
|
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _passwordController,
|
controller: _passwordController,
|
||||||
obscureText: _obscurePassword,
|
obscureText: _obscurePassword,
|
||||||
@@ -175,7 +179,8 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
suffixIcon: IconButton(
|
suffixIcon: IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
_obscurePassword ? Icons.visibility : Icons.visibility_off),
|
_obscurePassword ? Icons.visibility : Icons.visibility_off),
|
||||||
onPressed: () => setState(() => _obscurePassword = !_obscurePassword),
|
onPressed: () =>
|
||||||
|
setState(() => _obscurePassword = !_obscurePassword),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
validator: (value) => (value == null || value.isEmpty)
|
validator: (value) => (value == null || value.isEmpty)
|
||||||
@@ -183,7 +188,6 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
: null,
|
: null,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 32),
|
const SizedBox(height: 32),
|
||||||
|
|
||||||
BlocBuilder<AuthBloc, AuthState>(
|
BlocBuilder<AuthBloc, AuthState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
final isLoading = state is AuthLoading;
|
final isLoading = state is AuthLoading;
|
||||||
@@ -200,7 +204,8 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
? []
|
? []
|
||||||
: [
|
: [
|
||||||
BoxShadow(
|
BoxShadow(
|
||||||
color: AppTheme.leafGreen.withValues(alpha: 0.3),
|
color:
|
||||||
|
AppTheme.leafGreen.withValues(alpha: 0.3),
|
||||||
blurRadius: 16,
|
blurRadius: 16,
|
||||||
offset: const Offset(0, 6),
|
offset: const Offset(0, 6),
|
||||||
),
|
),
|
||||||
@@ -220,43 +225,26 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
),
|
),
|
||||||
child: isLoading
|
child: isLoading
|
||||||
? const SizedBox(
|
? const SizedBox(
|
||||||
height: 22,
|
height: 20,
|
||||||
width: 22,
|
width: 20,
|
||||||
child: CircularProgressIndicator(
|
child: CircularProgressIndicator(
|
||||||
strokeWidth: 2.5,
|
color: Colors.white, strokeWidth: 2),
|
||||||
valueColor: AlwaysStoppedAnimation(Colors.white),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
: const Row(
|
: const Text('Iniciar Sesión',
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
style: TextStyle(color: Colors.white)),
|
||||||
children: [
|
|
||||||
Icon(Icons.login_rounded,
|
|
||||||
size: 20, color: Colors.white),
|
|
||||||
SizedBox(width: 10),
|
|
||||||
Text(
|
|
||||||
'Ingresar de forma segura',
|
|
||||||
style: TextStyle(
|
|
||||||
color: Colors.white,
|
|
||||||
fontSize: 15,
|
|
||||||
fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(height: 16),
|
||||||
|
// 📍 BOTÓN DE REGISTRO CORREGIDO (child al final)
|
||||||
const SizedBox(height: 20),
|
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () => context.push('/register'),
|
onPressed: () => context.push(AppRoutes.register),
|
||||||
child: const Text(
|
style: TextButton.styleFrom(
|
||||||
'¿No tienes una cuenta ciudadana? Regístrate aquí',
|
foregroundColor: AppTheme.leafGreen,
|
||||||
style: TextStyle(
|
textStyle: const TextStyle(
|
||||||
color: AppTheme.leafGreen,
|
fontSize: 14, fontWeight: FontWeight.w500),
|
||||||
fontWeight: FontWeight.w700,
|
|
||||||
fontSize: 13,
|
|
||||||
),
|
),
|
||||||
|
child: const Text('¿No tienes cuenta? Regístrate aquí'),
|
||||||
),
|
),
|
||||||
)
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -266,10 +254,6 @@ class _LoginScreenState extends State<LoginScreen>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildDemoHint() {
|
// Placeholders para evitar errores si no están definidos
|
||||||
return const Text(
|
Widget _buildDemoHint() => const SizedBox.shrink();
|
||||||
'Credenciales de demo: ciudadano@ejemplo.com / password123',
|
|
||||||
style: TextStyle(fontSize: 12, color: Colors.black54),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,31 @@ class RegisterScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class _RegisterScreenState extends State<RegisterScreen> {
|
class _RegisterScreenState extends State<RegisterScreen> {
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
String _selectedColonia = 'Centro';
|
late String _selectedColonia; // 📍 Se mantiene late pero se inicializa abajo
|
||||||
final _nameController = TextEditingController();
|
final _nameController = TextEditingController();
|
||||||
final _emailController = TextEditingController();
|
final _emailController = TextEditingController();
|
||||||
final _passwordController = TextEditingController();
|
final _passwordController = TextEditingController();
|
||||||
|
|
||||||
|
// 📍 CORRECCIÓN CRÍTICA: Asignar el valor inicial antes del método build
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
if (MockWasteData.schedules.isNotEmpty) {
|
||||||
|
// Evita problemas de aserción tomando el primer valor real de tus mocks
|
||||||
|
_selectedColonia = MockWasteData.schedules.first.colonia;
|
||||||
|
} else {
|
||||||
|
_selectedColonia = ''; // Respaldo por si la lista estuviera vacía
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_nameController.dispose();
|
||||||
|
_emailController.dispose();
|
||||||
|
_passwordController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -38,26 +58,26 @@ class _RegisterScreenState extends State<RegisterScreen> {
|
|||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _nameController,
|
controller: _nameController,
|
||||||
decoration: const InputDecoration(labelText: 'Nombre Completo', border: OutlineInputBorder()),
|
decoration: const InputDecoration(labelText: 'Nombre Completo', border: OutlineInputBorder()),
|
||||||
validator: (v) => v!.isEmpty ? 'Ingresa tu nombre' : null,
|
validator: (v) => v == null || v.isEmpty ? 'Ingresa tu nombre' : null,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _emailController,
|
controller: _emailController,
|
||||||
decoration: const InputDecoration(labelText: 'Correo o Teléfono', border: OutlineInputBorder()),
|
decoration: const InputDecoration(labelText: 'Correo o Teléfono', border: OutlineInputBorder()),
|
||||||
validator: (v) => v!.isEmpty ? 'Ingresa tus datos' : null,
|
validator: (v) => v == null || v.isEmpty ? 'Ingresa tus datos' : null,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _passwordController,
|
controller: _passwordController,
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
decoration: const InputDecoration(labelText: 'Contraseña', border: OutlineInputBorder()),
|
decoration: const InputDecoration(labelText: 'Contraseña', border: OutlineInputBorder()),
|
||||||
validator: (v) => v!.length < 6 ? 'Mínimo 6 caracteres' : null,
|
validator: (v) => v == null || v.length < 6 ? 'Mínimo 6 caracteres' : null,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
// Dropdown para seleccionar la Colonia (Requisito MVP)
|
// Dropdown para seleccionar la Colonia
|
||||||
DropdownButtonFormField<String>(
|
DropdownButtonFormField<String>(
|
||||||
initialValue: _selectedColonia,
|
value: _selectedColonia,
|
||||||
decoration: const InputDecoration(labelText: 'Selecciona tu Colonia / Domicilio', border: OutlineInputBorder()),
|
decoration: const InputDecoration(labelText: 'Selecciona tu Colonia / Domicilio', border: OutlineInputBorder()),
|
||||||
items: MockWasteData.schedules.map((zone) {
|
items: MockWasteData.schedules.map((zone) {
|
||||||
return DropdownMenuItem(value: zone.colonia, child: Text(zone.colonia));
|
return DropdownMenuItem(value: zone.colonia, child: Text(zone.colonia));
|
||||||
@@ -82,7 +102,6 @@ class _RegisterScreenState extends State<RegisterScreen> {
|
|||||||
style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 16), backgroundColor: Colors.green),
|
style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 16), backgroundColor: Colors.green),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
// Simulación de Registro Exitoso: Navega al Home enviando la colonia elegida
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(content: Text('Cuenta creada con éxito (Modo Simulación)')),
|
const SnackBar(content: Text('Cuenta creada con éxito (Modo Simulación)')),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user