import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'providers/auth_provider.dart'; import 'providers/accounts_provider.dart'; import 'providers/transactions_provider.dart'; import 'screens/backend_config_screen.dart'; import 'screens/login_screen.dart'; import 'screens/dashboard_screen.dart'; import 'services/api_config.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await ApiConfig.initialize(); runApp(const SureApp()); } class SureApp extends StatelessWidget { const SureApp({super.key}); @override Widget build(BuildContext context) { return MultiProvider( providers: [ ChangeNotifierProvider(create: (_) => AuthProvider()), ChangeNotifierProvider(create: (_) => AccountsProvider()), ChangeNotifierProvider(create: (_) => TransactionsProvider()), ], child: MaterialApp( title: 'Sure Finance', debugShowCheckedModeBanner: false, theme: ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: const Color(0xFF6366F1), brightness: Brightness.light, ), useMaterial3: true, appBarTheme: const AppBarTheme( centerTitle: true, elevation: 0, ), cardTheme: CardThemeData( elevation: 2, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), inputDecorationTheme: InputDecorationTheme( border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), ), filled: true, ), elevatedButtonTheme: ElevatedButtonThemeData( style: ElevatedButton.styleFrom( minimumSize: const Size(double.infinity, 50), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), ), ), darkTheme: ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: const Color(0xFF6366F1), brightness: Brightness.dark, ), useMaterial3: true, appBarTheme: const AppBarTheme( centerTitle: true, elevation: 0, ), cardTheme: CardThemeData( elevation: 2, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), inputDecorationTheme: InputDecorationTheme( border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), ), filled: true, ), elevatedButtonTheme: ElevatedButtonThemeData( style: ElevatedButton.styleFrom( minimumSize: const Size(double.infinity, 50), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), ), ), themeMode: ThemeMode.system, routes: { '/config': (context) => const BackendConfigScreen(), '/login': (context) => const LoginScreen(), '/dashboard': (context) => const DashboardScreen(), }, home: const AppWrapper(), ), ); } } class AppWrapper extends StatefulWidget { const AppWrapper({super.key}); @override State createState() => _AppWrapperState(); } class _AppWrapperState extends State { bool _isCheckingConfig = true; bool _hasBackendUrl = false; @override void initState() { super.initState(); _checkBackendConfig(); } Future _checkBackendConfig() async { final hasUrl = await ApiConfig.initialize(); if (mounted) { setState(() { _hasBackendUrl = hasUrl; _isCheckingConfig = false; }); } } void _onBackendConfigSaved() { setState(() { _hasBackendUrl = true; }); } void _goToBackendConfig() { setState(() { _hasBackendUrl = false; }); } @override Widget build(BuildContext context) { if (_isCheckingConfig) { return const Scaffold( body: Center( child: CircularProgressIndicator(), ), ); } if (!_hasBackendUrl) { return BackendConfigScreen( onConfigSaved: _onBackendConfigSaved, ); } return Consumer( builder: (context, authProvider, _) { // Only show loading spinner during initial auth check if (authProvider.isInitializing) { return const Scaffold( body: Center( child: CircularProgressIndicator(), ), ); } if (authProvider.isAuthenticated) { return const DashboardScreen(); } return LoginScreen( onGoToSettings: _goToBackendConfig, ); }, ); } }