191 lines
5.3 KiB
Dart
191 lines
5.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import 'core/config/supabase_config.dart';
|
|
import 'core/theme/app_theme.dart';
|
|
import 'core/ws_provider.dart';
|
|
import 'features/recycling_guide/presentation/screens/recycling_guide_screen.dart';
|
|
import 'features/routes/presentation/screens/routes_home_screen.dart';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
await Supabase.initialize(
|
|
url: SUPABASE_URL,
|
|
anonKey: SUPABASE_ANON_KEY,
|
|
);
|
|
|
|
runApp(const ProviderScope(child: MyApp()));
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Basura App - Notificación de Residuos',
|
|
theme: AppTheme.lightTheme,
|
|
home: const LoginScreen(),
|
|
routes: {
|
|
'/guia': (context) => const RecyclingGuideScreen(),
|
|
'/rutas': (context) => const RoutesHomeScreen(),
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class LoginScreen extends ConsumerStatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
|
final emailCtrl = TextEditingController();
|
|
final passCtrl = TextEditingController();
|
|
bool loading = false;
|
|
String? error;
|
|
|
|
@override
|
|
void dispose() {
|
|
emailCtrl.dispose();
|
|
passCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> login() async {
|
|
setState(() {
|
|
loading = true;
|
|
error = null;
|
|
});
|
|
|
|
try {
|
|
// TODO: Llamar a endpoint /auth/login del backend en lugar de Supabase directo
|
|
// Por ahora: simulación básica
|
|
if (emailCtrl.text.isNotEmpty && passCtrl.text.isNotEmpty) {
|
|
if (!mounted) return;
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(builder: (_) => const HomePage()),
|
|
);
|
|
} else {
|
|
setState(() => error = 'Email y password requeridos');
|
|
}
|
|
} catch (e) {
|
|
setState(() => error = 'Error: $e');
|
|
} finally {
|
|
setState(() => loading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Login')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
TextField(
|
|
controller: emailCtrl,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: passCtrl,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
if (error != null)
|
|
Text(error!, style: const TextStyle(color: Colors.red)),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: loading ? null : login,
|
|
child: loading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(),
|
|
)
|
|
: const Text('Ingresar'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class HomePage extends ConsumerWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final wsState = ref.watch(wsProvider);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Basura App'),
|
|
centerTitle: true,
|
|
actions: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Center(
|
|
child: Tooltip(
|
|
message: wsState.connected ? 'Conectado' : 'Desconectado',
|
|
child: Container(
|
|
width: 12,
|
|
height: 12,
|
|
decoration: BoxDecoration(
|
|
color: wsState.connected ? Colors.green : Colors.red,
|
|
shape: BoxShape.circle,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
// Estado WebSocket
|
|
if (wsState.lastMessage != null)
|
|
Container(
|
|
color: Colors.blue[100],
|
|
padding: const EdgeInsets.all(16),
|
|
child: Text('📬 ${wsState.lastMessage}'),
|
|
),
|
|
// Botones principales
|
|
Expanded(
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.pushNamed(context, '/guia'),
|
|
child: const Text('📚 Guía de Reciclaje'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.pushNamed(context, '/rutas'),
|
|
child: const Text('🚚 Rutas & ETA'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|