Files
flux/lib/features/home/ui/home_screen.dart

430 lines
16 KiB
Dart
Raw Normal View History

2026-04-09 19:25:32 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/core/routes/routes.dart';
2026-04-09 19:25:32 +02:00
import 'package:flux/core/theme/theme.dart';
import 'package:flux/core/utils/extensions.dart';
2026-05-13 12:41:07 +02:00
import 'package:flux/core/widgets/staff_selector_modal.dart';
2026-05-26 12:28:12 +02:00
import 'package:flux/features/home/dashboard_task_list/ui/dashboard_tasks_card.dart';
import 'package:flux/features/home/latest_store_operations/ui/latest_store_operations_card.dart';
2026-05-19 12:46:13 +02:00
import 'package:flux/features/home/latest_store_tickets/ui/latest_store_tickets_card.dart';
import 'package:flux/features/home/ui/quick_actions_widget.dart';
import 'package:flux/features/master_data/staff/blocs/staff_cubit.dart';
2026-05-13 12:41:07 +02:00
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
2026-05-21 14:43:47 +02:00
import 'package:flux/features/notes/data/notes_repository.dart';
import 'package:flux/features/notes/models/note_model.dart';
2026-05-23 11:08:39 +02:00
import 'package:flux/features/notes/ui/dashboard_notes_widget.dart';
2026-05-26 12:28:12 +02:00
import 'package:flux/features/tasks/data/task_repository.dart';
import 'package:flux/features/tasks/models/task_model.dart';
2026-05-21 14:43:47 +02:00
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
2026-04-09 19:25:32 +02:00
class HomeScreen extends StatelessWidget {
2026-04-09 19:25:32 +02:00
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
2026-04-09 19:25:32 +02:00
return Scaffold(
backgroundColor: theme.colorScheme.surface,
body: SafeArea(
child: Column(
children: [
// ==========================================
// 1. HEADER FISSO (Non scrolla mai)
// ==========================================
Container(
padding: const EdgeInsets.all(24.0),
// Un leggero colore di sfondo aiuta a staccare l'header quando il contenuto ci passa sotto
color: theme.colorScheme.surface,
child: _buildHeader(context, theme),
),
2026-04-09 19:25:32 +02:00
// ==========================================
// 2. CORPO DELLA DASHBOARD (Scrollabile)
// ==========================================
Expanded(
child: CustomScrollView(
slivers: [
// --- QUICK ACTIONS: AZIONI RAPIDE ---
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: _buildQuickActions(context),
),
),
const SliverToBoxAdapter(child: SizedBox(height: 32)),
2026-04-09 19:25:32 +02:00
// --- I WIDGET DELLA DASHBOARD (Responsive Grid) ---
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
sliver: SliverGrid(
gridDelegate:
const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 500,
mainAxisSpacing: 16,
crossAxisSpacing: 16,
childAspectRatio: 1.3,
),
delegate: SliverChildListDelegate([
2026-05-21 14:43:47 +02:00
LatestStoreOperationsCard(),
LatestStoreTicketsCard(),
_buildDashboardWidget(
title: context.l10n.homeExpiringContracts,
icon: Icons.assignment_late_outlined,
color: Colors.orange,
context: context,
),
2026-05-23 11:08:39 +02:00
DashboardNotesWidget(),
2026-05-26 12:28:12 +02:00
DashboardTasksCard(),
]),
),
2026-04-09 19:25:32 +02:00
),
// Spazio finale per non far attaccare l'ultima card al fondo
const SliverToBoxAdapter(child: SizedBox(height: 40)),
2026-04-09 19:25:32 +02:00
],
),
),
],
2026-04-13 10:00:07 +02:00
),
),
);
}
// ==========================================
// WIDGET BUILDERS
// ==========================================
2026-04-13 10:00:07 +02:00
Widget _buildHeader(BuildContext context, ThemeData theme) {
final user = context.watch<SessionCubit>().state.currentStaffMember;
final currentStore = context.watch<SessionCubit>().state.currentStore;
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.homeWelcomeBack(user?.name ?? "Utente"),
style: theme.textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.bold,
letterSpacing: -0.5,
color: context.primaryText, // Uso dell'estensione!
),
),
const SizedBox(height: 8),
InkWell(
onTap: () => _showStoreSelector(context, theme),
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
color: context.primary.withValues(
alpha: 0.08,
), // Sfondo delicato
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: context.primary.withValues(
alpha: 0.2,
), // Bordino netto
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.storefront, size: 16, color: context.primary),
const SizedBox(width: 8),
Text(
currentStore?.name ?? context.l10n.homeNoStoreFound,
style: TextStyle(
fontWeight: FontWeight.w600,
color: context.primary,
),
),
const SizedBox(width: 4),
Icon(Icons.arrow_drop_down, color: context.primary),
],
),
),
),
],
),
),
CircleAvatar(
radius: 24,
// Usiamo il Turchese (accent) in trasparenza per l'avatar
backgroundColor: context.accent.withValues(alpha: 0.15),
child: Icon(Icons.person, color: context.accent, size: 26),
),
],
);
}
Widget _buildQuickActions(BuildContext context) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
QuickActionButton(
icon: Icons.add,
label: context.l10n.commonOperation,
color: Colors.blue,
onTap: () async {
StaffMemberModel? createdBy = await getStaffMember(context);
if (createdBy == null || !context.mounted) return;
2026-05-09 19:32:40 +02:00
context.pushNamed(
Routes.operationForm,
2026-05-11 11:44:14 +02:00
pathParameters: {'id': 'new'},
extra: (createdBy: createdBy, operation: null),
2026-05-09 19:32:40 +02:00
);
},
),
const SizedBox(width: 12),
QuickActionButton(
icon: Icons.handyman,
label: context.l10n.homeNewOperationTicket,
color: Colors.redAccent,
2026-05-13 12:41:07 +02:00
onTap: () async {
StaffMemberModel? createdBy = await getStaffMember(context);
if (createdBy == null || !context.mounted) return;
context.pushNamed(
Routes.ticketForm,
2026-05-11 11:44:14 +02:00
pathParameters: {'id': 'new'},
extra: (createdBy: createdBy, ticket: null),
);
},
),
const SizedBox(width: 12),
QuickActionButton(
icon: Icons.note_add,
label: context.l10n.commonNote,
color: Colors.amber,
2026-05-21 14:43:47 +02:00
onTap: () async {
final companyId = context.read<SessionCubit>().state.company!.id!;
final currentStaffId = context
.read<SessionCubit>()
.state
.currentStaffMember!
.id!;
final emptyNote = NoteModel.empty(
createdBy: currentStaffId,
companyId: companyId,
);
final noteId = await GetIt.I.get<NotesRepository>().saveNote(
emptyNote,
);
if (!context.mounted) return;
context.pushNamed(
Routes.noteForm,
pathParameters: {'id': noteId},
extra: emptyNote.copyWith(id: noteId),
);
},
),
const SizedBox(width: 12),
QuickActionButton(
icon: Icons.task_alt,
label: context.l10n.commonTask,
color: Colors.teal,
onTap: () {
2026-05-26 12:28:12 +02:00
final companyId = context.read<SessionCubit>().state.company!.id!;
final currentStaffId = context
.read<SessionCubit>()
.state
.currentStaffMember!
.id!;
final emptyTask = TaskModel.empty(
companyId: companyId,
createdById: currentStaffId,
);
final savedTask = GetIt.I.get<TaskRepository>().createTask(
emptyTask,
);
context.pushNamed(
Routes.taskForm,
pathParameters: {'id': 'new'},
extra: savedTask,
);
},
),
],
),
);
}
Widget _buildDashboardWidget({
required BuildContext context,
required String title,
required IconData icon,
required Color color,
VoidCallback? onTap,
}) {
final theme = Theme.of(context);
return Card(
elevation: 0,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(color: theme.dividerColor.withValues(alpha: 0.5)),
),
child: InkWell(
onTap: onTap,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, color: color, size: 20),
),
const SizedBox(width: 12),
Expanded(
child: Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: context.primaryText,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
IconButton(
icon: Icon(
Icons.more_vert,
size: 20,
color: context.secondaryText,
),
onPressed: () {},
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
],
),
const Spacer(),
Center(
child: Text(
context.l10n.commonComingSoon,
style: TextStyle(
color: context.secondaryText.withValues(alpha: 0.7),
fontStyle: FontStyle.italic,
fontSize: 13,
),
2026-04-13 10:00:07 +02:00
),
),
const Spacer(),
],
),
),
2026-04-13 10:00:07 +02:00
),
2026-04-09 19:25:32 +02:00
);
}
void _showStoreSelector(BuildContext context, ThemeData theme) {
showModalBottomSheet(
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
),
builder: (context) {
// Leggiamo la lista dei negozi dal Cubit dedicato (se ce l'hai lì)
// Oppure adatta il blocco se li salvi altrove!
final staffState = context.watch<StaffCubit>().state;
final currentStoreId = context
.read<SessionCubit>()
.state
.currentStore
?.id;
final currentStaffId = context
.read<SessionCubit>()
.state
.currentStaffMember
?.id;
return SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: Text(
"Seleziona Negozio",
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 16),
if (staffState.status == StaffStatus.loading)
const Center(child: CircularProgressIndicator())
else if (staffState.storesByStaff[currentStaffId] == null)
const Padding(
padding: EdgeInsets.all(24.0),
child: Text("Nessun negozio disponibile."),
)
else
...staffState.storesByStaff[currentStaffId]!.map((store) {
final isSelected = store.id == currentStoreId;
return ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 24.0,
),
leading: Icon(
Icons.storefront,
color: isSelected
? theme.colorScheme.primary
: theme.iconTheme.color,
),
title: Text(
store.name,
style: TextStyle(
fontWeight: isSelected
? FontWeight.bold
: FontWeight.normal,
color: isSelected ? theme.colorScheme.primary : null,
),
),
trailing: isSelected
? Icon(
Icons.check_circle,
color: theme.colorScheme.primary,
)
: null,
onTap: () {
// Cambiamo il negozio nel SessionCubit!
context.read<SessionCubit>().changeStore(store);
2026-05-13 12:41:07 +02:00
context.read<StaffCubit>().loadStaffForStore(store.id!);
Navigator.pop(context);
},
);
}),
],
),
),
);
},
);
2026-04-09 19:25:32 +02:00
}
}