sistemato deeplinking alla serviceformscreen, aggiunto logout e sistemate altre cose

This commit is contained in:
2026-04-19 10:57:55 +02:00
parent e9f3327f31
commit 023665ae58
17 changed files with 742 additions and 198 deletions

View File

@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_bloc.dart';
@@ -123,4 +124,34 @@ class ProductCubit extends Cubit<ProductState> {
);
}
}
Future<ModelModel?> quickCreateProduct({
required String brandName,
required String modelName,
}) async {
try {
await loadBrands();
BrandModel? brand = state.brands.firstWhereOrNull(
(b) => b.name.toLowerCase() == brandName.toLowerCase(),
);
// 1. Cerchiamo o creiamo il Brand
// (Usa una funzione upsert o una ricerca rapida nel repository)
brand ??= await _repository.upsertBrand(
BrandModel(name: brandName, companyId: _sessionBloc.state.company!.id),
);
// 2. Creiamo il Modello legato al Brand
final newModel = await _repository.upsertModel(
ModelModel(brandId: brand.id!, name: modelName),
);
// 3. Aggiorniamo lo stato locale così la lista modelli lo vede subito
emit(state.copyWith(models: [newModel, ...state.models]));
return newModel;
} catch (e) {
emit(state.copyWith(errorMessage: "Errore creazione rapida: $e"));
return null;
}
}
}

View File

@@ -12,7 +12,7 @@ class ModelModel extends Equatable {
const ModelModel({
this.id,
required this.name,
required this.nameWithBrand,
this.nameWithBrand = '',
required this.brandId,
this.isActive = true,
this.createdAt,

View File

@@ -0,0 +1,111 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
import 'package:flux/features/master_data/products/models/brand_model.dart';
class QuickProductDialog extends StatefulWidget {
final List<BrandModel> existingBrands;
const QuickProductDialog({super.key, required this.existingBrands});
@override
State<QuickProductDialog> createState() => _QuickProductDialogState();
}
class _QuickProductDialogState extends State<QuickProductDialog> {
final _modelCtrl = TextEditingController();
String _selectedBrandName = "";
bool _isLoading = false;
Future<void> _save() async {
final NavigatorState navigator = Navigator.of(context);
if (_selectedBrandName.isEmpty || _modelCtrl.text.isEmpty) return;
setState(() => _isLoading = true);
final newModel = await context.read<ProductCubit>().quickCreateProduct(
brandName: _selectedBrandName.trim(),
modelName: _modelCtrl.text.trim(),
);
setState(() => _isLoading = false);
if (context.mounted) {
navigator.pop(newModel); // Restituiamo il modello creato
}
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Nuovo Dispositivo"),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// AUTOCOMPLETE PER IL BRAND LOCALE
Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return const Iterable<String>.empty();
}
final query = textEditingValue.text.toLowerCase();
// Filtriamo i brand che contengono la stringa cercata
return widget.existingBrands
.map((b) => b.name)
.where((name) => name.toLowerCase().contains(query));
},
onSelected: (String selection) {
_selectedBrandName = selection;
},
fieldViewBuilder:
(
context,
textEditingController,
focusNode,
onFieldSubmitted,
) {
return TextField(
controller: textEditingController,
focusNode: focusNode,
autofocus: true,
decoration: const InputDecoration(
labelText: "Marca (es: Apple, Samsung)",
hintText: "Inizia a scrivere...",
),
onChanged: (val) => _selectedBrandName = val,
);
},
),
const SizedBox(height: 16),
TextField(
controller: _modelCtrl,
decoration: const InputDecoration(
labelText: "Modello (es: iPhone 15 Pro)",
),
textInputAction: TextInputAction.done,
onSubmitted: (_) => _save(),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text("Annulla"),
),
ElevatedButton(
onPressed: _isLoading ? null : _save,
child: _isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text("Crea"),
),
],
);
}
}