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

384 lines
14 KiB
Dart
Raw Permalink 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';
2026-04-09 19:25:32 +02:00
import 'package:flux/core/theme/theme.dart';
import 'package:flux/core/utils/extensions.dart';
import 'package:flux/features/home/latest_store_operations/ui/latest_store_operations_card.dart';
import 'package:flux/features/home/ui/quick_actions_widget.dart';
import 'package:flux/features/master_data/staff/blocs/staff_cubit.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([
_buildDashboardWidget(
title: context.l10n.homeExpiringContracts,
icon: Icons.assignment_late_outlined,
color: Colors.orange,
context: context,
),
_buildDashboardWidget(
title: context.l10n.commonStickyNotes,
icon: Icons.sticky_note_2_outlined,
color: Colors.yellow.shade700,
context: context,
),
_buildDashboardWidget(
title: context.l10n.homeMyTasks,
icon: Icons.check_box_outlined,
color: Colors.green,
context: context,
),
LatestStoreOperationsCard(),
_buildDashboardWidget(
title: context.l10n.homeLatestOperationTickets,
icon: Icons.support_agent_outlined,
color: Colors.purple,
context: context,
),
]),
),
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: () {
// Entriamo nel form! Nessun parametro extra = Nuovo Servizio
context.push('/operation-form');
},
),
const SizedBox(width: 12),
QuickActionButton(
icon: Icons.handyman,
label: context.l10n.homeNewOperationTicket,
color: Colors.redAccent,
onTap: () {
// TODO: Quando avrai la rotta per la nuova assistenza
// context.push('/assistance-form');
},
),
const SizedBox(width: 12),
QuickActionButton(
icon: Icons.note_add,
label: context.l10n.commonNote,
color: Colors.amber,
onTap: () {
// TODO: Quando faremo il modale/pagina delle note
},
),
const SizedBox(width: 12),
QuickActionButton(
icon: Icons.task_alt,
label: context.l10n.commonTask,
color: Colors.teal,
onTap: () {
// TODO: Quando faremo i task
},
),
],
),
);
}
Widget _buildDashboardWidget({
required BuildContext context,
required String title,
required IconData icon,
required Color color,
}) {
final theme = Theme.of(context);
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(color: theme.dividerColor.withValues(alpha: 0.5)),
),
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,
2026-04-13 10:00:07 +02:00
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);
Navigator.pop(context);
},
);
}),
],
),
),
);
},
);
2026-04-09 19:25:32 +02:00
}
}