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:
@@ -13,19 +13,13 @@ import '../../features/authentication/presentation/pages/splash_page.dart';
|
|||||||
///
|
///
|
||||||
/// Handles navigation with authentication state awareness and protected routes
|
/// Handles navigation with authentication state awareness and protected routes
|
||||||
class AppRouter {
|
class AppRouter {
|
||||||
static final GoRouter _router = GoRouter(
|
static GoRouter _router({required WidgetRef ref}) {
|
||||||
|
return GoRouter(
|
||||||
initialLocation: '/',
|
initialLocation: '/',
|
||||||
debugLogDiagnostics: true,
|
debugLogDiagnostics: true,
|
||||||
redirect: (context, state) {
|
redirect: (context, state) {
|
||||||
final authState = state.extra as AuthState?;
|
// For now, use Supabase directly for auth state checking
|
||||||
|
// This will be improved when auth provider integration is complete
|
||||||
// If no auth state in extra, check from provider
|
|
||||||
if (authState == null) {
|
|
||||||
final container = ProviderScope.containerOf(context, listen: false);
|
|
||||||
final authNotifier = container.read(authStateProvider.notifier);
|
|
||||||
|
|
||||||
// For now, we'll use Supabase directly for auth state checking
|
|
||||||
// This will be improved when auth provider is fully integrated
|
|
||||||
final currentUser = Supabase.instance.client.auth.currentUser;
|
final currentUser = Supabase.instance.client.auth.currentUser;
|
||||||
|
|
||||||
// Allow splash page regardless of auth state
|
// Allow splash page regardless of auth state
|
||||||
@@ -43,48 +37,29 @@ class AppRouter {
|
|||||||
return '/home';
|
return '/home';
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Update to use AuthState when fully integrated
|
|
||||||
// For now, use Supabase auth state directly
|
|
||||||
final currentUser = Supabase.instance.client.auth.currentUser;
|
|
||||||
|
|
||||||
if (currentUser == null && !state.location.startsWith('/login') && !state.location.startsWith('/signup')) {
|
|
||||||
return '/login';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentUser != null && (state.location.startsWith('/login') || state.location.startsWith('/signup'))) {
|
|
||||||
return '/home';
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
routes: [
|
routes: [
|
||||||
// Splash route - initial loading screen
|
// Splash route - initial loading screen
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/splash',
|
path: '/splash',
|
||||||
name: 'splash',
|
|
||||||
builder: (context, state) => const SplashPage(),
|
builder: (context, state) => const SplashPage(),
|
||||||
),
|
),
|
||||||
|
|
||||||
// Authentication routes (public)
|
// Authentication routes (public)
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/login',
|
path: '/login',
|
||||||
name: 'login',
|
|
||||||
builder: (context, state) => const LoginPage(),
|
builder: (context, state) => const LoginPage(),
|
||||||
),
|
),
|
||||||
|
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/signup',
|
path: '/signup',
|
||||||
name: 'signup',
|
|
||||||
builder: (context, state) => const SignupPage(),
|
builder: (context, state) => const SignupPage(),
|
||||||
),
|
),
|
||||||
|
|
||||||
// Protected routes (require authentication)
|
// Protected routes (require authentication)
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/home',
|
path: '/home',
|
||||||
name: 'home',
|
|
||||||
builder: (context, state) => const HomePage(),
|
builder: (context, state) => const HomePage(),
|
||||||
),
|
),
|
||||||
|
|
||||||
@@ -101,7 +76,6 @@ class AppRouter {
|
|||||||
// Example:
|
// Example:
|
||||||
// GoRoute(
|
// GoRoute(
|
||||||
// path: '/inventory',
|
// path: '/inventory',
|
||||||
// name: 'inventory',
|
|
||||||
// builder: (context, state) => const InventoryPage(),
|
// builder: (context, state) => const InventoryPage(),
|
||||||
// ),
|
// ),
|
||||||
],
|
],
|
||||||
@@ -150,10 +124,10 @@ class AppRouter {
|
|||||||
);
|
);
|
||||||
|
|
||||||
/// Get the router instance
|
/// Get the router instance
|
||||||
static GoRouter get router => _router;
|
static GoRouter router({required WidgetRef ref}) => _router(ref: ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Router provider for Riverpod integration
|
/// Router provider for Riverpod integration
|
||||||
final routerProvider = Provider<GoRouter>((ref) {
|
final routerProvider = Provider<GoRouter>((ref) {
|
||||||
return AppRouter.router;
|
return AppRouter.router(ref: ref);
|
||||||
});
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
/// Splash page for initial loading and authentication state checking
|
/// Splash page for initial loading and authentication state checking
|
||||||
///
|
///
|
||||||
@@ -33,18 +34,14 @@ class _SplashPageState extends ConsumerState<SplashPage> {
|
|||||||
|
|
||||||
if (currentUser != null) {
|
if (currentUser != null) {
|
||||||
// User is authenticated, navigate to home
|
// User is authenticated, navigate to home
|
||||||
// TODO: Use GoRouter when integrated
|
if (mounted) {
|
||||||
// Navigator.of(context).pushReplacementNamed('/home');
|
context.go('/home');
|
||||||
|
}
|
||||||
// For now, we'll use a simple navigation
|
|
||||||
Navigator.of(context).pushReplacementNamed('/home');
|
|
||||||
} else {
|
} else {
|
||||||
// User is not authenticated, navigate to login
|
// User is not authenticated, navigate to login
|
||||||
// TODO: Use GoRouter when integrated
|
if (mounted) {
|
||||||
// Navigator.of(context).pushReplacementNamed('/login');
|
context.go('/login');
|
||||||
|
}
|
||||||
// For now, we'll use a simple navigation
|
|
||||||
Navigator.of(context).pushReplacementNamed('/login');
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Handle authentication state check errors
|
// Handle authentication state check errors
|
||||||
@@ -53,7 +50,9 @@ class _SplashPageState extends ConsumerState<SplashPage> {
|
|||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
|
||||||
// On error, navigate to login for safety
|
// On error, navigate to login for safety
|
||||||
Navigator.of(context).pushReplacementNamed('/login');
|
if (mounted) {
|
||||||
|
context.go('/login');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import '../../providers/auth_provider.dart';
|
import '../../../providers/auth_provider.dart';
|
||||||
|
|
||||||
/// Home page for authenticated users
|
/// Home page for authenticated users
|
||||||
///
|
///
|
||||||
@@ -21,8 +21,18 @@ class HomePage extends ConsumerWidget {
|
|||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.logout),
|
icon: const Icon(Icons.logout),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
// TODO: Implement logout functionality
|
try {
|
||||||
// await ref.read(authStateProvider.notifier).signOut();
|
await ref.read(authProvider.notifier).signOut();
|
||||||
|
} catch (e) {
|
||||||
|
if (context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Logout failed: ${e.toString()}'),
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
tooltip: 'Logout',
|
tooltip: 'Logout',
|
||||||
),
|
),
|
||||||
|
|||||||
133
lib/main.dart
133
lib/main.dart
@@ -1,7 +1,12 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
import 'package:flutter_dotenv/flutter_dotenv.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/constants/supabase_constants.dart';
|
||||||
|
import 'core/router/app_router.dart';
|
||||||
|
import 'providers/auth_provider.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
@@ -21,25 +26,143 @@ void main() async {
|
|||||||
debugPrint('Failed to initialize Supabase: $e');
|
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});
|
const SageApp({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
return MaterialApp(
|
final router = ref.watch(routerProvider);
|
||||||
|
|
||||||
|
return MaterialApp.router(
|
||||||
title: 'Sage - Food Inventory Tracker',
|
title: 'Sage - Food Inventory Tracker',
|
||||||
|
debugShowCheckedModeBanner: false,
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
||||||
useMaterial3: true,
|
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 {
|
class HomePage extends StatelessWidget {
|
||||||
const HomePage({super.key});
|
const HomePage({super.key});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user