From d091b4412da469b5ac0549d62ac10def4e3ad5c0 Mon Sep 17 00:00:00 2001 From: Nabil Derouiche Date: Mon, 20 Apr 2026 22:28:48 +0100 Subject: [PATCH] fix: stable GoRouter instance + spinner on dashboard initial load --- frontend/lib/main.dart | 61 ++++++++++------------ frontend/lib/screens/dashboard_screen.dart | 2 +- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/frontend/lib/main.dart b/frontend/lib/main.dart index f13fc2a..6b14a2c 100644 --- a/frontend/lib/main.dart +++ b/frontend/lib/main.dart @@ -16,7 +16,6 @@ import 'screens/ventes_screen.dart'; import 'screens/achats_screen.dart'; import 'screens/production_screen.dart'; import 'screens/stock_screen.dart'; -import 'widgets/app_drawer.dart'; void main() { runApp( @@ -35,15 +34,28 @@ void main() { ); } -class RayhanApp extends StatelessWidget { +class RayhanApp extends StatefulWidget { const RayhanApp({super.key}); @override - Widget build(BuildContext context) { - final authProvider = context.watch(); + State createState() => _RayhanAppState(); +} - final router = GoRouter( +class _RayhanAppState extends State { + late final 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'; @@ -61,7 +73,16 @@ class RayhanApp extends StatelessWidget { GoRoute(path: '/stock', builder: (_, __) => const StockScreen()), ], ); + } + @override + void dispose() { + _router.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { return MaterialApp.router( title: 'Rayhan ERP', debugShowCheckedModeBanner: false, @@ -73,35 +94,7 @@ class RayhanApp extends StatelessWidget { useMaterial3: true, fontFamily: 'Roboto', ), - routerConfig: router, - ); - } -} - -class _PlaceholderScreen extends StatelessWidget { - final String title; - final String route; - const _PlaceholderScreen({required this.title, required this.route}); - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: Text(title)), - drawer: AppDrawer(currentRoute: route), - body: Center( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - Icon(Icons.construction_outlined, size: 64, color: Colors.grey[400]), - const SizedBox(height: 16), - Text('Module $title', - style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), - const SizedBox(height: 8), - Text('En cours de développement', - style: TextStyle(color: Colors.grey[600])), - ], - ), - ), + routerConfig: _router, ); } } diff --git a/frontend/lib/screens/dashboard_screen.dart b/frontend/lib/screens/dashboard_screen.dart index 95ccc69..84fb553 100644 --- a/frontend/lib/screens/dashboard_screen.dart +++ b/frontend/lib/screens/dashboard_screen.dart @@ -90,7 +90,7 @@ class _DashboardScreenState extends State { } final kpi = provider.kpi; - if (kpi == null) return const SizedBox.shrink(); + if (kpi == null) return const Center(child: CircularProgressIndicator()); return RefreshIndicator( onRefresh: () => context.read().load(),