j
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/features/company/data/company_repository.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'company_events.dart';
|
||||
part 'company_state.dart';
|
||||
|
||||
class CompanyBloc extends Bloc<CompanyEvent, CompanyState> {
|
||||
final CompanyRepository _repository = GetIt.I<CompanyRepository>();
|
||||
CompanyBloc() : super(const CompanyState(status: CompanyStatus.initial)) {
|
||||
on<CreateCompanyRequested>((event, emit) async {
|
||||
emit(const CompanyState(status: CompanyStatus.loading));
|
||||
try {
|
||||
final createdCompany = await _repository.createCompany(event.company);
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanyStatus.success,
|
||||
company: createdCompany,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanyStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
part of 'company_bloc.dart';
|
||||
|
||||
// lib/blocs/company/company_event.dart
|
||||
|
||||
abstract class CompanyEvent extends Equatable {
|
||||
const CompanyEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class CreateCompanyRequested extends CompanyEvent {
|
||||
final CompanyModel company;
|
||||
|
||||
const CreateCompanyRequested({required this.company});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [company];
|
||||
}
|
||||
117
lib/features/company/bloc/company_settings_cubit.dart
Normal file
117
lib/features/company/bloc/company_settings_cubit.dart
Normal file
@@ -0,0 +1,117 @@
|
||||
import 'dart:io';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/foundation.dart'; // Per kIsWeb
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/features/company/data/company_repository.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
part 'company_settings_state.dart';
|
||||
|
||||
class CompanySettingsCubit extends Cubit<CompanySettingsState> {
|
||||
final CompanyRepository _repository = GetIt.I<CompanyRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
|
||||
|
||||
CompanySettingsCubit() : super(const CompanySettingsState());
|
||||
|
||||
void initSettings() {
|
||||
final currentCompany = _sessionCubit.state.company;
|
||||
if (currentCompany != null) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
company: currentCompany,
|
||||
status: CompanySettingsStatus.ready,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void updateFields({
|
||||
String? name,
|
||||
String? vatId,
|
||||
String? address,
|
||||
String? city,
|
||||
String? zipCode,
|
||||
String? phone,
|
||||
String? email,
|
||||
}) {
|
||||
if (state.company == null) return;
|
||||
|
||||
final updated = state.company!.copyWith(
|
||||
name: name ?? state.company!.name,
|
||||
vatId: vatId ?? state.company!.vatId,
|
||||
address: address ?? state.company!.address,
|
||||
city: city ?? state.company!.city,
|
||||
zipCode: zipCode ?? state.company!.zipCode,
|
||||
phone: phone ?? state.company!.phone,
|
||||
email: email ?? state.company!.email,
|
||||
);
|
||||
emit(state.copyWith(company: updated));
|
||||
}
|
||||
|
||||
Future<void> saveSettings() async {
|
||||
if (state.company == null) return;
|
||||
emit(
|
||||
state.copyWith(status: CompanySettingsStatus.saving, errorMessage: null),
|
||||
);
|
||||
|
||||
try {
|
||||
// 1. Salva i dati su Supabase
|
||||
final updatedCompany = await _repository.updateCompany(state.company!);
|
||||
|
||||
// 2. Aggiorna la sessione globale per riflettere i cambiamenti in tutta l'app
|
||||
_sessionCubit.updateCurrentCompany(updatedCompany);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanySettingsStatus.success,
|
||||
company: updatedCompany,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanySettingsStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Metodo per gestire l'upload del logo
|
||||
Future<void> uploadLogo(Uint8List bytes, String fileName) async {
|
||||
if (state.company == null) return;
|
||||
emit(state.copyWith(status: CompanySettingsStatus.uploadingLogo));
|
||||
|
||||
try {
|
||||
// Usa il tuo repository per caricare il file nel bucket 'company_logos'
|
||||
// Il file può essere Uint8List (se sei su Web) o File (se sei su Mobile/Desktop)
|
||||
final publicUrl = await _repository.uploadCompanyLogo(
|
||||
companyId: state.company!.id!,
|
||||
fileBytes: bytes,
|
||||
fileName: fileName,
|
||||
);
|
||||
|
||||
final updatedCompany = state.company!.copyWith(logoUrl: publicUrl);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
company: updatedCompany,
|
||||
status: CompanySettingsStatus.ready,
|
||||
),
|
||||
);
|
||||
|
||||
// Chiamiamo il salvataggio per rendere definitivo l'URL nel record della compagnia
|
||||
await saveSettings();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CompanySettingsStatus.failure,
|
||||
errorMessage: "Errore caricamento logo: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
lib/features/company/bloc/company_settings_state.dart
Normal file
34
lib/features/company/bloc/company_settings_state.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
part of 'company_settings_cubit.dart';
|
||||
|
||||
class CompanySettingsState {
|
||||
final CompanySettingsStatus status;
|
||||
final CompanyModel? company;
|
||||
final String? errorMessage;
|
||||
|
||||
const CompanySettingsState({
|
||||
this.status = CompanySettingsStatus.initial,
|
||||
this.company,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
CompanySettingsState copyWith({
|
||||
CompanySettingsStatus? status,
|
||||
CompanyModel? company,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return CompanySettingsState(
|
||||
status: status ?? this.status,
|
||||
company: company ?? this.company,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum CompanySettingsStatus {
|
||||
initial,
|
||||
ready,
|
||||
saving,
|
||||
uploadingLogo,
|
||||
success,
|
||||
failure,
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
part of 'company_bloc.dart';
|
||||
|
||||
enum CompanyStatus { initial, loading, success, failure }
|
||||
|
||||
class CompanyState extends Equatable {
|
||||
final CompanyStatus status;
|
||||
final String? errorMessage;
|
||||
final CompanyModel? company;
|
||||
|
||||
const CompanyState({required this.status, this.errorMessage, this.company});
|
||||
|
||||
CompanyState copyWith({
|
||||
CompanyStatus? status,
|
||||
String? errorMessage,
|
||||
CompanyModel? company,
|
||||
}) {
|
||||
return CompanyState(
|
||||
status: status ?? this.status,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
company: company ?? this.company,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, errorMessage, company];
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/company_model.dart';
|
||||
|
||||
@@ -21,6 +23,62 @@ class CompanyRepository {
|
||||
}
|
||||
}
|
||||
|
||||
Future<CompanyModel> updateCompany(CompanyModel company) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('company')
|
||||
.update(company.toMap())
|
||||
.eq('id', company.id!)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
return CompanyModel.fromMap(response);
|
||||
} on PostgrestException catch (e) {
|
||||
throw e.message;
|
||||
} catch (e) {
|
||||
throw e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> uploadCompanyLogo({
|
||||
required String companyId,
|
||||
required Uint8List fileBytes,
|
||||
required String fileName,
|
||||
}) async {
|
||||
try {
|
||||
// 1. Prepariamo il path.
|
||||
// Organizziamo per companyId e aggiungiamo un timestamp per evitare cache del browser
|
||||
// quando l'utente cambia logo più volte.
|
||||
final extension = fileName.split('.').last;
|
||||
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
||||
final filePath = '$companyId/logo_$timestamp.$extension';
|
||||
|
||||
// 2. Caricamento fisico dei bytes
|
||||
// Usiamo uploadBinary che è perfetto per Uint8List
|
||||
await _supabase.storage
|
||||
.from('company_logos')
|
||||
.uploadBinary(
|
||||
filePath,
|
||||
fileBytes,
|
||||
fileOptions: const FileOptions(
|
||||
cacheControl: '3600',
|
||||
upsert:
|
||||
true, // Se esiste già un file con lo stesso nome, lo sovrascrive
|
||||
),
|
||||
);
|
||||
|
||||
// 3. Otteniamo l'URL pubblico.
|
||||
// Nota: il bucket 'company_logos' deve essere impostato come PUBLIC su Supabase
|
||||
final String publicUrl = _supabase.storage
|
||||
.from('company_logos')
|
||||
.getPublicUrl(filePath);
|
||||
|
||||
return publicUrl;
|
||||
} catch (e) {
|
||||
throw Exception("Errore durante l'upload del logo: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<CompanyModel?> getCompany() async {
|
||||
try {
|
||||
final userId = _supabase.auth.currentUser?.id;
|
||||
|
||||
@@ -53,7 +53,9 @@ class CompanyModel extends Equatable {
|
||||
final String vatId;
|
||||
final String fiscalCode;
|
||||
final String sdi;
|
||||
final String companyLogo;
|
||||
final String? phone;
|
||||
final String? email;
|
||||
final String? logoUrl;
|
||||
|
||||
// Stato Pagamenti (Ibride: manuale + Stripe)
|
||||
final bool isPaid;
|
||||
@@ -78,7 +80,9 @@ class CompanyModel extends Equatable {
|
||||
required this.vatId,
|
||||
required this.fiscalCode,
|
||||
required this.sdi,
|
||||
this.companyLogo = '',
|
||||
this.phone,
|
||||
this.email,
|
||||
this.logoUrl,
|
||||
this.isPaid = false,
|
||||
this.paymentExpiration,
|
||||
this.subscriptionTier = SubscriptionTier.free,
|
||||
@@ -100,7 +104,9 @@ class CompanyModel extends Equatable {
|
||||
String? vatId,
|
||||
String? fiscalCode,
|
||||
String? sdi,
|
||||
String? companyLogo,
|
||||
String? logoUrl,
|
||||
String? phone,
|
||||
String? email,
|
||||
bool? isPaid,
|
||||
DateTime? paymentExpiration,
|
||||
SubscriptionTier? subscriptionTier,
|
||||
@@ -121,7 +127,9 @@ class CompanyModel extends Equatable {
|
||||
vatId: vatId ?? this.vatId,
|
||||
fiscalCode: fiscalCode ?? this.fiscalCode,
|
||||
sdi: sdi ?? this.sdi,
|
||||
companyLogo: companyLogo ?? this.companyLogo,
|
||||
logoUrl: logoUrl ?? this.logoUrl,
|
||||
phone: phone ?? this.phone,
|
||||
email: email ?? this.email,
|
||||
isPaid: isPaid ?? this.isPaid,
|
||||
paymentExpiration: paymentExpiration ?? this.paymentExpiration,
|
||||
subscriptionTier: subscriptionTier ?? this.subscriptionTier,
|
||||
@@ -163,7 +171,9 @@ class CompanyModel extends Equatable {
|
||||
vatId: map['vat_id'] ?? '',
|
||||
fiscalCode: map['fiscal_code'] ?? '',
|
||||
sdi: map['sdi'] ?? '',
|
||||
companyLogo: map['company_logo'] ?? '',
|
||||
logoUrl: map['company_logo'],
|
||||
phone: map['phone'] ?? '',
|
||||
email: map['email'] ?? '',
|
||||
isPaid: map['is_paid'] ?? false,
|
||||
paymentExpiration: map['payment_expiration'] != null
|
||||
? DateTime.tryParse(map['payment_expiration'])
|
||||
@@ -193,7 +203,9 @@ class CompanyModel extends Equatable {
|
||||
'vat_id': vatId,
|
||||
'fiscal_code': fiscalCode,
|
||||
'sdi': sdi,
|
||||
'company_logo': companyLogo,
|
||||
'company_logo': logoUrl,
|
||||
'phone': phone,
|
||||
'email': 'email',
|
||||
'is_paid': isPaid,
|
||||
if (paymentExpiration != null)
|
||||
'payment_expiration': paymentExpiration!.toIso8601String(),
|
||||
@@ -221,7 +233,9 @@ class CompanyModel extends Equatable {
|
||||
vatId,
|
||||
fiscalCode,
|
||||
sdi,
|
||||
companyLogo,
|
||||
logoUrl,
|
||||
phone,
|
||||
email,
|
||||
isPaid,
|
||||
paymentExpiration,
|
||||
subscriptionTier,
|
||||
|
||||
310
lib/features/company/ui/company_settings_screen.dart
Normal file
310
lib/features/company/ui/company_settings_screen.dart
Normal file
@@ -0,0 +1,310 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/company/bloc/company_settings_cubit.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
class CompanySettingsScreen extends StatefulWidget {
|
||||
const CompanySettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
State<CompanySettingsScreen> createState() => _CompanySettingsScreenState();
|
||||
}
|
||||
|
||||
class _CompanySettingsScreenState extends State<CompanySettingsScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
final _nameCtrl = TextEditingController();
|
||||
final _vatCtrl = TextEditingController();
|
||||
final _addressCtrl = TextEditingController();
|
||||
final _cityCtrl = TextEditingController();
|
||||
final _zipCtrl = TextEditingController();
|
||||
final _phoneCtrl = TextEditingController();
|
||||
final _emailCtrl = TextEditingController();
|
||||
|
||||
bool _isInitialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<CompanySettingsCubit>().initSettings();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameCtrl.dispose();
|
||||
_vatCtrl.dispose();
|
||||
_addressCtrl.dispose();
|
||||
_cityCtrl.dispose();
|
||||
_zipCtrl.dispose();
|
||||
_phoneCtrl.dispose();
|
||||
_emailCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _syncControllers(company) {
|
||||
if (_nameCtrl.text.isEmpty) _nameCtrl.text = company.name ?? '';
|
||||
if (_vatCtrl.text.isEmpty) _vatCtrl.text = company.vatNumber ?? '';
|
||||
if (_addressCtrl.text.isEmpty) _addressCtrl.text = company.address ?? '';
|
||||
if (_cityCtrl.text.isEmpty) _cityCtrl.text = company.city ?? '';
|
||||
if (_zipCtrl.text.isEmpty) _zipCtrl.text = company.zipCode ?? '';
|
||||
if (_phoneCtrl.text.isEmpty) _phoneCtrl.text = company.phone ?? '';
|
||||
if (_emailCtrl.text.isEmpty) _emailCtrl.text = company.email ?? '';
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
void _flushToCubit() {
|
||||
context.read<CompanySettingsCubit>().updateFields(
|
||||
name: _nameCtrl.text,
|
||||
vatId: _vatCtrl.text,
|
||||
address: _addressCtrl.text,
|
||||
city: _cityCtrl.text,
|
||||
zipCode: _zipCtrl.text,
|
||||
phone: _phoneCtrl.text,
|
||||
email: _emailCtrl.text,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _pickAndUploadLogo() async {
|
||||
final picker = ImagePicker();
|
||||
final companySettingsCubit = context.read<CompanySettingsCubit>();
|
||||
|
||||
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
|
||||
|
||||
if (pickedFile != null && mounted) {
|
||||
// Passiamo i bytes per compatibilità totale con Flutter Web
|
||||
final bytes = await pickedFile.readAsBytes();
|
||||
companySettingsCubit.uploadLogo(bytes, pickedFile.name);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Impostazioni Azienda')),
|
||||
body: BlocConsumer<CompanySettingsCubit, CompanySettingsState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == CompanySettingsStatus.ready && !_isInitialized) {
|
||||
_syncControllers(state.company!);
|
||||
}
|
||||
if (state.status == CompanySettingsStatus.success) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Impostazioni salvate con successo!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (state.status == CompanySettingsStatus.failure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.errorMessage ?? 'Errore'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
if (state.company == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final company = state.company!;
|
||||
|
||||
return Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 800),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
children: [
|
||||
// --- SEZIONE LOGO ---
|
||||
Center(
|
||||
child: Stack(
|
||||
alignment: Alignment.bottomRight,
|
||||
children: [
|
||||
Container(
|
||||
height: 120,
|
||||
width: 120,
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.surfaceContainerHighest,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: theme.dividerColor),
|
||||
image: company.logoUrl != null
|
||||
? DecorationImage(
|
||||
image: NetworkImage(company.logoUrl!),
|
||||
fit: BoxFit.contain,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
child: company.logoUrl == null
|
||||
? const Icon(
|
||||
Icons.business,
|
||||
size: 50,
|
||||
color: Colors.grey,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
if (state.status ==
|
||||
CompanySettingsStatus.uploadingLogo)
|
||||
const Positioned.fill(
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
FloatingActionButton.small(
|
||||
onPressed:
|
||||
state.status ==
|
||||
CompanySettingsStatus.uploadingLogo
|
||||
? null
|
||||
: _pickAndUploadLogo,
|
||||
child: const Icon(Icons.camera_alt),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE DATI PRINCIPALI ---
|
||||
Text(
|
||||
'Dati Legali',
|
||||
style: theme.textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextFormField(
|
||||
controller: _nameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Ragione Sociale',
|
||||
prefixIcon: Icon(Icons.badge),
|
||||
),
|
||||
validator: (val) => val == null || val.isEmpty
|
||||
? 'Campo obbligatorio'
|
||||
: null,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: TextFormField(
|
||||
controller: _vatCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Partita IVA / C.F.',
|
||||
prefixIcon: Icon(Icons.receipt_long),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// --- SEZIONE INDIRIZZO E CONTATTI ---
|
||||
Text(
|
||||
'Sede e Contatti',
|
||||
style: theme.textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _addressCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Indirizzo (Via e numero civico)',
|
||||
prefixIcon: Icon(Icons.location_on),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextFormField(
|
||||
controller: _cityCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Città',
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: TextFormField(
|
||||
controller: _zipCtrl,
|
||||
decoration: const InputDecoration(labelText: 'CAP'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _phoneCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Telefono',
|
||||
prefixIcon: Icon(Icons.phone),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _emailCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Email',
|
||||
prefixIcon: Icon(Icons.email),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 48),
|
||||
|
||||
// --- PULSANTE SALVATAGGIO ---
|
||||
SizedBox(
|
||||
height: 50,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: state.status == CompanySettingsStatus.saving
|
||||
? null
|
||||
: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_flushToCubit();
|
||||
context
|
||||
.read<CompanySettingsCubit>()
|
||||
.saveSettings();
|
||||
}
|
||||
},
|
||||
icon: state.status == CompanySettingsStatus.saving
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
)
|
||||
: const Icon(Icons.save),
|
||||
label: const Text(
|
||||
'Salva Impostazioni',
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,328 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/utils/extensions.dart';
|
||||
import 'package:flux/features/company/bloc/company_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/core/theme/theme.dart';
|
||||
import 'package:flux/core/widgets/flux_text_field.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
|
||||
class CreateCompanyScreen extends StatefulWidget {
|
||||
const CreateCompanyScreen({super.key});
|
||||
|
||||
@override
|
||||
State<CreateCompanyScreen> createState() => _CreateCompanyScreenState();
|
||||
}
|
||||
|
||||
// lib/ui/setup/create_company_screen.dart
|
||||
|
||||
class _CreateCompanyScreenState extends State<CreateCompanyScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
// Controller per i campi obbligatori
|
||||
final _ragioneSocialeController = TextEditingController();
|
||||
final _indirizzoController = TextEditingController();
|
||||
final _capController = TextEditingController();
|
||||
final _cittaController = TextEditingController();
|
||||
final _provinciaController = TextEditingController();
|
||||
final _pIvaController = TextEditingController();
|
||||
final _cfController = TextEditingController();
|
||||
final _univocoController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Ricordati sempre di chiuderli!
|
||||
_ragioneSocialeController.dispose();
|
||||
_indirizzoController.dispose();
|
||||
_capController.dispose();
|
||||
_cittaController.dispose();
|
||||
_provinciaController.dispose();
|
||||
_pIvaController.dispose();
|
||||
_cfController.dispose();
|
||||
_univocoController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onSave() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// Recuperiamo l'ID utente attuale da Supabase o dal SessionBloc
|
||||
final userId = context.read<SessionCubit>().state.user!.id;
|
||||
|
||||
final company = CompanyModel(
|
||||
userId: userId,
|
||||
name: _ragioneSocialeController.text.trim(),
|
||||
address: _indirizzoController.text.trim(),
|
||||
zipCode: _capController.text.trim(),
|
||||
city: _cittaController.text.trim(),
|
||||
province: _provinciaController.text.trim(),
|
||||
vatId: _pIvaController.text.trim(),
|
||||
fiscalCode: _cfController.text.trim(),
|
||||
sdi: _univocoController.text.trim().toUpperCase(),
|
||||
// Gli altri campi hanno i default nel modello
|
||||
);
|
||||
|
||||
// Spariamo l'evento al Bloc
|
||||
context.read<CompanyBloc>().add(CreateCompanyRequested(company: company));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(context.l10n.createCompanyScreenCompanyConfiguration),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout_rounded),
|
||||
onPressed: () {
|
||||
// Qui chiami il tuo Bloc dell'autenticazione per fare logout
|
||||
// Esempio se hai un AuthBloc o SessionBloc:
|
||||
//context.read<AuthBloc>().add(LogoutRequested());
|
||||
|
||||
// Se vuoi solo tornare brutalmente alla login per testare il logo:
|
||||
// Navigator.of(context).pushReplacementNamed('/login');
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: BlocConsumer<CompanyBloc, CompanyState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == CompanyStatus.success && state.company != null) {
|
||||
// 1. Aggiorniamo la singleton con i dati reali (ID incluso)
|
||||
//GetIt.I.get<AppSettings>().setCurrentCompany(state.company);
|
||||
|
||||
// 2. Notifichiamo il SessionBloc per cambiare pagina
|
||||
//context.read<SessionCubit>().add(AppStarted());
|
||||
}
|
||||
|
||||
if (state.status == CompanyStatus.failure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
state.errorMessage ?? context.l10n.commonSavingError,
|
||||
),
|
||||
backgroundColor: Colors.redAccent,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 1: IDENTITÀ FISCALE ---
|
||||
_SectionTitle(
|
||||
title: context.l10n.createCompanyScreenFiscalData,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: context.l10n.createCompanyScreenCompanyName,
|
||||
icon: Icons.business,
|
||||
controller: _ragioneSocialeController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: context.l10n.createCompanyScreenVatId,
|
||||
icon: Icons.numbers,
|
||||
controller: _pIvaController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: context.l10n.createCompanyScreenFiscalCode,
|
||||
icon: Icons.badge_outlined,
|
||||
controller: _cfController,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: context.l10n.createCompanyScreenSdiPec,
|
||||
icon: Icons.send_and_archive_outlined,
|
||||
controller: _univocoController,
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 2: SEDE LEGALE ---
|
||||
_SectionTitle(
|
||||
title:
|
||||
context.l10n.createCompanyScreenCompanyLegalAddress,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: context.l10n.commonAddress,
|
||||
icon: Icons.home_work_outlined,
|
||||
controller: _indirizzoController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: FluxTextField(
|
||||
label: context.l10n.commonCity,
|
||||
icon: Icons.location_city,
|
||||
controller: _cittaController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: context.l10n.commonZipCode,
|
||||
icon: Icons.map_outlined,
|
||||
controller: _capController,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: FluxTextField(
|
||||
label: context.l10n.commonProvince,
|
||||
icon: Icons.explore_outlined,
|
||||
controller: _provinciaController,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// --- SEZIONE 3: LOGO AZIENDALE ---
|
||||
_SectionTitle(title: 'BRANDING'),
|
||||
const SizedBox(height: 16),
|
||||
_buildLogoPicker(context),
|
||||
|
||||
const SizedBox(height: 48),
|
||||
|
||||
// --- BOTTONE INVIO ---
|
||||
_buildSubmitButton(context, state),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Placeholder per il futuro caricamento logo
|
||||
Widget _buildLogoPicker(BuildContext context) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: context.accent.withValues(alpha: 0.05),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
// Bordo continuo ma sottile e semitrasparente per un look pulito
|
||||
border: Border.all(
|
||||
color: context.accent.withValues(alpha: 0.3),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(Icons.cloud_upload_outlined, color: context.accent, size: 32),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
context.l10n.createCompanyScreenUploadLogo,
|
||||
style: TextStyle(
|
||||
color: context.primaryText,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
context.l10n.createCompanyScreenWillBeUsedForReceipts,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: context.secondaryText, fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSubmitButton(BuildContext context, CompanyState state) {
|
||||
return SizedBox(
|
||||
width: double.infinity,
|
||||
height: 56,
|
||||
child: ElevatedButton(
|
||||
onPressed: state.status == CompanyStatus.loading
|
||||
? null
|
||||
: () => _onSave(),
|
||||
child: state.status == CompanyStatus.loading
|
||||
? const CircularProgressIndicator()
|
||||
: Text(context.l10n.createCompanyScreenSaveCompany),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: context.accent.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.domain_add_rounded,
|
||||
color: context.accent,
|
||||
size: 32,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
context.l10n.createCompanyScreenSetupYourCompany,
|
||||
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: context.primaryText,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
context.l10n.createCompanyScreenFluxNeedsYourFiscalData,
|
||||
style: TextStyle(
|
||||
color: context.secondaryText,
|
||||
fontSize: 15,
|
||||
height: 1.5,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Widget di supporto per i titoli delle sezioni
|
||||
class _SectionTitle extends StatelessWidget {
|
||||
final String title;
|
||||
const _SectionTitle({required this.title});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
color: context.accent,
|
||||
fontWeight: FontWeight.w800,
|
||||
letterSpacing: 1.2,
|
||||
fontSize: 13,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user