Files
Sage/lib/features/home/presentation/pages/home_page.dart
Dani B d4006d73ce feat(01-10): add logout functionality to home page
- Updated HomePage to use AuthProvider instead of direct Supabase calls
- Added confirmation dialog before logout
- Added loading state display during logout
- Added error display for auth errors with dismiss functionality
- Added success message for successful logout
- Enhanced accessibility and user feedback
2026-01-28 12:57:02 -05:00

206 lines
7.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../providers/auth_provider.dart';
/// Home page for authenticated users
///
/// Displays welcome message and provides logout functionality
/// This is the main landing page after successful authentication
class HomePage extends ConsumerWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final authState = ref.watch(authStateProvider);
final currentUser = authState.user;
final authNotifier = ref.read(authProvider.notifier);
return Scaffold(
return Scaffold(
appBar: AppBar(
title: const Text('Sage'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
actions: [
if (authState.isLoading)
const Padding(
padding: EdgeInsets.all(16.0),
child: SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(strokeWidth: 2),
),
)
else
IconButton(
icon: const Icon(Icons.logout),
onPressed: () async {
// Show confirmation dialog before logout
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Confirm Logout'),
content: const Text('Are you sure you want to log out?'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Logout'),
),
],
),
);
if (confirmed == true && context.mounted) {
try {
await authNotifier.signOut();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Logged out successfully'),
backgroundColor: Colors.green,
),
);
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Logout failed: ${e.toString()}'),
backgroundColor: Colors.red,
),
);
}
}
}
},
tooltip: 'Logout',
),
],
),
body: Column(
children: [
// Show auth error if present
if (authState.error != null)
Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
color: Colors.red.shade50,
child: Row(
children: [
Icon(Icons.error, color: Colors.red.shade700),
const SizedBox(width: 8),
Expanded(
child: Text(
authState.error!,
style: TextStyle(color: Colors.red.shade700),
),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () => authNotifier.clearError(),
color: Colors.red.shade700,
),
],
),
),
Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (currentUser != null) ...[
CircleAvatar(
radius: 50,
backgroundColor: Theme.of(context).colorScheme.primary,
child: Text(
(currentUser?.email.isNotEmpty ?? false)
? currentUser!.email[0].toUpperCase()
: 'U',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
const SizedBox(height: 24),
Text(
'Welcome back!',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
currentUser?.email ?? '',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: Colors.grey[600],
),
),
] else ...[
const Icon(
Icons.account_circle,
size: 100,
color: Colors.grey[400],
),
const SizedBox(height: 24),
Text(
'Welcome to Sage',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
],
const SizedBox(height: 48),
const Text(
'Your collaborative household food inventory tracker',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
const SizedBox(height: 32),
// Placeholder for future features
Card(
margin: const EdgeInsets.symmetric(horizontal: 32),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
const Text(
'Coming Soon',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
const SizedBox(height: 8),
const Text('• Barcode scanning\n• Inventory management\n• Expiration tracking'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
// TODO: Navigate to inventory when implemented
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Inventory feature coming soon!'),
),
);
},
child: const Text('Start Adding Items'),
),
],
),
),
],
),
),
],
),
);
);
}
}