aiuto
This commit is contained in:
355
lib/features/onboarding/ui/onboarding_screen.dart
Normal file
355
lib/features/onboarding/ui/onboarding_screen.dart
Normal file
@@ -0,0 +1,355 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
import 'package:flux/features/master_data/store/models/store_model.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/onboarding/blocs/onboarding_cubit.dart';
|
||||
|
||||
// Sostituisci con il percorso corretto della tua FluxTextField
|
||||
import 'package:flux/core/widgets/flux_text_field.dart';
|
||||
import 'package:flux/features/onboarding/blocs/onboarding_state.dart';
|
||||
|
||||
class OnboardingScreen extends StatefulWidget {
|
||||
const OnboardingScreen({super.key});
|
||||
|
||||
@override
|
||||
State<OnboardingScreen> createState() => _OnboardingScreenState();
|
||||
}
|
||||
|
||||
class _OnboardingScreenState extends State<OnboardingScreen> {
|
||||
late PageController _pageController;
|
||||
|
||||
// --- CHIAVI DEI FORM (Per la validazione indipendente di ogni step) ---
|
||||
final _companyFormKey = GlobalKey<FormState>();
|
||||
final _storeFormKey = GlobalKey<FormState>();
|
||||
final _staffFormKey = GlobalKey<FormState>();
|
||||
|
||||
// --- CONTROLLERS: STEP 1 (Company) ---
|
||||
final _companyNameCtrl = TextEditingController();
|
||||
final _companyVatCtrl = TextEditingController();
|
||||
|
||||
// --- CONTROLLERS: STEP 2 (Store) ---
|
||||
final _storeNameCtrl = TextEditingController();
|
||||
final _storeAddressCtrl = TextEditingController();
|
||||
|
||||
// --- CONTROLLERS: STEP 3 (Staff) ---
|
||||
final _staffFirstNameCtrl = TextEditingController();
|
||||
final _staffLastNameCtrl = TextEditingController();
|
||||
final _staffJobTitleCtrl = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Calcoliamo la pagina iniziale in base allo step salvato nel Cubit
|
||||
final initialStep = context.read<OnboardingCubit>().state.step;
|
||||
_pageController = PageController(initialPage: _getPageIndex(initialStep));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pageController.dispose();
|
||||
_companyNameCtrl.dispose();
|
||||
_companyVatCtrl.dispose();
|
||||
_storeNameCtrl.dispose();
|
||||
_storeAddressCtrl.dispose();
|
||||
_staffFirstNameCtrl.dispose();
|
||||
_staffLastNameCtrl.dispose();
|
||||
_staffJobTitleCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
int _getPageIndex(OnboardingStep step) {
|
||||
switch (step) {
|
||||
case OnboardingStep.company:
|
||||
return 0;
|
||||
case OnboardingStep.store:
|
||||
return 1;
|
||||
case OnboardingStep.staff:
|
||||
return 2;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Validatore generico riutilizzabile
|
||||
String? _requireValidator(String? value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Campo obbligatorio';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocConsumer<OnboardingCubit, OnboardingState>(
|
||||
// Ascoltiamo i cambi di stato per animare la pagina e mostrare errori
|
||||
listenWhen: (previous, current) =>
|
||||
previous.step != current.step || previous.error != current.error,
|
||||
listener: (context, state) {
|
||||
// Gestione Errori
|
||||
if (state.error != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.error!), backgroundColor: Colors.red),
|
||||
);
|
||||
}
|
||||
|
||||
// Se ha finito, non animiamo nulla: il GoRouter prenderà il controllo a breve!
|
||||
if (state.step == OnboardingStep.completed) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text("Configurazione completata! Benvenuto a bordo 🚀"),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Animazione cambio pagina
|
||||
if (state.step != OnboardingStep.completed) {
|
||||
final targetPage = _getPageIndex(state.step);
|
||||
if (_pageController.hasClients &&
|
||||
_pageController.page?.round() != targetPage) {
|
||||
_pageController.animateToPage(
|
||||
targetPage,
|
||||
duration: const Duration(milliseconds: 600),
|
||||
curve: Curves.easeInOutCubic, // Animazione super fluida
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Stack(
|
||||
children: [
|
||||
// IL PAGEVIEW CORAZZATO
|
||||
PageView(
|
||||
controller: _pageController,
|
||||
physics:
|
||||
const NeverScrollableScrollPhysics(), // Vietato lo swipe manuale!
|
||||
children: [
|
||||
_buildCompanyForm(context, state),
|
||||
_buildStoreForm(context, state),
|
||||
_buildStaffForm(context, state),
|
||||
],
|
||||
),
|
||||
|
||||
// OVERLAY CARICAMENTO
|
||||
if (state.isLoading)
|
||||
Container(
|
||||
color: Colors.black.withValues(alpha: 0.4),
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// SCHERMATE DEI SINGOLI STEP
|
||||
// =========================================================================
|
||||
|
||||
Widget _buildCompanyForm(BuildContext context, OnboardingState state) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
child: Form(
|
||||
key: _companyFormKey,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(
|
||||
"Iniziamo! 🏢",
|
||||
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
"Inserisci i dati della tua attività per configurare il tuo ambiente FLUX.",
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 48),
|
||||
|
||||
FluxTextField(
|
||||
label: 'Ragione Sociale / Nome Azienda',
|
||||
controller: _companyNameCtrl,
|
||||
validator: _requireValidator,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Partita IVA',
|
||||
controller: _companyVatCtrl,
|
||||
validator: _requireValidator,
|
||||
),
|
||||
|
||||
const Spacer(),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
if (_companyFormKey.currentState!.validate()) {
|
||||
// MOCK DI ESEMPIO: Sostituisci con il tuo vero CompanyModel
|
||||
final newCompany = CompanyModel(
|
||||
ownerId: '', // Questo lo gestirà o ignorerà il Cubit
|
||||
name: _companyNameCtrl.text.trim(),
|
||||
vatNumber: _companyVatCtrl.text.trim(),
|
||||
);
|
||||
context.read<OnboardingCubit>().saveCompany(newCompany);
|
||||
}
|
||||
},
|
||||
child: const Text(
|
||||
"Salva e Prosegui",
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStoreForm(BuildContext context, OnboardingState state) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
child: Form(
|
||||
key: _storeFormKey,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(
|
||||
"Il tuo Negozio 🏪",
|
||||
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
"Dove si trova il tuo punto vendita principale? (Potrai aggiungerne altri in seguito).",
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 48),
|
||||
|
||||
FluxTextField(
|
||||
label: 'Nome Negozio (es. Sede Centrale)',
|
||||
controller: _storeNameCtrl,
|
||||
validator: _requireValidator,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Indirizzo completo',
|
||||
controller: _storeAddressCtrl,
|
||||
validator: _requireValidator,
|
||||
),
|
||||
|
||||
const Spacer(),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
if (_storeFormKey.currentState!.validate()) {
|
||||
final newStore = StoreModel(
|
||||
companyId: '', // Iniettato dal Cubit
|
||||
name: _storeNameCtrl.text.trim(),
|
||||
address: _storeAddressCtrl.text.trim(),
|
||||
isActive: true,
|
||||
);
|
||||
context.read<OnboardingCubit>().saveStore(newStore);
|
||||
}
|
||||
},
|
||||
child: const Text(
|
||||
"Salva Negozio",
|
||||
style: TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStaffForm(BuildContext context, OnboardingState state) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
child: Form(
|
||||
key: _staffFormKey,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(
|
||||
"Il tuo Profilo 👤",
|
||||
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
"Ultimo step! Crea il tuo profilo operativo per iniziare a usare FLUX.",
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 48),
|
||||
|
||||
FluxTextField(
|
||||
label: 'Nome',
|
||||
controller: _staffFirstNameCtrl,
|
||||
validator: _requireValidator,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Cognome',
|
||||
controller: _staffLastNameCtrl,
|
||||
validator: _requireValidator,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Etichetta Ruolo (es. Titolare, Manager)',
|
||||
controller: _staffJobTitleCtrl,
|
||||
// Il jobTitle può anche essere opzionale, decidi tu!
|
||||
),
|
||||
|
||||
const Spacer(),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
backgroundColor: Colors.black, // O il tuo context.accent
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
if (_staffFormKey.currentState!.validate()) {
|
||||
final newStaff = StaffMemberModel(
|
||||
companyId: '', // Iniettato dal Cubit
|
||||
storeId: '', // Iniettato dal Cubit
|
||||
userId: '', // Iniettato dal Cubit
|
||||
name: _staffFirstNameCtrl.text.trim(),
|
||||
surname: _staffLastNameCtrl.text.trim(),
|
||||
jobTitle: _staffJobTitleCtrl.text.trim(),
|
||||
// systemRole non viene passato qui: la Paranoia Mode del Cubit forzerà "admin"
|
||||
);
|
||||
context.read<OnboardingCubit>().saveStaff(newStaff);
|
||||
}
|
||||
},
|
||||
child: const Text(
|
||||
"Entra in FLUX",
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user