This commit is contained in:
2026-06-03 12:08:59 +02:00
parent 8ad2b7cf7e
commit a7fd37a894
9 changed files with 589 additions and 166 deletions

View File

@@ -1,6 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/core/enums_and_consts/consts.dart';
import 'package:get_it/get_it.dart';
import 'package:supabase_flutter/supabase_flutter.dart'; // Per estrarre gli store
import '../models/provider_model.dart';
@@ -32,7 +33,7 @@ class ProviderFormCubit extends Cubit<ProviderFormState> {
try {
// 1. Scarichiamo tutti i negozi dell'azienda
final storesResponse = await _client
.from('store')
.from(Tables.stores)
.select('id, name')
.eq('company_id', companyId);
@@ -41,7 +42,7 @@ class ProviderFormCubit extends Cubit<ProviderFormState> {
if (existingProvider != null && existingProvider.id != null) {
// ... (Vecchio codice di recupero)
final links = await _client
.from('providers_in_stores')
.from(Tables.providersInStores)
.select('store_id')
.eq('provider_id', existingProvider.id!);
linkedStoreIds = (links as List)
@@ -83,6 +84,7 @@ class ProviderFormCubit extends Cubit<ProviderFormState> {
String? fiscalCode,
String? sdiCode,
String? emailPec,
String? Function()? colorHex,
}) {
emit(
state.copyWith(
@@ -93,6 +95,7 @@ class ProviderFormCubit extends Cubit<ProviderFormState> {
fiscalCode: fiscalCode,
sdiCode: sdiCode,
emailPec: emailPec,
colorHex: colorHex,
),
),
);

View File

@@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'provider_location_model.dart';
import 'provider_role.dart';
@@ -8,6 +9,7 @@ class ProviderModel extends Equatable {
final String companyId;
final String name; // Nome "commerciale" per riconoscerlo velocemente
final bool isActive;
final String? colorHex;
// Dati fiscali e legali
final String? businessName; // Ragione Sociale
@@ -29,6 +31,7 @@ class ProviderModel extends Equatable {
required this.companyId,
required this.name,
this.isActive = true,
this.colorHex,
this.businessName,
this.vatNumber,
this.fiscalCode,
@@ -42,6 +45,17 @@ class ProviderModel extends Equatable {
this.locations,
});
// 🥷 IL GETTER MAGICO: Converte l'esadecimale in un Color di Flutter
Color get displayColor {
if (colorHex == null || colorHex!.isEmpty) {
return Colors.blueGrey; // Colore di default
}
// Rimuove l'eventuale '#' e aggiunge 'FF' per l'opacità (Alpha)
final hex = colorHex!.replaceAll('#', '');
return Color(int.parse('FF$hex', radix: 16));
}
factory ProviderModel.empty({required String companyId}) {
return ProviderModel(
companyId: companyId,
@@ -56,6 +70,7 @@ class ProviderModel extends Equatable {
String? companyId,
String? name,
bool? isActive,
String? Function()? colorHex,
String? businessName,
String? vatNumber,
String? fiscalCode,
@@ -73,6 +88,7 @@ class ProviderModel extends Equatable {
companyId: companyId ?? this.companyId,
name: name ?? this.name,
isActive: isActive ?? this.isActive,
colorHex: colorHex != null ? colorHex() : this.colorHex,
businessName: businessName ?? this.businessName,
vatNumber: vatNumber ?? this.vatNumber,
fiscalCode: fiscalCode ?? this.fiscalCode,
@@ -114,6 +130,7 @@ class ProviderModel extends Equatable {
companyId: map['company_id'] as String,
name: map['name'] as String,
isActive: map['is_active'] as bool? ?? true,
colorHex: map['color_hex'] as String?,
businessName: map['business_name'] as String?,
vatNumber: map['vat_number'] as String?,
fiscalCode: map['fiscal_code'] as String?,
@@ -134,6 +151,7 @@ class ProviderModel extends Equatable {
'company_id': companyId,
'name': name,
'is_active': isActive,
'color_hex': colorHex,
'business_name': businessName,
'vat_number': vatNumber,
'fiscal_code': fiscalCode,
@@ -155,6 +173,7 @@ class ProviderModel extends Equatable {
companyId,
name,
isActive,
colorHex,
businessName,
vatNumber,
fiscalCode,

View File

@@ -66,6 +66,17 @@ class _ProviderFormScreenState extends State<ProviderFormScreen> {
super.dispose();
}
final List<String> _brandColors = [
'#E60000', // Vodafone/Iliad (Rosso scuro)
'#0047BB', // TIM (Blu)
'#F4811F', // WINDTRE (Arancione)
'#FFCC00', // Fastweb (Giallo)
'#00A859', // Verde generico
'#8E44AD', // Viola
'#2C3E50', // Blu scuro/Nero
'#607D8B', // BlueGrey (Default)
];
void _flushControllers() {
context.read<ProviderFormCubit>().updateFields(
name: _nameCtrl.text.trim(),
@@ -132,6 +143,8 @@ class _ProviderFormScreenState extends State<ProviderFormScreen> {
children: [
_buildGeneralCard(context, state),
const SizedBox(height: 24),
_buildColorPicker(),
const SizedBox(height: 24),
_buildRolesCard(context, state),
const SizedBox(height: 24),
_buildFiscalCard(context),
@@ -392,4 +405,70 @@ class _ProviderFormScreenState extends State<ProviderFormScreen> {
),
);
}
Widget _buildColorPicker() {
return Column(
children: [
const Text(
'Colore Riconoscitivo',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(height: 12),
BlocBuilder<ProviderFormCubit, ProviderFormState>(
builder: (context, state) {
// Se non ha un colore, usiamo il BlueGrey di default
final currentColorHex = state.provider?.colorHex ?? '#607D8B';
return Wrap(
spacing: 12,
runSpacing: 12,
children: _brandColors.map((hexCode) {
final isSelected =
currentColorHex.toUpperCase() == hexCode.toUpperCase();
// Conversione rapida per disegnare il cerchio
final colorValue = Color(
int.parse('FF${hexCode.replaceAll('#', '')}', radix: 16),
);
return InkWell(
borderRadius: BorderRadius.circular(24),
onTap: () {
// Aggiorniamo il Cubit con il nuovo colore
context.read<ProviderFormCubit>().updateFields(
colorHex: () => hexCode,
);
},
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
width: 42,
height: 42,
decoration: BoxDecoration(
color: colorValue,
shape: BoxShape.circle,
border: Border.all(
color: isSelected ? Colors.black : Colors.transparent,
width: isSelected ? 3 : 0,
),
boxShadow: [
if (isSelected)
BoxShadow(
color: colorValue.withValues(alpha: 0.4),
blurRadius: 8,
spreadRadius: 2,
),
],
),
child: isSelected
? const Icon(Icons.check, color: Colors.white, size: 24)
: null,
),
);
}).toList(),
);
},
),
],
);
}
}