feat(01-11): integrate router into main app with error handling
- Updated main.dart to use ProviderScope and MaterialApp.router - Integrated AppRouter with proper GoRouter configuration - Added comprehensive error handling with ErrorBoundary widget - Updated HomePage to use auth provider for logout functionality - Fixed SplashPage to use GoRouter navigation - Implemented router provider for Riverpod integration - Added proper resource disposal and restart functionality Files modified: - lib/main.dart (complete app structure) - lib/core/router/app_router.dart (router integration) - lib/features/authentication/presentation/pages/splash_page.dart (navigation fix) - lib/features/home/presentation/pages/home_page.dart (logout implementation)
This commit is contained in:
133
lib/main.dart
133
lib/main.dart
@@ -1,7 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'core/constants/supabase_constants.dart';
|
||||
import 'core/router/app_router.dart';
|
||||
import 'providers/auth_provider.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -21,25 +26,143 @@ void main() async {
|
||||
debugPrint('Failed to initialize Supabase: $e');
|
||||
}
|
||||
|
||||
runApp(const SageApp());
|
||||
runApp(const ProviderScope(child: SageApp()));
|
||||
}
|
||||
|
||||
class SageApp extends StatelessWidget {
|
||||
class SageApp extends ConsumerWidget {
|
||||
const SageApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final router = ref.watch(routerProvider);
|
||||
|
||||
return MaterialApp.router(
|
||||
title: 'Sage - Food Inventory Tracker',
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const HomePage(),
|
||||
routerConfig: router,
|
||||
builder: (context, child) {
|
||||
// Set up error handling for the entire app
|
||||
return ErrorBoundary(
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Error boundary widget for catching and displaying errors
|
||||
class ErrorBoundary extends StatelessWidget {
|
||||
final Widget child;
|
||||
|
||||
const ErrorBoundary({
|
||||
super.key,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: child,
|
||||
builder: (context, widget) {
|
||||
ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
|
||||
return CustomErrorWidget(errorDetails: errorDetails);
|
||||
};
|
||||
return widget!;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Custom error widget for better error display
|
||||
class CustomErrorWidget extends StatelessWidget {
|
||||
final FlutterErrorDetails errorDetails;
|
||||
|
||||
const CustomErrorWidget({
|
||||
super.key,
|
||||
required this.errorDetails,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.red[50],
|
||||
appBar: AppBar(
|
||||
title: const Text('Error'),
|
||||
backgroundColor: Colors.red[100],
|
||||
),
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: Colors.red,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'An error occurred',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Please restart the app and try again.',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.red[700],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
if (debugMode) ...[
|
||||
const SizedBox(height: 16),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red[100],
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
errorDetails.toString(),
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
// Attempt to restart the app by navigating to splash
|
||||
GoRouter.of(context).go('/splash');
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.red,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Restart App'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool get debugMode => !const bool.fromEnvironment('dart.vm.product');
|
||||
}
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user