feat-add-files-from-qr #8
@@ -5,7 +5,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||||||
// Importa il tuo SessionCubit e lo State
|
// Importa il tuo SessionCubit e lo State
|
||||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||||
import 'package:flux/core/data/core_repository.dart';
|
import 'package:flux/core/data/core_repository.dart';
|
||||||
import 'package:flux/core/widgets/mobile_upload_screen.dart';
|
import 'package:flux/features/customers/ui/customer_mobile_upload_screen.dart';
|
||||||
import 'package:flux/features/auth/ui/auth_screen.dart';
|
import 'package:flux/features/auth/ui/auth_screen.dart';
|
||||||
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
||||||
import 'package:flux/features/customers/models/customer_model.dart';
|
import 'package:flux/features/customers/models/customer_model.dart';
|
||||||
@@ -110,7 +110,7 @@ class AppRouter {
|
|||||||
|
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) => CustomerFilesBloc(customerId),
|
create: (context) => CustomerFilesBloc(customerId),
|
||||||
child: MobileUploadScreen(
|
child: CustomerMobileUploadScreen(
|
||||||
customerId: customerId,
|
customerId: customerId,
|
||||||
customerName: customerName,
|
customerName: customerName,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,117 +0,0 @@
|
|||||||
import 'dart:io';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
||||||
import 'package:image_picker/image_picker.dart';
|
|
||||||
import 'package:file_picker/file_picker.dart';
|
|
||||||
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
|
||||||
|
|
||||||
class MobileUploadScreen extends StatelessWidget {
|
|
||||||
final String customerId;
|
|
||||||
final String customerName;
|
|
||||||
|
|
||||||
const MobileUploadScreen({
|
|
||||||
super.key,
|
|
||||||
required this.customerId,
|
|
||||||
required this.customerName,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return BlocListener<CustomerFilesBloc, CustomerFilesState>(
|
|
||||||
listener: (context, state) {
|
|
||||||
if (state.status == CustomerFilesStatus.success) {
|
|
||||||
ScaffoldMessenger.of(
|
|
||||||
context,
|
|
||||||
).showSnackBar(const SnackBar(content: Text("File caricato! ✅")));
|
|
||||||
}
|
|
||||||
if (state.status == CustomerFilesStatus.failure) {
|
|
||||||
ScaffoldMessenger.of(
|
|
||||||
context,
|
|
||||||
).showSnackBar(SnackBar(content: Text("Errore: ${state.error}")));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
child: Scaffold(
|
|
||||||
appBar: AppBar(title: Text("Upload: $customerName")),
|
|
||||||
body: Padding(
|
|
||||||
padding: const EdgeInsets.all(24.0),
|
|
||||||
child: Column(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
_UploadButton(
|
|
||||||
title: "SCATTA FOTO",
|
|
||||||
icon: Icons.camera_alt_rounded,
|
|
||||||
onTap: () => _handleCamera(context),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 20),
|
|
||||||
_UploadButton(
|
|
||||||
title: "CARICA DA MEMORIA",
|
|
||||||
icon: Icons.file_present_rounded,
|
|
||||||
onTap: () => _handleFilePicker(context),
|
|
||||||
isSecondary: true,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _handleCamera(BuildContext context) async {
|
|
||||||
final picker = ImagePicker();
|
|
||||||
final photo = await picker.pickImage(
|
|
||||||
source: ImageSource.camera,
|
|
||||||
imageQuality: 80,
|
|
||||||
);
|
|
||||||
if (photo != null && context.mounted) {
|
|
||||||
context.read<CustomerFilesBloc>().add(
|
|
||||||
UploadCustomerFileEvent(photo: File(photo.path)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _handleFilePicker(BuildContext context) async {
|
|
||||||
final result = await FilePicker.pickFiles(withData: true);
|
|
||||||
if (result != null && context.mounted) {
|
|
||||||
context.read<CustomerFilesBloc>().add(
|
|
||||||
UploadCustomerFileEvent(pickedFile: result.files.first),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _UploadButton extends StatelessWidget {
|
|
||||||
final String title;
|
|
||||||
final IconData icon;
|
|
||||||
final VoidCallback onTap;
|
|
||||||
final bool isSecondary;
|
|
||||||
|
|
||||||
const _UploadButton({
|
|
||||||
required this.title,
|
|
||||||
required this.icon,
|
|
||||||
required this.onTap,
|
|
||||||
this.isSecondary = false,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return SizedBox(
|
|
||||||
width: double.infinity,
|
|
||||||
height: 80,
|
|
||||||
child: ElevatedButton.icon(
|
|
||||||
onPressed: onTap,
|
|
||||||
icon: Icon(icon, size: 28),
|
|
||||||
label: Text(
|
|
||||||
title,
|
|
||||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
style: ElevatedButton.styleFrom(
|
|
||||||
backgroundColor: isSecondary ? Colors.grey[200] : null,
|
|
||||||
foregroundColor: isSecondary ? Colors.black87 : null,
|
|
||||||
shape: RoundedRectangleBorder(
|
|
||||||
borderRadius: BorderRadius.circular(16),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
@@ -17,6 +18,7 @@ class CustomerFilesBloc extends Bloc<CustomerFilesEvent, CustomerFilesState> {
|
|||||||
: super(const CustomerFilesState(status: CustomerFilesStatus.initial)) {
|
: super(const CustomerFilesState(status: CustomerFilesStatus.initial)) {
|
||||||
on<LoadCustomerFilesEvent>(_loadCustomerFiles);
|
on<LoadCustomerFilesEvent>(_loadCustomerFiles);
|
||||||
on<UploadCustomerFileEvent>(_uploadCustomerFile);
|
on<UploadCustomerFileEvent>(_uploadCustomerFile);
|
||||||
|
on<UploadMultipleCustomerFilesEvent>(_uploadMultipleCustomerFiles);
|
||||||
on<DeleteCustomerFilesEvent>(_deleteCustomerFiles);
|
on<DeleteCustomerFilesEvent>(_deleteCustomerFiles);
|
||||||
on<ToggleCustomerFileSelectionEvent>(_toggleCustomerFileSelection);
|
on<ToggleCustomerFileSelectionEvent>(_toggleCustomerFileSelection);
|
||||||
}
|
}
|
||||||
@@ -60,6 +62,48 @@ class CustomerFilesBloc extends Bloc<CustomerFilesEvent, CustomerFilesState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _uploadMultipleCustomerFiles(
|
||||||
|
UploadMultipleCustomerFilesEvent event,
|
||||||
|
Emitter<CustomerFilesState> emit,
|
||||||
|
) async {
|
||||||
|
if (event.files.isEmpty) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: CustomerFilesStatus.failure,
|
||||||
|
error: "Nessun file selezionato",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit(state.copyWith(status: CustomerFilesStatus.uploading, error: null));
|
||||||
|
try {
|
||||||
|
// 2. Creiamo una lista di "Promesse" (Futures) per il repository
|
||||||
|
final List<Future<void>> uploadTasks = [];
|
||||||
|
for (var file in event.files) {
|
||||||
|
// Aggiungiamo il task alla lista, ma NON usiamo await qui dentro!
|
||||||
|
uploadTasks.add(
|
||||||
|
_repository.uploadAndRegisterFile(
|
||||||
|
customerId: customerId,
|
||||||
|
pickedFile: file,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// 3. ESECUZIONE PARALLELA!
|
||||||
|
// Aspettiamo che tutti i file siano caricati contemporaneamente.
|
||||||
|
await Future.wait(uploadTasks);
|
||||||
|
// 4. GRAN FINALE: Tutto caricato, emettiamo il success!
|
||||||
|
emit(state.copyWith(status: CustomerFilesStatus.success));
|
||||||
|
} catch (e) {
|
||||||
|
// Se anche un solo file fallisce, catturiamo l'errore
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: CustomerFilesStatus.failure,
|
||||||
|
error: "Errore durante l'upload multiplo: $e",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _deleteCustomerFiles(
|
Future<void> _deleteCustomerFiles(
|
||||||
DeleteCustomerFilesEvent event,
|
DeleteCustomerFilesEvent event,
|
||||||
Emitter<CustomerFilesState> emit,
|
Emitter<CustomerFilesState> emit,
|
||||||
|
|||||||
@@ -15,6 +15,13 @@ class UploadCustomerFileEvent extends CustomerFilesEvent {
|
|||||||
const UploadCustomerFileEvent({this.pickedFile, this.photo});
|
const UploadCustomerFileEvent({this.pickedFile, this.photo});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UploadMultipleCustomerFilesEvent extends CustomerFilesEvent {
|
||||||
|
final List<PlatformFile> files;
|
||||||
|
const UploadMultipleCustomerFilesEvent(this.files);
|
||||||
|
@override
|
||||||
|
List<Object> get props => [files];
|
||||||
|
}
|
||||||
|
|
||||||
class DeleteCustomerFilesEvent extends CustomerFilesEvent {}
|
class DeleteCustomerFilesEvent extends CustomerFilesEvent {}
|
||||||
|
|
||||||
class ToggleCustomerFileSelectionEvent extends CustomerFilesEvent {
|
class ToggleCustomerFileSelectionEvent extends CustomerFilesEvent {
|
||||||
|
|||||||
304
lib/features/customers/ui/customer_mobile_upload_screen.dart
Normal file
304
lib/features/customers/ui/customer_mobile_upload_screen.dart
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
||||||
|
|
||||||
|
class CustomerMobileUploadScreen extends StatefulWidget {
|
||||||
|
final String customerId;
|
||||||
|
final String customerName;
|
||||||
|
|
||||||
|
const CustomerMobileUploadScreen({
|
||||||
|
super.key,
|
||||||
|
required this.customerId,
|
||||||
|
required this.customerName,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<CustomerMobileUploadScreen> createState() =>
|
||||||
|
_CustomerMobileUploadScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CustomerMobileUploadScreenState
|
||||||
|
extends State<CustomerMobileUploadScreen> {
|
||||||
|
// 1. LA NOSTRA STAGING AREA (Il "Carrello")
|
||||||
|
final List<PlatformFile> _stagedFiles = [];
|
||||||
|
|
||||||
|
// 2. STATO DI CARICAMENTO GLOBALE
|
||||||
|
bool _isUploading = false;
|
||||||
|
|
||||||
|
// Funzione magica per capire se è un'immagine o un PDF dall'estensione
|
||||||
|
bool _isImage(String path) {
|
||||||
|
final ext = path.split('.').last.toLowerCase();
|
||||||
|
return ['jpg', 'jpeg', 'png', 'webp'].contains(ext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return BlocListener<CustomerFilesBloc, CustomerFilesState>(
|
||||||
|
listener: (context, state) {
|
||||||
|
// Quando il BLoC ci dice che ha finito l'upload (Success), chiudiamo la pagina!
|
||||||
|
if (state.status == CustomerFilesStatus.success && _isUploading) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text("Tutti i file caricati con successo! ✅"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
}
|
||||||
|
if (state.status == CustomerFilesStatus.failure) {
|
||||||
|
setState(() => _isUploading = false);
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(SnackBar(content: Text("Errore: ${state.error}")));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Text("Upload: ${widget.customerName}"),
|
||||||
|
// Togliamo la freccia indietro se stiamo caricando per evitare disastri
|
||||||
|
automaticallyImplyLeading: !_isUploading,
|
||||||
|
),
|
||||||
|
body: Stack(
|
||||||
|
children: [
|
||||||
|
Column(
|
||||||
|
children: [
|
||||||
|
// --- SEZIONE PULSANTI (Fotocamera / Galleria) ---
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: ElevatedButton.icon(
|
||||||
|
onPressed: _isUploading ? null : _handleCamera,
|
||||||
|
icon: const Icon(Icons.camera_alt),
|
||||||
|
label: const Text("SCATTA"),
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
Expanded(
|
||||||
|
child: OutlinedButton.icon(
|
||||||
|
onPressed: _isUploading ? null : _handleFilePicker,
|
||||||
|
icon: const Icon(Icons.folder),
|
||||||
|
label: const Text("GALLERIA"),
|
||||||
|
style: OutlinedButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
const Divider(),
|
||||||
|
|
||||||
|
// --- SEZIONE ANTEPRIME (La GridView Magica) ---
|
||||||
|
Expanded(
|
||||||
|
child: _stagedFiles.isEmpty
|
||||||
|
? const Center(
|
||||||
|
child: Text(
|
||||||
|
"Nessun file selezionato.\nScatta una foto o scegli dalla galleria.",
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: Colors.grey),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: GridView.builder(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
gridDelegate:
|
||||||
|
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||||
|
crossAxisCount:
|
||||||
|
3, // 3 colonne come la galleria dell'iPhone
|
||||||
|
crossAxisSpacing: 12,
|
||||||
|
mainAxisSpacing: 12,
|
||||||
|
),
|
||||||
|
itemCount: _stagedFiles.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final file = _stagedFiles[index];
|
||||||
|
final isImg = _isImage(file.name);
|
||||||
|
|
||||||
|
return Stack(
|
||||||
|
clipBehavior: Clip.none,
|
||||||
|
children: [
|
||||||
|
// L'ANTEPRIMA
|
||||||
|
Container(
|
||||||
|
width: double.infinity,
|
||||||
|
height: double.infinity,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.grey.shade200,
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
border: Border.all(
|
||||||
|
color: Colors.grey.shade300,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: ClipRRect(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
child: isImg
|
||||||
|
? Image.file(
|
||||||
|
File(file.path!),
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
)
|
||||||
|
: const Column(
|
||||||
|
mainAxisAlignment:
|
||||||
|
MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
Icons.picture_as_pdf,
|
||||||
|
color: Colors.red,
|
||||||
|
size: 36,
|
||||||
|
),
|
||||||
|
SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
"PDF",
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 10,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
// IL PULSANTE CESTINO (In alto a destra)
|
||||||
|
Positioned(
|
||||||
|
top: -8,
|
||||||
|
right: -8,
|
||||||
|
child: GestureDetector(
|
||||||
|
onTap: () {
|
||||||
|
setState(() {
|
||||||
|
_stagedFiles.removeAt(index);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
child: Container(
|
||||||
|
padding: const EdgeInsets.all(4),
|
||||||
|
decoration: const BoxDecoration(
|
||||||
|
color: Colors.red,
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
),
|
||||||
|
child: const Icon(
|
||||||
|
Icons.close,
|
||||||
|
color: Colors.white,
|
||||||
|
size: 16,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
// --- SEZIONE INVIA E CHIUDI ---
|
||||||
|
SafeArea(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: SizedBox(
|
||||||
|
width: double.infinity,
|
||||||
|
height: 56,
|
||||||
|
child: ElevatedButton.icon(
|
||||||
|
// Il pulsante si accende SOLO se ci sono file nel carrello
|
||||||
|
onPressed: _stagedFiles.isEmpty || _isUploading
|
||||||
|
? null
|
||||||
|
: _submitAllFiles,
|
||||||
|
icon: const Icon(Icons.cloud_upload),
|
||||||
|
label: Text(
|
||||||
|
"INVIA ${_stagedFiles.length} FILE E CHIUDI",
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Theme.of(
|
||||||
|
context,
|
||||||
|
).colorScheme.primary,
|
||||||
|
foregroundColor: Theme.of(
|
||||||
|
context,
|
||||||
|
).colorScheme.onPrimary,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
|
||||||
|
// --- OVERLAY DI CARICAMENTO (Impedisce tap multipli) ---
|
||||||
|
if (_isUploading)
|
||||||
|
Container(
|
||||||
|
color: Colors.black.withValues(alpha: 0.5),
|
||||||
|
child: const Center(
|
||||||
|
child: Card(
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.all(24.0),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
CircularProgressIndicator(),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
Text(
|
||||||
|
"Caricamento in corso...",
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- LOGICA FOTOCAMERA E LIBRERIA ---
|
||||||
|
Future<void> _handleCamera() async {
|
||||||
|
final picker = ImagePicker();
|
||||||
|
final photo = await picker.pickImage(
|
||||||
|
source: ImageSource.camera,
|
||||||
|
imageQuality: 80,
|
||||||
|
);
|
||||||
|
if (photo != null) {
|
||||||
|
final photoBytes = await photo.readAsBytes(); // Sicuro anche per Web!
|
||||||
|
final photoSize = await photo.length();
|
||||||
|
|
||||||
|
final platformFile = PlatformFile(
|
||||||
|
name: photo.name,
|
||||||
|
size: photoSize,
|
||||||
|
path: photo.path,
|
||||||
|
bytes: photoBytes, // I bytes ci salvano la vita su Supabase!
|
||||||
|
);
|
||||||
|
setState(() {
|
||||||
|
_stagedFiles.add(platformFile); // Unifichiamo tutto in un dart:io File
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _handleFilePicker() async {
|
||||||
|
// allowMultiple: true permette di pescare 5 foto dalla galleria in un colpo solo!
|
||||||
|
final result = await FilePicker.pickFiles(allowMultiple: true);
|
||||||
|
if (result != null) {
|
||||||
|
setState(() {
|
||||||
|
_stagedFiles.addAll(result.files);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- LOGICA DI INVIO AL BLoC ---
|
||||||
|
void _submitAllFiles() {
|
||||||
|
setState(() => _isUploading = true);
|
||||||
|
|
||||||
|
// Diciamo al BLoC di caricare tutti i file.
|
||||||
|
// Usiamo il tuo evento esistente per ogni file (il BLoC li metterà in coda)
|
||||||
|
final bloc = context.read<CustomerFilesBloc>();
|
||||||
|
bloc.add(UploadMultipleCustomerFilesEvent(_stagedFiles));
|
||||||
|
|
||||||
|
// N.B: Il Navigator.pop() viene chiamato dal BlocListener in alto quando lo stato diventa "success"!
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,8 +28,13 @@ class ServiceFilesBloc extends Bloc<ServiceFilesEvent, ServiceFilesState> {
|
|||||||
on<LoadServiceFilesEvent>(_onLoadServiceFiles);
|
on<LoadServiceFilesEvent>(_onLoadServiceFiles);
|
||||||
on<AddServiceFilesEvent>(_onAddServiceFiles);
|
on<AddServiceFilesEvent>(_onAddServiceFiles);
|
||||||
on<UploadServiceFilesEvent>(_onUploadServiceFiles);
|
on<UploadServiceFilesEvent>(_onUploadServiceFiles);
|
||||||
|
on<UploadMultipleServiceFilesEvent>(_onUploadMultipleServiceFiles);
|
||||||
on<DeleteServiceFilesEvent>(_onDeleteServiceFiles);
|
on<DeleteServiceFilesEvent>(_onDeleteServiceFiles);
|
||||||
on<ToggleServiceFileSelectionEvent>(_onToggleServiceFileSelection);
|
on<ToggleServiceFileSelectionEvent>(_onToggleServiceFileSelection);
|
||||||
|
// Se il BLoC nasce con un ID, accendiamo subito lo stream!
|
||||||
|
if (serviceId != null) {
|
||||||
|
add(LoadServiceFilesEvent(serviceId: serviceId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onServiceSaved(
|
FutureOr<void> _onServiceSaved(
|
||||||
@@ -80,8 +85,9 @@ class ServiceFilesBloc extends Bloc<ServiceFilesEvent, ServiceFilesState> {
|
|||||||
AddServiceFilesEvent event,
|
AddServiceFilesEvent event,
|
||||||
Emitter<ServiceFilesState> emit,
|
Emitter<ServiceFilesState> emit,
|
||||||
) async {
|
) async {
|
||||||
|
final currentId = state.serviceId;
|
||||||
// BIVIO 1: PRATICA NUOVA (Nessun ID)
|
// BIVIO 1: PRATICA NUOVA (Nessun ID)
|
||||||
if (serviceId == null) {
|
if (currentId == null) {
|
||||||
// Mettiamo i file nel "parcheggio" locale dello State
|
// Mettiamo i file nel "parcheggio" locale dello State
|
||||||
final newLocalFiles = event.files.map((file) {
|
final newLocalFiles = event.files.map((file) {
|
||||||
return ServiceFileModel(
|
return ServiceFileModel(
|
||||||
@@ -139,7 +145,7 @@ class ServiceFilesBloc extends Bloc<ServiceFilesEvent, ServiceFilesState> {
|
|||||||
if (event.pickedFiles != null && event.pickedFiles!.isNotEmpty) {
|
if (event.pickedFiles != null && event.pickedFiles!.isNotEmpty) {
|
||||||
for (var file in event.pickedFiles!) {
|
for (var file in event.pickedFiles!) {
|
||||||
await _repository.uploadAndRegisterServiceFile(
|
await _repository.uploadAndRegisterServiceFile(
|
||||||
serviceId: serviceId!,
|
serviceId: state.serviceId!,
|
||||||
pickedFile: file,
|
pickedFile: file,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -152,13 +158,75 @@ class ServiceFilesBloc extends Bloc<ServiceFilesEvent, ServiceFilesState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _onUploadMultipleServiceFiles(
|
||||||
|
UploadMultipleServiceFilesEvent event,
|
||||||
|
Emitter<ServiceFilesState> emit,
|
||||||
|
) async {
|
||||||
|
if (event.files.isEmpty) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ServiceFilesStatus.failure,
|
||||||
|
error: "Nessun file selezionato",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit(state.copyWith(status: ServiceFilesStatus.uploading, error: null));
|
||||||
|
try {
|
||||||
|
// 2. Creiamo una lista di "Promesse" (Futures) per il repository
|
||||||
|
final List<Future<void>> uploadTasks = [];
|
||||||
|
for (var file in event.files) {
|
||||||
|
// Aggiungiamo il task alla lista, ma NON usiamo await qui dentro!
|
||||||
|
uploadTasks.add(
|
||||||
|
_repository.uploadAndRegisterServiceFile(
|
||||||
|
serviceId: state.serviceId!,
|
||||||
|
pickedFile: file,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// 3. ESECUZIONE PARALLELA!
|
||||||
|
// Aspettiamo che tutti i file siano caricati contemporaneamente.
|
||||||
|
await Future.wait(uploadTasks);
|
||||||
|
// 4. GRAN FINALE: Tutto caricato, emettiamo il success!
|
||||||
|
emit(state.copyWith(status: ServiceFilesStatus.success));
|
||||||
|
} catch (e) {
|
||||||
|
// Se anche un solo file fallisce, catturiamo l'errore
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ServiceFilesStatus.failure,
|
||||||
|
error: "Errore durante l'upload multiplo: $e",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
FutureOr<void> _onDeleteServiceFiles(
|
FutureOr<void> _onDeleteServiceFiles(
|
||||||
DeleteServiceFilesEvent event,
|
DeleteServiceFilesEvent event,
|
||||||
Emitter<ServiceFilesState> emit,
|
Emitter<ServiceFilesState> emit,
|
||||||
) {}
|
) async {
|
||||||
|
emit(state.copyWith(status: ServiceFilesStatus.loading));
|
||||||
|
try {
|
||||||
|
await _repository.deleteServiceFiles(state.selectedFiles);
|
||||||
|
emit(
|
||||||
|
state.copyWith(status: ServiceFilesStatus.success, selectedFiles: []),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(status: ServiceFilesStatus.failure, error: e.toString()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
FutureOr<void> _onToggleServiceFileSelection(
|
FutureOr<void> _onToggleServiceFileSelection(
|
||||||
ToggleServiceFileSelectionEvent event,
|
ToggleServiceFileSelectionEvent event,
|
||||||
Emitter<ServiceFilesState> emit,
|
Emitter<ServiceFilesState> emit,
|
||||||
) {}
|
) {
|
||||||
|
List<ServiceFileModel> selectedFiles = List.from(state.selectedFiles);
|
||||||
|
if (selectedFiles.contains(event.file)) {
|
||||||
|
selectedFiles.remove(event.file);
|
||||||
|
} else {
|
||||||
|
selectedFiles.add(event.file);
|
||||||
|
}
|
||||||
|
emit(state.copyWith(selectedFiles: selectedFiles));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,13 @@ class UploadServiceFilesEvent extends ServiceFilesEvent {
|
|||||||
List<Object?> get props => [pickedFiles, photos];
|
List<Object?> get props => [pickedFiles, photos];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UploadMultipleServiceFilesEvent extends ServiceFilesEvent {
|
||||||
|
final List<PlatformFile> files;
|
||||||
|
const UploadMultipleServiceFilesEvent(this.files);
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [files];
|
||||||
|
}
|
||||||
|
|
||||||
class DeleteServiceFilesEvent extends ServiceFilesEvent {}
|
class DeleteServiceFilesEvent extends ServiceFilesEvent {}
|
||||||
|
|
||||||
class ToggleServiceFileSelectionEvent extends ServiceFilesEvent {
|
class ToggleServiceFileSelectionEvent extends ServiceFilesEvent {
|
||||||
|
|||||||
@@ -203,19 +203,31 @@ class ServicesCubit extends Cubit<ServicesState> {
|
|||||||
|
|
||||||
// --- PERSISTENZA ---
|
// --- PERSISTENZA ---
|
||||||
|
|
||||||
Future<void> saveCurrentService({required bool isBozza}) async {
|
Future<void> saveCurrentService({
|
||||||
|
required bool isBozza,
|
||||||
|
bool shouldPop = true,
|
||||||
|
List<ServiceFileModel>? files,
|
||||||
|
}) async {
|
||||||
if (state.currentService == null) return;
|
if (state.currentService == null) return;
|
||||||
|
|
||||||
emit(state.copyWith(status: ServicesStatus.saving, errorMessage: null));
|
emit(state.copyWith(status: ServicesStatus.saving, errorMessage: null));
|
||||||
try {
|
try {
|
||||||
// 1. Aggiorniamo il flag bozza in base a quale pulsante ha premuto l'utente
|
// 1. Aggiorniamo il flag bozza in base a quale pulsante ha premuto l'utente
|
||||||
final serviceToSave = state.currentService!.copyWith(isBozza: isBozza);
|
final serviceToSave = state.currentService!.copyWith(
|
||||||
|
isBozza: isBozza,
|
||||||
|
files: files,
|
||||||
|
);
|
||||||
|
|
||||||
// 2. Salvataggio corazzato
|
// 2. Salvataggio corazzato
|
||||||
await _repository.saveFullService(serviceToSave);
|
final updatedService = await _repository.saveFullService(serviceToSave);
|
||||||
|
|
||||||
// 3. Reset e ricaricamento
|
// 3. Reset e ricaricamento
|
||||||
emit(state.copyWith(status: ServicesStatus.saved, currentService: null));
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: shouldPop ? ServicesStatus.saved : ServicesStatus.savedNoPop,
|
||||||
|
currentService: shouldPop ? null : updatedService,
|
||||||
|
),
|
||||||
|
);
|
||||||
await loadServices(refresh: true);
|
await loadServices(refresh: true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(
|
emit(
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
part of 'services_cubit.dart';
|
part of 'services_cubit.dart';
|
||||||
|
|
||||||
enum ServicesStatus { initial, loading, ready, saving, saved, success, failure }
|
enum ServicesStatus {
|
||||||
|
initial,
|
||||||
|
loading,
|
||||||
|
ready,
|
||||||
|
saving,
|
||||||
|
saved,
|
||||||
|
savedNoPop,
|
||||||
|
success,
|
||||||
|
failure,
|
||||||
|
}
|
||||||
|
|
||||||
class ServicesState extends Equatable {
|
class ServicesState extends Equatable {
|
||||||
final ServicesStatus status;
|
final ServicesStatus status;
|
||||||
@@ -10,6 +19,7 @@ class ServicesState extends Equatable {
|
|||||||
final String query;
|
final String query;
|
||||||
final DateTimeRange? dateRange;
|
final DateTimeRange? dateRange;
|
||||||
final bool hasReachedMax;
|
final bool hasReachedMax;
|
||||||
|
final bool isSavingDraft;
|
||||||
|
|
||||||
const ServicesState({
|
const ServicesState({
|
||||||
required this.status,
|
required this.status,
|
||||||
@@ -19,6 +29,7 @@ class ServicesState extends Equatable {
|
|||||||
this.query = '',
|
this.query = '',
|
||||||
this.dateRange,
|
this.dateRange,
|
||||||
this.hasReachedMax = false,
|
this.hasReachedMax = false,
|
||||||
|
this.isSavingDraft = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
ServicesState copyWith({
|
ServicesState copyWith({
|
||||||
@@ -29,6 +40,7 @@ class ServicesState extends Equatable {
|
|||||||
String? query,
|
String? query,
|
||||||
DateTimeRange? dateRange,
|
DateTimeRange? dateRange,
|
||||||
bool? hasReachedMax,
|
bool? hasReachedMax,
|
||||||
|
bool? isSavingDraft,
|
||||||
}) {
|
}) {
|
||||||
return ServicesState(
|
return ServicesState(
|
||||||
status: status ?? this.status,
|
status: status ?? this.status,
|
||||||
@@ -38,6 +50,7 @@ class ServicesState extends Equatable {
|
|||||||
query: query ?? this.query,
|
query: query ?? this.query,
|
||||||
dateRange: dateRange ?? this.dateRange,
|
dateRange: dateRange ?? this.dateRange,
|
||||||
hasReachedMax: hasReachedMax ?? this.hasReachedMax,
|
hasReachedMax: hasReachedMax ?? this.hasReachedMax,
|
||||||
|
isSavingDraft: isSavingDraft ?? this.isSavingDraft,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,5 +63,6 @@ class ServicesState extends Equatable {
|
|||||||
query,
|
query,
|
||||||
dateRange,
|
dateRange,
|
||||||
hasReachedMax,
|
hasReachedMax,
|
||||||
|
isSavingDraft,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -335,4 +335,25 @@ class ServicesRepository {
|
|||||||
);
|
);
|
||||||
await _customerRepository.saveFileReference(fileToCopy);
|
await _customerRepository.saveFileReference(fileToCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> deleteServiceFiles(List<ServiceFileModel> files) async {
|
||||||
|
if (files.isEmpty) return;
|
||||||
|
// 1. Prepariamo le liste di ID e di Percorsi
|
||||||
|
final List<String> idsToDelete = files.map((f) => f.id!).toList();
|
||||||
|
final List<String> storagePaths = files.map((f) => f.storagePath).toList();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await _supabase.from('service_file').delete().inFilter('id', idsToDelete);
|
||||||
|
|
||||||
|
await _supabase.storage.from('documents').remove(storagePaths);
|
||||||
|
|
||||||
|
debugPrint("Eliminati con successo ${files.length} file.");
|
||||||
|
} on PostgrestException catch (e) {
|
||||||
|
debugPrint("Errore DB: ${e.message}");
|
||||||
|
throw 'Errore database: ${e.message}';
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint("Errore generico: $e");
|
||||||
|
throw 'Errore durante l\'eliminazione dei file: $e';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import 'package:flux/core/blocs/session/session_cubit.dart';
|
|||||||
import 'package:flux/core/widgets/image_viewer_widget.dart';
|
import 'package:flux/core/widgets/image_viewer_widget.dart';
|
||||||
import 'package:flux/core/widgets/pdf_viewer_widget.dart';
|
import 'package:flux/core/widgets/pdf_viewer_widget.dart';
|
||||||
import 'package:flux/core/widgets/qr_upload_dialog.dart';
|
import 'package:flux/core/widgets/qr_upload_dialog.dart';
|
||||||
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
|
||||||
import 'package:flux/features/services/blocs/service_files_bloc.dart';
|
import 'package:flux/features/services/blocs/service_files_bloc.dart';
|
||||||
import 'package:flux/features/services/blocs/services_cubit.dart';
|
import 'package:flux/features/services/blocs/services_cubit.dart';
|
||||||
import 'package:flux/features/services/models/service_file_model.dart';
|
import 'package:flux/features/services/models/service_file_model.dart';
|
||||||
@@ -23,7 +22,7 @@ class AttachmentsSection extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (result != null && context.mounted) {
|
if (result != null && context.mounted) {
|
||||||
context.read<ServicesCubit>().addAttachments(result.files);
|
context.read<ServiceFilesBloc>().add(AddServiceFilesEvent(result.files));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,7 +276,11 @@ class AttachmentsSection extends StatelessWidget {
|
|||||||
if (confirm != true) return; // Utente ha annullato
|
if (confirm != true) return; // Utente ha annullato
|
||||||
|
|
||||||
// Salviamo forzatamente in bozza
|
// Salviamo forzatamente in bozza
|
||||||
await cubit.saveCurrentService(isBozza: true);
|
await cubit.saveCurrentService(
|
||||||
|
isBozza: true,
|
||||||
|
shouldPop: false,
|
||||||
|
files: serviceFilesBloc.state.localFiles,
|
||||||
|
);
|
||||||
|
|
||||||
// Recuperiamo il servizio aggiornato con l'ID!
|
// Recuperiamo il servizio aggiornato con l'ID!
|
||||||
currentService = cubit.state.currentService;
|
currentService = cubit.state.currentService;
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ class _ServiceFormScreenState extends State<ServiceFormScreen> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
} else if (state.status == ServicesStatus.failure) {
|
}
|
||||||
|
if (state.status == ServicesStatus.failure) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text("Errore: ${state.errorMessage ?? ''}"),
|
content: Text("Errore: ${state.errorMessage ?? ''}"),
|
||||||
@@ -59,6 +60,14 @@ class _ServiceFormScreenState extends State<ServiceFormScreen> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (state.status == ServicesStatus.savedNoPop) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text("Pratica salvata con successo!"),
|
||||||
|
backgroundColor: Colors.green,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
final service = state.currentService;
|
final service = state.currentService;
|
||||||
|
|||||||
@@ -21,85 +21,230 @@ class ServiceMobileUploadScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _ServiceMobileUploadScreenState extends State<ServiceMobileUploadScreen> {
|
class _ServiceMobileUploadScreenState extends State<ServiceMobileUploadScreen> {
|
||||||
final List<PlatformFile> _pickedFiles = [];
|
// 1. LA NOSTRA STAGING AREA (Il "Carrello")
|
||||||
final List<File> _photos = [];
|
final List<PlatformFile> _stagedFiles = [];
|
||||||
|
|
||||||
|
// 2. STATO DI CARICAMENTO GLOBALE
|
||||||
|
bool _isUploading = false;
|
||||||
|
|
||||||
|
// Funzione magica per capire se è un'immagine o un PDF dall'estensione
|
||||||
|
bool _isImage(String path) {
|
||||||
|
final ext = path.split('.').last.toLowerCase();
|
||||||
|
return ['jpg', 'jpeg', 'png', 'webp'].contains(ext);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocListener<ServiceFilesBloc, ServiceFilesState>(
|
return BlocListener<ServiceFilesBloc, ServiceFilesState>(
|
||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
if (state.status == ServiceFilesStatus.success) {
|
// Quando il BLoC ci dice che ha finito l'upload (Success), chiudiamo la pagina!
|
||||||
ScaffoldMessenger.of(
|
if (state.status == ServiceFilesStatus.success && _isUploading) {
|
||||||
context,
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
).showSnackBar(const SnackBar(content: Text("File caricato! ✅")));
|
const SnackBar(
|
||||||
|
content: Text("Tutti i file caricati con successo! ✅"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
if (state.status == ServiceFilesStatus.failure) {
|
if (state.status == ServiceFilesStatus.failure) {
|
||||||
|
setState(() => _isUploading = false);
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(
|
||||||
context,
|
context,
|
||||||
).showSnackBar(SnackBar(content: Text("Errore: ${state.error}")));
|
).showSnackBar(SnackBar(content: Text("Errore: ${state.error}")));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
appBar: AppBar(title: Text("Upload Pratica:\n${widget.serviceName}")),
|
appBar: AppBar(
|
||||||
body: Padding(
|
title: Text("Upload Pratica:\n${widget.serviceName}"),
|
||||||
padding: const EdgeInsets.all(24.0),
|
automaticallyImplyLeading: !_isUploading,
|
||||||
child: Column(
|
),
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
body: Stack(
|
||||||
children: [
|
children: [
|
||||||
SizedBox(
|
Column(
|
||||||
width: double.infinity,
|
children: [
|
||||||
height: 80,
|
// --- SEZIONE PULSANTI (Fotocamera / Galleria) ---
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
child: ElevatedButton.icon(
|
child: ElevatedButton.icon(
|
||||||
onPressed: () => _handleCamera(context),
|
onPressed: _isUploading ? null : _handleCamera,
|
||||||
icon: const Icon(Icons.camera_alt_rounded, size: 28),
|
icon: const Icon(Icons.camera_alt),
|
||||||
label: const Text(
|
label: const Text("SCATTA"),
|
||||||
"SCATTA FOTO",
|
|
||||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
shape: RoundedRectangleBorder(
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||||
borderRadius: BorderRadius.circular(16),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
Expanded(
|
||||||
|
child: OutlinedButton.icon(
|
||||||
|
onPressed: _isUploading ? null : _handleFilePicker,
|
||||||
|
icon: const Icon(Icons.folder),
|
||||||
|
label: const Text("GALLERIA"),
|
||||||
|
style: OutlinedButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
),
|
||||||
SizedBox(
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
const Divider(),
|
||||||
|
|
||||||
|
// --- SEZIONE ANTEPRIME (La GridView Magica) ---
|
||||||
|
Expanded(
|
||||||
|
child: _stagedFiles.isEmpty
|
||||||
|
? const Center(
|
||||||
|
child: Text(
|
||||||
|
"Nessun file selezionato.\nScatta una foto o scegli dalla galleria.",
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: Colors.grey),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: GridView.builder(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
gridDelegate:
|
||||||
|
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||||
|
crossAxisCount:
|
||||||
|
3, // 3 colonne come la galleria dell'iPhone
|
||||||
|
crossAxisSpacing: 12,
|
||||||
|
mainAxisSpacing: 12,
|
||||||
|
),
|
||||||
|
itemCount: _stagedFiles.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final file = _stagedFiles[index];
|
||||||
|
final isImg = _isImage(file.name);
|
||||||
|
|
||||||
|
return Stack(
|
||||||
|
clipBehavior: Clip.none,
|
||||||
|
children: [
|
||||||
|
// L'ANTEPRIMA
|
||||||
|
Container(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
height: 80,
|
height: double.infinity,
|
||||||
child: ElevatedButton.icon(
|
decoration: BoxDecoration(
|
||||||
onPressed: () => _handleFilePicker(context),
|
color: Colors.grey.shade200,
|
||||||
icon: const Icon(Icons.file_present_rounded, size: 28),
|
borderRadius: BorderRadius.circular(12),
|
||||||
label: const Text(
|
border: Border.all(
|
||||||
"CARICA DA MEMORIA",
|
color: Colors.grey.shade300,
|
||||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
||||||
),
|
),
|
||||||
style: ElevatedButton.styleFrom(
|
|
||||||
backgroundColor: Colors.grey[200],
|
|
||||||
foregroundColor: Colors.black87,
|
|
||||||
shape: RoundedRectangleBorder(
|
|
||||||
borderRadius: BorderRadius.circular(16),
|
|
||||||
),
|
),
|
||||||
|
child: ClipRRect(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
child: isImg
|
||||||
|
? Image.file(
|
||||||
|
File(file.path!),
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
)
|
||||||
|
: const Column(
|
||||||
|
mainAxisAlignment:
|
||||||
|
MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
Icons.picture_as_pdf,
|
||||||
|
color: Colors.red,
|
||||||
|
size: 36,
|
||||||
|
),
|
||||||
|
SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
"PDF",
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 10,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
const SizedBox(height: 30),
|
// IL PULSANTE CESTINO (In alto a destra)
|
||||||
SizedBox(
|
Positioned(
|
||||||
|
top: -8,
|
||||||
|
right: -8,
|
||||||
|
child: GestureDetector(
|
||||||
|
onTap: () {
|
||||||
|
setState(() {
|
||||||
|
_stagedFiles.removeAt(index);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
child: Container(
|
||||||
|
padding: const EdgeInsets.all(4),
|
||||||
|
decoration: const BoxDecoration(
|
||||||
|
color: Colors.red,
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
),
|
||||||
|
child: const Icon(
|
||||||
|
Icons.close,
|
||||||
|
color: Colors.white,
|
||||||
|
size: 16,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
// --- SEZIONE INVIA E CHIUDI ---
|
||||||
|
SafeArea(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: SizedBox(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
height: 80,
|
height: 56,
|
||||||
child: ElevatedButton.icon(
|
child: ElevatedButton.icon(
|
||||||
onPressed: () => _handleSaveAndClose(context),
|
// Il pulsante si accende SOLO se ci sono file nel carrello
|
||||||
icon: const Icon(Icons.save_alt_rounded, size: 28),
|
onPressed: _stagedFiles.isEmpty || _isUploading
|
||||||
label: const Text(
|
? null
|
||||||
"INVIA E CHIUDI",
|
: _submitAllFiles,
|
||||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
icon: const Icon(Icons.cloud_upload),
|
||||||
|
label: Text(
|
||||||
|
"INVIA ${_stagedFiles.length} FILE E CHIUDI",
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: Colors.grey[200],
|
backgroundColor: Theme.of(
|
||||||
foregroundColor: Colors.black87,
|
context,
|
||||||
shape: RoundedRectangleBorder(
|
).colorScheme.primary,
|
||||||
borderRadius: BorderRadius.circular(16),
|
foregroundColor: Theme.of(
|
||||||
|
context,
|
||||||
|
).colorScheme.onPrimary,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
|
||||||
|
// --- OVERLAY DI CARICAMENTO (Impedisce tap multipli) ---
|
||||||
|
if (_isUploading)
|
||||||
|
Container(
|
||||||
|
color: Colors.black.withValues(alpha: 0.5),
|
||||||
|
child: const Center(
|
||||||
|
child: Card(
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.all(24.0),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
CircularProgressIndicator(),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
Text(
|
||||||
|
"Caricamento in corso...",
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -107,36 +252,51 @@ class _ServiceMobileUploadScreenState extends State<ServiceMobileUploadScreen> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleCamera(BuildContext context) async {
|
// --- LOGICA FOTOCAMERA E LIBRERIA ---
|
||||||
|
Future<void> _handleCamera() async {
|
||||||
final picker = ImagePicker();
|
final picker = ImagePicker();
|
||||||
final photo = await picker.pickImage(
|
final photo = await picker.pickImage(
|
||||||
source: ImageSource.camera,
|
source: ImageSource.camera,
|
||||||
imageQuality: 80,
|
imageQuality: 80,
|
||||||
);
|
);
|
||||||
if (photo != null && context.mounted) {
|
if (photo != null) {
|
||||||
setState(() {
|
final photoBytes = await photo.readAsBytes(); // Sicuro anche per Web!
|
||||||
_photos.add(File(photo.path));
|
final photoSize = await photo.length();
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _handleFilePicker(BuildContext context) async {
|
final platformFile = PlatformFile(
|
||||||
final result = await FilePicker.pickFiles(withData: true);
|
name: photo.name,
|
||||||
if (result != null && context.mounted) {
|
size: photoSize,
|
||||||
setState(() {
|
path: photo.path,
|
||||||
_pickedFiles.addAll(result.files);
|
bytes: photoBytes, // I bytes ci salvano la vita su Supabase!
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _handleSaveAndClose(BuildContext context) async {
|
|
||||||
context.read<ServiceFilesBloc>().add(
|
|
||||||
UploadServiceFilesEvent(pickedFiles: _pickedFiles, photos: _photos),
|
|
||||||
);
|
);
|
||||||
Navigator.pop(context);
|
setState(() {
|
||||||
|
_stagedFiles.add(platformFile); // Unifichiamo tutto in un dart:io File
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _handleFilePicker() async {
|
||||||
|
// allowMultiple: true permette di pescare 5 foto dalla galleria in un colpo solo!
|
||||||
|
final result = await FilePicker.pickFiles(allowMultiple: true);
|
||||||
|
if (result != null) {
|
||||||
|
setState(() {
|
||||||
|
_stagedFiles.addAll(result.files);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- LOGICA DI INVIO AL BLoC ---
|
||||||
|
void _submitAllFiles() {
|
||||||
|
setState(() => _isUploading = true);
|
||||||
|
|
||||||
|
// Diciamo al BLoC di caricare tutti i file.
|
||||||
|
// Usiamo il tuo evento esistente per ogni file (il BLoC li metterà in coda)
|
||||||
|
final bloc = context.read<ServiceFilesBloc>();
|
||||||
|
bloc.add(UploadMultipleServiceFilesEvent(_stagedFiles));
|
||||||
|
|
||||||
|
// N.B: Il Navigator.pop() viene chiamato dal BlocListener in alto quando lo stato diventa "success"!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user