e_receipt_mobile/lib/presentation/home/home_screen.dart

265 lines
9.3 KiB
Dart
Raw Normal View History

2026-02-14 18:34:16 +00:00
import 'package:e_receipt_mobile/core/theme/app_colors.dart';
2026-02-14 10:16:13 +00:00
import 'package:e_receipt_mobile/domain/entities/login_user.dart';
2026-02-14 10:51:16 +00:00
import 'package:e_receipt_mobile/presentation/auth/session_controller.dart';
2026-02-14 10:46:58 +00:00
import 'package:e_receipt_mobile/presentation/home/home_view_model.dart';
2026-02-14 10:51:16 +00:00
import 'package:e_receipt_mobile/presentation/login/login_page.dart';
2026-02-14 17:57:18 +00:00
import 'package:e_receipt_mobile/presentation/terminal/terminal_selection_screen.dart';
2026-02-13 19:46:02 +00:00
import 'package:flutter/material.dart';
2026-02-14 10:46:58 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2026-02-13 19:46:02 +00:00
2026-02-14 10:46:58 +00:00
class HomeScreen extends ConsumerWidget {
2026-02-14 10:16:13 +00:00
const HomeScreen({required this.user, super.key});
final LoginUser user;
2026-02-13 19:46:02 +00:00
@override
2026-02-14 10:46:58 +00:00
Widget build(BuildContext context, WidgetRef ref) {
final merchantsAsync = ref.watch(merchantListProvider);
2026-02-13 19:46:02 +00:00
return Scaffold(
appBar: AppBar(
2026-02-14 10:46:58 +00:00
title: Text("Merchants"),
2026-02-13 19:46:02 +00:00
centerTitle: true,
),
2026-02-14 10:51:16 +00:00
drawer: Drawer(
2026-02-14 18:34:16 +00:00
backgroundColor: Colors.white,
2026-02-14 10:51:16 +00:00
child: Column(
children: [
UserAccountsDrawerHeader(
2026-02-14 18:34:16 +00:00
decoration: const BoxDecoration(color: AppColors.primary),
accountName: Text(
user.username,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
accountEmail: Text(
'Role: ${user.role}',
style: const TextStyle(color: Colors.white70),
),
2026-02-14 10:51:16 +00:00
currentAccountPicture: CircleAvatar(
2026-02-14 18:34:16 +00:00
backgroundColor: Colors.white,
2026-02-14 10:51:16 +00:00
child: Text(
(user.username.isNotEmpty ? user.username[0] : 'U')
.toUpperCase(),
2026-02-14 18:34:16 +00:00
style: const TextStyle(color: AppColors.primary),
2026-02-14 10:51:16 +00:00
),
),
),
ListTile(
2026-02-14 18:34:16 +00:00
leading: const Icon(Icons.person_outline, color: Colors.black87),
title: const Text(
'Profile',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.w500,
),
),
2026-02-14 10:51:16 +00:00
onTap: () {
Navigator.of(context).pop();
_showProfile(context);
},
),
2026-02-14 17:57:18 +00:00
// ListTile(
// leading: const Icon(Icons.storefront_outlined),
// title: const Text('Merchants'),
// onTap: () => Navigator.of(context).pop(),
// ),
2026-02-14 10:51:16 +00:00
ListTile(
2026-02-14 18:34:16 +00:00
leading: const Icon(Icons.analytics_outlined, color: Colors.black87),
title: const Text(
'Reports',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.w500,
),
),
2026-02-14 10:51:16 +00:00
onTap: () {
Navigator.of(context).pop();
_showComingSoon(context, 'Reports');
},
),
ListTile(
2026-02-14 18:34:16 +00:00
leading: const Icon(Icons.settings_outlined, color: Colors.black87),
title: const Text(
'Settings',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.w500,
),
),
2026-02-14 10:51:16 +00:00
onTap: () {
Navigator.of(context).pop();
_showComingSoon(context, 'Settings');
},
),
ListTile(
2026-02-14 18:34:16 +00:00
leading: const Icon(Icons.help_outline, color: Colors.black87),
title: const Text(
'Help',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.w500,
),
),
2026-02-14 10:51:16 +00:00
onTap: () {
Navigator.of(context).pop();
_showComingSoon(context, 'Help');
},
),
const Spacer(),
2026-02-14 18:34:16 +00:00
const Divider(height: 1, color: Colors.black12),
2026-02-14 10:51:16 +00:00
ListTile(
2026-02-14 18:34:16 +00:00
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
minTileHeight: 64,
leading: const Icon(Icons.logout, color: Colors.black87),
title: const Text(
'Logout',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.w600,
),
),
onTap: () async {
final shouldLogout = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Logout'),
content: const Text('Are you sure want to logout?'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Logout'),
),
],
);
},
);
if (shouldLogout != true || !context.mounted) {
return;
}
2026-02-14 10:51:16 +00:00
ref.read(sessionControllerProvider.notifier).clearUser();
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute<void>(
builder: (_) => const LoginPage(),
),
(route) => false,
);
},
),
],
),
),
2026-02-14 10:46:58 +00:00
body: Padding(
padding: const EdgeInsets.all(16),
child: merchantsAsync.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, _) => Center(
child: Text('Failed to load merchants: $error'),
),
data: (merchants) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Total (${merchants.length})',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
Expanded(
child: merchants.isEmpty
? const Center(child: Text('No merchants found'))
: ListView.separated(
itemCount: merchants.length,
separatorBuilder: (_, __) =>
const SizedBox(height: 10),
itemBuilder: (context, index) {
final merchant = merchants[index];
return Card(
2026-02-14 17:57:18 +00:00
child: ListTile(
onTap: merchant.id == null
? null
: () => _openTerminalSelection(
context,
merchant.id!,
merchant.name ?? '-',
),
leading: const Icon(Icons.storefront_outlined),
trailing: const Icon(Icons.chevron_right),
title: Text(
'${index + 1}. ${merchant.name ?? '-'}',
2026-02-14 10:46:58 +00:00
),
2026-02-14 17:57:18 +00:00
subtitle: Text(merchant.address ?? '-'),
2026-02-14 10:46:58 +00:00
),
);
},
),
),
],
);
},
2026-02-14 10:16:13 +00:00
),
2026-02-13 19:46:02 +00:00
),
);
}
2026-02-14 10:51:16 +00:00
void _showComingSoon(BuildContext context, String feature) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('$feature is coming soon')));
}
void _showProfile(BuildContext context) {
showDialog<void>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Profile'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Username: ${user.username}'),
const SizedBox(height: 8),
Text('Role: ${user.role}'),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
);
},
);
}
2026-02-14 17:57:18 +00:00
void _openTerminalSelection(
BuildContext context,
String merchantId,
String merchantName,
) {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => TerminalSelectionScreen(
merchantId: merchantId,
merchantName: merchantName,
),
),
);
}
2026-02-13 19:46:02 +00:00
}