Fix: Gestione dipendenti nei negozi funzionante con doppio Cubit e UI reattiva

This commit is contained in:
2026-04-15 09:58:27 +02:00
parent 7b3dc449a6
commit 92febadd61
3 changed files with 111 additions and 40 deletions

View File

@@ -90,10 +90,10 @@ class _StoresScreenState extends State<StoresScreen> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// Mostra quanti dipendenti ci sono (usando lo StaffCubit)
BlocBuilder<StaffCubit, StaffState>(
builder: (context, staffState) {
BlocBuilder<StoreCubit, StoreState>(
builder: (context, storeState) {
final staffCount =
staffState.storesByStaff[store.id]?.length ?? 0;
storeState.staffByStore[store.id]?.length ?? 0;
return ActionChip(
avatar: const Icon(Icons.people, size: 16),
label: Text("$staffCount Dipendenti"),
@@ -119,42 +119,52 @@ class _StoresScreenState extends State<StoresScreen> {
context: context,
isScrollControlled: true,
builder: (context) => BlocBuilder<StaffCubit, StaffState>(
builder: (context, state) {
return Container(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("Personale di ${store.nome}", style: context.titleLarge),
const SizedBox(height: 16),
// Lista di TUTTO lo staff dell'azienda con checkbox
...state.allStaff.map((person) {
final bool isAssigned =
state.storesByStaff[store.id!]?.any(
(s) => s.id == person.id,
) ??
false;
// 1. Prendi TUTTI i dipendenti
builder: (context, staffState) {
return BlocBuilder<StoreCubit, StoreState>(
// 2. Prendi le ASSEGNAZIONI
builder: (context, storeState) {
final assignedToThisStore =
storeState.staffByStore[store.id!] ?? [];
return CheckboxListTile(
title: Text(person.name),
value: isAssigned,
onChanged: (selected) {
if (selected == true) {
context.read<StaffCubit>().assignMemberToStore(
person.id!,
store.id!,
);
} else {
context.read<StaffCubit>().removeMemberFromStore(
person.id!,
store.id!,
);
}
},
);
}),
],
),
return Container(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Personale di ${store.nome}",
style: context.titleLarge,
),
const SizedBox(height: 16),
...staffState.allStaff.map((person) {
// La spunta deve dipendere dallo StoreCubit!
final bool isAssigned = assignedToThisStore.any(
(s) => s.id == person.id,
);
return CheckboxListTile(
title: Text(person.name),
value: isAssigned,
onChanged: (selected) {
if (selected == true) {
context.read<StoreCubit>().assignStaffToStore(
store.id!,
person.id!,
);
} else {
context.read<StoreCubit>().removeStaffFromStore(
person.id!,
store.id!,
);
}
},
);
}),
],
),
);
},
);
},
),