107 lines
4.1 KiB
Dart
107 lines
4.1 KiB
Dart
// ================================================================
|
|
// main.dart — Punto de entrada de la aplicación
|
|
// ================================================================
|
|
//
|
|
// RESPONSABILIDADES:
|
|
// 1. Inicializar Firebase (requerido antes de runApp)
|
|
// 2. Configurar el tema visual de la app
|
|
// 3. Definir el router básico de pantallas
|
|
//
|
|
// ATAJO DE HACKATHON:
|
|
// Sin state management complejo (Riverpod/Bloc). Usamos
|
|
// setState + shared_preferences para el MVP. Suficiente.
|
|
// ================================================================
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'screens/login_screen.dart';
|
|
import 'screens/home_screen.dart';
|
|
import 'screens/route_list_screen.dart';
|
|
import 'firebase_options.dart'; // Opcional si usas FlutterFire CLI para generar opciones
|
|
import 'screens/info_screen.dart';
|
|
|
|
// ----------------------------------------------------------------
|
|
// HANDLER DE MENSAJES EN BACKGROUND
|
|
//
|
|
// Firebase requiere que este handler sea una función TOP-LEVEL
|
|
// (fuera de cualquier clase). Se ejecuta cuando llega una
|
|
// notificación y la app está en segundo plano o cerrada.
|
|
// ----------------------------------------------------------------
|
|
@pragma('vm:entry-point')
|
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
// IMPORTANTE: Si el handler hace operaciones async pesadas,
|
|
// también hay que inicializar Firebase aquí.
|
|
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
|
debugPrint('📬 [Background] Mensaje recibido: ${message.messageId}');
|
|
// TODO: Aquí puedes guardar el mensaje en local storage para mostrarlo
|
|
// después cuando el usuario abra la app.
|
|
}
|
|
|
|
// ----------------------------------------------------------------
|
|
// FUNCIÓN MAIN — Punto de entrada real de Flutter
|
|
// ----------------------------------------------------------------
|
|
void main() async {
|
|
// WidgetsFlutterBinding.ensureInitialized() es OBLIGATORIO
|
|
// antes de cualquier código async en main(). Inicializa el
|
|
// binding entre Flutter y el sistema operativo.
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
try {
|
|
// CAMBIAMOS EL BLOQUE COMENTADO POR ESTO:
|
|
// Al no pasarle opciones, lee el google-services.json automáticamente en Android.
|
|
await Firebase.initializeApp();
|
|
|
|
// Vinculamos el handler de notificaciones en segundo plano
|
|
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
|
|
|
|
debugPrint('🔥 Firebase inicializado con éxito en modo nativo.');
|
|
} catch (e) {
|
|
debugPrint('❌ Error al inicializar Firebase: $e');
|
|
}
|
|
|
|
runApp(const ResiduosApp());
|
|
}
|
|
|
|
// ----------------------------------------------------------------
|
|
// WIDGET RAÍZ DE LA APLICACIÓN
|
|
// ----------------------------------------------------------------
|
|
class ResiduosApp extends StatelessWidget {
|
|
const ResiduosApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Recolección Inteligente',
|
|
debugShowCheckedModeBanner: false, // Quita el banner rojo de DEBUG
|
|
|
|
// --------------------------------------------------------
|
|
// TEMA VISUAL
|
|
// Verde oscuro = sostenibilidad y medio ambiente.
|
|
// Fácil de cambiar para el pitch/demo.
|
|
// --------------------------------------------------------
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(
|
|
seedColor: const Color(0xFF2E7D32), // Verde oscuro
|
|
brightness: Brightness.light,
|
|
),
|
|
useMaterial3: true,
|
|
fontFamily: 'Roboto',
|
|
),
|
|
|
|
// --------------------------------------------------------
|
|
// ROUTER SIMPLE
|
|
// Dos rutas: login (/) y home (/home).
|
|
// Pasamos el usuario_id a /home via arguments.
|
|
// --------------------------------------------------------
|
|
initialRoute: '/',
|
|
routes: {
|
|
'/': (context) => const LoginScreen(),
|
|
'/home': (context) => const HomeScreen(),
|
|
'/routes': (context) => const RouteListScreen(),
|
|
'/info': (context) => const InfoScreen(),
|
|
},
|
|
);
|
|
}
|
|
}
|