fix: stable GoRouter instance + spinner on dashboard initial load

This commit is contained in:
Nabil Derouiche 2026-04-20 22:28:48 +01:00
parent 1a859fc5b9
commit d091b4412d
2 changed files with 28 additions and 35 deletions

View File

@ -16,7 +16,6 @@ import 'screens/ventes_screen.dart';
import 'screens/achats_screen.dart'; import 'screens/achats_screen.dart';
import 'screens/production_screen.dart'; import 'screens/production_screen.dart';
import 'screens/stock_screen.dart'; import 'screens/stock_screen.dart';
import 'widgets/app_drawer.dart';
void main() { void main() {
runApp( runApp(
@ -35,15 +34,28 @@ void main() {
); );
} }
class RayhanApp extends StatelessWidget { class RayhanApp extends StatefulWidget {
const RayhanApp({super.key}); const RayhanApp({super.key});
@override @override
Widget build(BuildContext context) { State<RayhanApp> createState() => _RayhanAppState();
final authProvider = context.watch<AuthProvider>(); }
final router = GoRouter( class _RayhanAppState extends State<RayhanApp> {
late final GoRouter _router;
@override
void didChangeDependencies() {
super.didChangeDependencies();
}
@override
void initState() {
super.initState();
final authProvider = context.read<AuthProvider>();
_router = GoRouter(
initialLocation: '/login', initialLocation: '/login',
refreshListenable: authProvider,
redirect: (context, state) { redirect: (context, state) {
final loggedIn = authProvider.isAuthenticated; final loggedIn = authProvider.isAuthenticated;
final onLogin = state.matchedLocation == '/login'; final onLogin = state.matchedLocation == '/login';
@ -61,7 +73,16 @@ class RayhanApp extends StatelessWidget {
GoRoute(path: '/stock', builder: (_, __) => const StockScreen()), GoRoute(path: '/stock', builder: (_, __) => const StockScreen()),
], ],
); );
}
@override
void dispose() {
_router.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp.router( return MaterialApp.router(
title: 'Rayhan ERP', title: 'Rayhan ERP',
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
@ -73,35 +94,7 @@ class RayhanApp extends StatelessWidget {
useMaterial3: true, useMaterial3: true,
fontFamily: 'Roboto', fontFamily: 'Roboto',
), ),
routerConfig: router, 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])),
],
),
),
); );
} }
} }

View File

@ -90,7 +90,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
} }
final kpi = provider.kpi; final kpi = provider.kpi;
if (kpi == null) return const SizedBox.shrink(); if (kpi == null) return const Center(child: CircularProgressIndicator());
return RefreshIndicator( return RefreshIndicator(
onRefresh: () => context.read<DashboardProvider>().load(), onRefresh: () => context.read<DashboardProvider>().load(),