import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'screens/main_shell.dart'; import 'screens/user_agreement_screen.dart'; import 'services/app_state.dart'; import 'theme/app_theme.dart'; import 'widgets/ad_free_upsell_dialog.dart'; void main() { runApp(const FuelTaxTrackerApp()); } /// A stable handle on the navigator so [AppState.onAdWatched] can show the /// "remove ads" dialog from wherever the app happens to be — it fires from /// deep inside a background sync, not from a widget's own build method, so /// there's no local [BuildContext] to reach for at that point. final navigatorKey = GlobalKey(); class FuelTaxTrackerApp extends StatelessWidget { const FuelTaxTrackerApp({super.key}); @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (_) => AppState()..init(), child: Consumer( builder: (context, appState, _) { appState.onAdWatched ??= () { final dialogContext = navigatorKey.currentContext; if (dialogContext != null) showAdFreeUpsellDialog(dialogContext); }; return MaterialApp( navigatorKey: navigatorKey, title: 'Fuel Tax Tracker', theme: AppTheme.light, darkTheme: AppTheme.dark, themeMode: appState.themeMode, home: const AppRoot(), ); }, ), ); } } class AppRoot extends StatelessWidget { const AppRoot({super.key}); @override Widget build(BuildContext context) { return Consumer( builder: (context, appState, _) { if (appState.isLoading) { return const Scaffold( body: Center(child: CircularProgressIndicator()), ); } if (appState.initError != null) { return Scaffold( body: Center( child: Padding( padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.error_outline, size: 48, color: Theme.of(context).colorScheme.error), const SizedBox(height: 16), Text( 'Could not start the app:\n${appState.initError}', textAlign: TextAlign.center, ), const SizedBox(height: 16), FilledButton( onPressed: () => appState.init(), child: const Text('Retry'), ), ], ), ), ), ); } if (!appState.hasAcceptedUserAgreement) { return const UserAgreementScreen(); } return const MainShell(); }, ); } }