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( 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 _handleCamera(BuildContext context) async { final picker = ImagePicker(); final photo = await picker.pickImage( source: ImageSource.camera, imageQuality: 80, ); if (photo != null && context.mounted) { context.read().add( UploadCustomerFileEvent(photo: File(photo.path)), ); } } Future _handleFilePicker(BuildContext context) async { final result = await FilePicker.pickFiles(withData: true); if (result != null && context.mounted) { context.read().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), ), ), ), ); } }