272 lines
7.7 KiB
Dart
272 lines
7.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flux/core/theme/theme.dart';
|
|
import 'package:flux/features/customers/data/customer_repository.dart';
|
|
import 'package:flux/features/customers/models/customer_model.dart';
|
|
import 'package:flux/features/customers/models/customer_file_model.dart';
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
class CustomerDetailScreen extends StatefulWidget {
|
|
final CustomerModel customer;
|
|
const CustomerDetailScreen({super.key, required this.customer});
|
|
|
|
@override
|
|
State<CustomerDetailScreen> createState() => _CustomerDetailScreenState();
|
|
}
|
|
|
|
class _CustomerDetailScreenState extends State<CustomerDetailScreen> {
|
|
final _repository = GetIt.I<CustomerRepository>();
|
|
List<CustomerFileModel> _files = [];
|
|
bool _isLoadingFiles = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadFiles();
|
|
}
|
|
|
|
Future<void> _loadFiles() async {
|
|
try {
|
|
final files = await _repository.getCustomerFiles(
|
|
widget.customer.id.toString(),
|
|
);
|
|
setState(() {
|
|
_files = files;
|
|
_isLoadingFiles = false;
|
|
});
|
|
} catch (e) {
|
|
setState(() => _isLoadingFiles = false);
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(e.toString())));
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _pickAndUpload() async {
|
|
// Chiamata statica pulita
|
|
FilePickerResult? result = await FilePicker.pickFiles(
|
|
allowMultiple: true,
|
|
type: FileType.any,
|
|
withData: true, // Fondamentale per avere i bytes pronti se servono
|
|
);
|
|
|
|
if (result != null) {
|
|
for (var pickedFile in result.files) {
|
|
try {
|
|
final newFile = await _repository.uploadAndRegisterFile(
|
|
customerId: widget.customer.id.toString(),
|
|
pickedFile: pickedFile,
|
|
);
|
|
setState(() => _files.add(newFile));
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text("Errore upload ${pickedFile.name}: $e")),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: context.background,
|
|
appBar: AppBar(
|
|
title: Text(
|
|
widget.customer.nome,
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
backgroundColor: context.background,
|
|
elevation: 0,
|
|
),
|
|
body: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// COLONNA SINISTRA: ANAGRAFICA
|
|
Expanded(
|
|
flex: 1,
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: _buildInfoSection(),
|
|
),
|
|
),
|
|
|
|
// DIVISORE VERTICALE
|
|
VerticalDivider(
|
|
width: 1,
|
|
color: context.accent.withValues(alpha: 0.1),
|
|
),
|
|
|
|
// COLONNA DESTRA: DOCUMENTI
|
|
Expanded(
|
|
flex: 2,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: _buildDocumentSection(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildInfoSection() {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_infoTile(Icons.phone_android, "Telefono", widget.customer.telefono),
|
|
_infoTile(
|
|
Icons.email_outlined,
|
|
"Email",
|
|
widget.customer.email.isEmpty ? "Non fornita" : widget.customer.email,
|
|
),
|
|
_infoTile(
|
|
Icons.notes_outlined,
|
|
"Note",
|
|
widget.customer.note.isEmpty
|
|
? "Nessun appunto"
|
|
: widget.customer.note,
|
|
),
|
|
const SizedBox(height: 20),
|
|
if (widget.customer.nonDisturbare)
|
|
Container(
|
|
padding: const EdgeInsets.all(12),
|
|
decoration: BoxDecoration(
|
|
color: Colors.red.withValues(alpha: 0.1),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: const Row(
|
|
children: [
|
|
Icon(Icons.privacy_tip, color: Colors.red, size: 20),
|
|
SizedBox(width: 10),
|
|
Text(
|
|
"PRIVACY: Non disturbare",
|
|
style: TextStyle(
|
|
color: Colors.red,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildDocumentSection() {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
"DOCUMENTI",
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: context.accent,
|
|
),
|
|
),
|
|
ElevatedButton.icon(
|
|
onPressed: _pickAndUpload,
|
|
icon: const Icon(Icons.add_circle_outline),
|
|
label: const Text("CARICA FILE"),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 20),
|
|
if (_isLoadingFiles)
|
|
const Center(child: CircularProgressIndicator())
|
|
else if (_files.isEmpty)
|
|
const Center(child: Text("Nessun documento presente"))
|
|
else
|
|
Expanded(
|
|
child: GridView.builder(
|
|
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
|
maxCrossAxisExtent: 200,
|
|
mainAxisSpacing: 16,
|
|
crossAxisSpacing: 16,
|
|
childAspectRatio: 1.2,
|
|
),
|
|
itemCount: _files.length,
|
|
itemBuilder: (context, index) => _FileCard(file: _files[index]),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _infoTile(IconData icon, String label, String value) {
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Icon(icon, size: 18, color: context.accent),
|
|
const SizedBox(width: 8),
|
|
Text(label, style: const TextStyle(fontWeight: FontWeight.w600)),
|
|
],
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
value,
|
|
style: TextStyle(color: context.secondaryText, fontSize: 16),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _FileCard extends StatelessWidget {
|
|
final CustomerFileModel file;
|
|
const _FileCard({required this.file});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: context.background,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: context.accent.withValues(alpha: 0.1)),
|
|
),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(_getFileIcon(file.extension), size: 48, color: context.accent),
|
|
const SizedBox(height: 8),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
child: Text(
|
|
file.name,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
IconData _getFileIcon(String ext) {
|
|
switch (ext.toLowerCase()) {
|
|
case 'pdf':
|
|
return Icons.picture_as_pdf;
|
|
case 'jpg':
|
|
case 'jpeg':
|
|
case 'png':
|
|
return Icons.image;
|
|
default:
|
|
return Icons.insert_drive_file;
|
|
}
|
|
}
|
|
}
|