2026-04-06 10:55:56 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-04-07 11:30:22 +02:00
|
|
|
import 'package:flux/core/theme/theme.dart';
|
2026-04-08 12:14:57 +02:00
|
|
|
import 'package:flux/core/widgets/flux_logo.dart';
|
2026-04-07 11:30:22 +02:00
|
|
|
import 'package:flux/core/widgets/flux_text_field.dart';
|
2026-04-22 11:06:02 +02:00
|
|
|
import 'package:flux/features/auth/bloc/auth_cubit.dart';
|
2026-04-06 10:55:56 +02:00
|
|
|
|
|
|
|
|
class AuthScreen extends StatefulWidget {
|
|
|
|
|
const AuthScreen({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<AuthScreen> createState() => _AuthScreenState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AuthScreenState extends State<AuthScreen> {
|
|
|
|
|
final _emailController = TextEditingController();
|
|
|
|
|
final _passwordController = TextEditingController();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_emailController.dispose();
|
|
|
|
|
_passwordController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 11:06:02 +02:00
|
|
|
void _submit() {
|
|
|
|
|
// Chiudiamo la tastiera per fare pulizia a schermo
|
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
|
|
|
|
|
|
context.read<AuthCubit>().submitAuth(
|
|
|
|
|
_emailController.text.trim(),
|
|
|
|
|
_passwordController.text.trim(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 10:55:56 +02:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2026-04-07 11:55:01 +02:00
|
|
|
return Scaffold(
|
2026-04-22 11:06:02 +02:00
|
|
|
body: BlocConsumer<AuthCubit, AuthState>(
|
|
|
|
|
// Ottimizzazione: Ridisegniamo la UI solo quando cambia lo status o la modalità
|
|
|
|
|
listenWhen: (previous, current) =>
|
|
|
|
|
previous.errorMessage != current.errorMessage ||
|
|
|
|
|
previous.infoMessage != current.infoMessage,
|
2026-04-07 11:55:01 +02:00
|
|
|
listener: (context, state) {
|
2026-04-22 11:06:02 +02:00
|
|
|
// Mostriamo l'errore se c'è
|
|
|
|
|
if (state.errorMessage != null) {
|
2026-04-07 11:55:01 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
2026-04-22 11:06:02 +02:00
|
|
|
content: Text(state.errorMessage!),
|
2026-04-07 11:55:01 +02:00
|
|
|
backgroundColor: Colors.redAccent,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-22 11:06:02 +02:00
|
|
|
// Mostriamo il messaggio info (es. Conferma Email)
|
|
|
|
|
if (state.infoMessage != null) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text(state.infoMessage!),
|
|
|
|
|
backgroundColor: Colors.blueAccent, // O context.accent
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-07 11:55:01 +02:00
|
|
|
},
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final isLoading = state.status == AuthStatus.loading;
|
|
|
|
|
|
|
|
|
|
return SafeArea(
|
|
|
|
|
child: Center(
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 32),
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
// --- LOGO FLUX ---
|
2026-04-22 11:06:02 +02:00
|
|
|
const FluxLogoAuto(height: 80),
|
2026-04-07 11:55:01 +02:00
|
|
|
const SizedBox(height: 60),
|
|
|
|
|
|
|
|
|
|
// --- TITOLO DINAMICO ---
|
|
|
|
|
Text(
|
|
|
|
|
state.isLoginMode ? 'BENTORNATO' : 'CREA ACCOUNT',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: context.primaryText,
|
|
|
|
|
fontSize: 24,
|
|
|
|
|
fontWeight: FontWeight.w900,
|
|
|
|
|
letterSpacing: 1.5,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
Text(
|
|
|
|
|
state.isLoginMode
|
|
|
|
|
? 'Accedi per gestire il tuo business'
|
|
|
|
|
: 'Inizia oggi a digitalizzare il tuo negozio',
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
style: TextStyle(color: context.secondaryText),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 40),
|
|
|
|
|
|
|
|
|
|
// --- CAMPI INPUT ---
|
|
|
|
|
FluxTextField(
|
|
|
|
|
label: 'Email Aziendale',
|
|
|
|
|
icon: Icons.email_outlined,
|
|
|
|
|
controller: _emailController,
|
|
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
FluxTextField(
|
|
|
|
|
label: 'Password',
|
|
|
|
|
icon: Icons.lock_outline,
|
2026-04-22 11:06:02 +02:00
|
|
|
isPassword: true, // Magia del FluxTextField!
|
2026-04-07 11:55:01 +02:00
|
|
|
controller: _passwordController,
|
2026-04-22 11:06:02 +02:00
|
|
|
onSubmitted: (_) =>
|
|
|
|
|
_submit(), // Se lo supporti nel tuo widget custom
|
2026-04-07 11:55:01 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
const SizedBox(height: 40),
|
|
|
|
|
|
|
|
|
|
// --- BOTTONE PRINCIPALE ---
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
height: 56,
|
|
|
|
|
child: ElevatedButton(
|
2026-04-22 11:06:02 +02:00
|
|
|
onPressed: isLoading ? null : _submit,
|
2026-04-07 11:55:01 +02:00
|
|
|
child: isLoading
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
height: 24,
|
|
|
|
|
width: 24,
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
strokeWidth: 2,
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-04-22 11:06:02 +02:00
|
|
|
: Text(
|
|
|
|
|
state.isLoginMode ? 'ACCEDI' : 'REGISTRATI',
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-04-07 11:55:01 +02:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// --- SWITCH LOGIN/SIGNUP ---
|
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: isLoading
|
|
|
|
|
? null
|
2026-04-22 11:06:02 +02:00
|
|
|
: () => context.read<AuthCubit>().toggleMode(),
|
2026-04-07 11:55:01 +02:00
|
|
|
child: RichText(
|
|
|
|
|
text: TextSpan(
|
|
|
|
|
text: state.isLoginMode
|
|
|
|
|
? "Non hai un account? "
|
|
|
|
|
: "Hai già un account? ",
|
|
|
|
|
style: TextStyle(color: context.secondaryText),
|
|
|
|
|
children: [
|
|
|
|
|
TextSpan(
|
|
|
|
|
text: state.isLoginMode ? "Registrati" : "Accedi",
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: context.accent,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-06 10:55:56 +02:00
|
|
|
}
|