import 'package:flutter/material.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:go_router/go_router.dart'; import 'core/constants/supabase_constants.dart'; import 'core/router/app_router.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); try { // Load environment variables await SupabaseConstants.initialize(); // Initialize Supabase await Supabase.initialize( url: SupabaseConstants.supabaseUrl, anonKey: SupabaseConstants.supabaseAnonKey, ); debugPrint('Supabase initialized successfully'); } catch (e) { debugPrint('Failed to initialize Supabase: $e'); } runApp(const SageApp()); } class SageApp extends StatelessWidget { const SageApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp.router( title: 'Sage - Food Inventory Tracker', debugShowCheckedModeBanner: false, theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.green), useMaterial3: true, ), routerConfig: AppRouter.router, // Configure deep linking for password reset onGenerateTitle: (context) => 'Sage - Food Inventory Tracker', ); builder: (context, child) { // Set up error handling for the entire app return ErrorBoundary( child: child!, ); }, ); } } /// Error boundary widget for catching and displaying errors class ErrorBoundary extends StatelessWidget { final Widget child; const ErrorBoundary({ super.key, required this.child, }); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: child, builder: (context, widget) { ErrorWidget.builder = (FlutterErrorDetails errorDetails) { return CustomErrorWidget(errorDetails: errorDetails); }; return widget!; }, ); } } /// Custom error widget for better error display class CustomErrorWidget extends StatelessWidget { final FlutterErrorDetails errorDetails; const CustomErrorWidget({ super.key, required this.errorDetails, }); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.red[50], appBar: AppBar( title: const Text('Error'), backgroundColor: Colors.red[100], ), body: Center( child: Padding( padding: const EdgeInsets.all(16), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.error_outline, size: 64, color: Colors.red, ), const SizedBox(height: 16), const Text( 'An error occurred', style: TextStyle( fontSize: 20, fontWeight: FontWeight.bold, color: Colors.red, ), ), const SizedBox(height: 8), Text( 'Please restart the app and try again.', style: TextStyle( fontSize: 16, color: Colors.red[700], ), textAlign: TextAlign.center, ), if (debugMode) ...[ const SizedBox(height: 16), Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.red[100], borderRadius: BorderRadius.circular(8), ), child: Text( errorDetails.toString(), style: const TextStyle( fontSize: 12, fontFamily: 'monospace', ), ), ), ], const SizedBox(height: 24), ElevatedButton( onPressed: () { // Attempt to restart the app by navigating to splash GoRouter.of(context).go('/splash'); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.red, foregroundColor: Colors.white, ), child: const Text('Restart App'), ), ], ), ), ), ); } bool get debugMode => !const bool.fromEnvironment('dart.vm.product'); } class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: const Text('Sage'), ), body: const Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'Welcome to Sage', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, ), ), SizedBox(height: 16), Text( 'Your collaborative household food inventory tracker', style: TextStyle( fontSize: 16, color: Colors.grey, ), ), SizedBox(height: 32), Text( 'Supabase connection: ✓ Ready', style: TextStyle( fontSize: 14, color: Colors.green, fontWeight: FontWeight.w500, ), ), ], ), ), ); } }