diff --git a/frontend/lib/main.dart b/frontend/lib/main.dart index 6b14a2c..e045827 100644 --- a/frontend/lib/main.dart +++ b/frontend/lib/main.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:go_router/go_router.dart'; +import 'package:intl/date_symbol_data_local.dart'; import 'providers/auth_provider.dart'; import 'providers/dashboard_provider.dart'; @@ -17,7 +18,10 @@ import 'screens/achats_screen.dart'; import 'screens/production_screen.dart'; import 'screens/stock_screen.dart'; -void main() { +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + await initializeDateFormatting('fr_FR'); + await initializeDateFormatting('fr_TN'); runApp( MultiProvider( providers: [ @@ -42,47 +46,45 @@ class RayhanApp extends StatefulWidget { } class _RayhanAppState extends State { - late final GoRouter _router; + GoRouter? _router; @override void didChangeDependencies() { super.didChangeDependencies(); - } - - @override - void initState() { - super.initState(); - final authProvider = context.read(); - _router = GoRouter( - initialLocation: '/login', - refreshListenable: authProvider, - redirect: (context, state) { - final loggedIn = authProvider.isAuthenticated; - final onLogin = state.matchedLocation == '/login'; - if (!loggedIn && !onLogin) return '/login'; - if (loggedIn && onLogin) return '/dashboard'; - return null; - }, - routes: [ - GoRoute(path: '/login', builder: (_, __) => const LoginScreen()), - GoRoute(path: '/dashboard', builder: (_, __) => const DashboardScreen()), - GoRoute(path: '/articles', builder: (_, __) => const ArticlesScreen()), - GoRoute(path: '/ventes', builder: (_, __) => const VentesScreen()), - GoRoute(path: '/achats', builder: (_, __) => const AchatsScreen()), - GoRoute(path: '/production', builder: (_, __) => const ProductionScreen()), - GoRoute(path: '/stock', builder: (_, __) => const StockScreen()), - ], - ); + if (_router == null) { + final auth = Provider.of(context, listen: false); + _router = GoRouter( + initialLocation: '/login', + refreshListenable: auth, + redirect: (_, state) { + final loggedIn = auth.isAuthenticated; + final onLogin = state.matchedLocation == '/login'; + if (!loggedIn && !onLogin) return '/login'; + if (loggedIn && onLogin) return '/dashboard'; + return null; + }, + routes: [ + GoRoute(path: '/login', builder: (_, __) => const LoginScreen()), + GoRoute(path: '/dashboard', builder: (_, __) => const DashboardScreen()), + GoRoute(path: '/articles', builder: (_, __) => const ArticlesScreen()), + GoRoute(path: '/ventes', builder: (_, __) => const VentesScreen()), + GoRoute(path: '/achats', builder: (_, __) => const AchatsScreen()), + GoRoute(path: '/production', builder: (_, __) => const ProductionScreen()), + GoRoute(path: '/stock', builder: (_, __) => const StockScreen()), + ], + ); + } } @override void dispose() { - _router.dispose(); + _router?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { + if (_router == null) return const SizedBox.shrink(); return MaterialApp.router( title: 'Rayhan ERP', debugShowCheckedModeBanner: false, @@ -94,7 +96,7 @@ class _RayhanAppState extends State { useMaterial3: true, fontFamily: 'Roboto', ), - routerConfig: _router, + routerConfig: _router!, ); } }