Files
flux/lib/features/settings/document_sequence/ui/document_sequence_section.dart

171 lines
6.2 KiB
Dart
Raw Permalink Normal View History

2026-05-10 14:09:57 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/settings/document_sequence/blocs/document_sequence_cubit.dart';
2026-05-16 09:04:18 +02:00
import 'package:flux/features/settings/document_sequence/models/document_sequence_model.dart';
2026-05-10 14:09:57 +02:00
class DocumentSequenceSection extends StatelessWidget {
const DocumentSequenceSection({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<DocumentSequenceCubit, DocumentSequenceState>(
builder: (context, state) {
2026-05-16 09:04:18 +02:00
if (state.status == DocumentSequenceStatus.loading) {
2026-05-10 14:09:57 +02:00
return const Center(child: CircularProgressIndicator());
}
2026-05-25 12:49:04 +02:00
return LayoutBuilder(
builder: ((context, constraints) {
final isLargeScreen = constraints.maxWidth >= 600;
return _buildMainContent(
state: state,
isLargeScreen: isLargeScreen,
context: context,
);
}),
);
},
);
}
2026-05-10 14:09:57 +02:00
2026-05-25 12:49:04 +02:00
Widget _buildMainContent({
required BuildContext context,
required DocumentSequenceState state,
required bool isLargeScreen,
}) {
final year = DateTime.now().year;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Text(
"Protocolli e Numerazione",
style: Theme.of(
context,
).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold),
),
),
2026-05-16 09:04:18 +02:00
2026-05-25 12:49:04 +02:00
// Invece di mappare state.sequences, mappiamo i documenti supportati
...DocumentType.values.map((docType) {
// Cerchiamo se c'è già una configurazione nello stato per questo documento
final existingList = state.sequences
.where((s) => s.docType == docType.name)
.toList();
final existingSeq = existingList.isNotEmpty
? existingList.first
: null;
2026-05-16 09:04:18 +02:00
2026-05-25 12:49:04 +02:00
// Se esiste usiamo i suoi valori, altrimenti i default
final prefix = existingSeq?.prefix ?? docType.defaultPrefix;
final nextValue = existingSeq?.nextValue ?? 1;
2026-05-16 09:04:18 +02:00
2026-05-25 12:49:04 +02:00
// Anteprima dinamica (aggiornata a 4 zeri come nel DB!)
final preview =
"${prefix.isNotEmpty ? '$prefix-' : ''}$year-${nextValue.toString().padLeft(4, '0')}";
2026-05-10 14:09:57 +02:00
2026-05-25 12:49:04 +02:00
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
docType.label,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
const SizedBox(height: 12),
Row(
2026-05-10 14:09:57 +02:00
children: [
2026-05-25 12:49:04 +02:00
Expanded(
flex: 2,
child: TextFormField(
initialValue: prefix,
decoration: const InputDecoration(
labelText: 'Prefisso',
hintText: 'es. TCK',
),
onChanged: (val) => context
.read<DocumentSequenceCubit>()
.updateLocalSequence(docType.name, prefix: val),
2026-05-10 14:09:57 +02:00
),
),
2026-05-25 12:49:04 +02:00
const SizedBox(width: 16),
Expanded(
flex: 3,
child: TextFormField(
initialValue: nextValue.toString(),
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Prossimo Numero',
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
onChanged: (val) => context
.read<DocumentSequenceCubit>()
.updateLocalSequence(
docType.name,
nextValue: int.tryParse(val) ?? 1,
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
),
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
],
),
const SizedBox(height: 12),
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainer,
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
const Icon(
Icons.visibility,
size: 16,
color: Colors.grey,
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
const SizedBox(width: 8),
Text(
"Anteprima prossimo: ",
style: TextStyle(
color:
Colors.grey.shade700, // Idem per la dark mode
fontSize: 12,
),
),
Flexible(
child: Text(
preview,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'monospace',
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
),
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
],
),
2026-05-10 14:09:57 +02:00
),
2026-05-25 12:49:04 +02:00
],
2026-05-10 14:09:57 +02:00
),
),
2026-05-25 12:49:04 +02:00
);
}),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: () =>
context.read<DocumentSequenceCubit>().saveSequences(),
icon: const Icon(Icons.save),
label: const Text("SALVA PROTOCOLLI"),
),
),
],
2026-05-10 14:09:57 +02:00
);
}
}