app mobile allarmi prima
This commit is contained in:
224
lib/screens/login_screen.dart
Normal file
224
lib/screens/login_screen.dart
Normal file
@@ -0,0 +1,224 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/constants.dart';
|
||||
import '../services/auth_service.dart';
|
||||
import '../services/notification_service.dart';
|
||||
import 'main_screen.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@override
|
||||
State<LoginScreen> createState() => _LoginScreenState();
|
||||
}
|
||||
|
||||
class _LoginScreenState extends State<LoginScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _emailController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
final _authService = AuthService();
|
||||
|
||||
bool _isLoading = false;
|
||||
bool _obscurePassword = true;
|
||||
String? _errorMessage;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_emailController.dispose();
|
||||
_passwordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _handleLogin() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final success = await _authService.login(
|
||||
_emailController.text.trim(),
|
||||
_passwordController.text,
|
||||
);
|
||||
|
||||
if (success && mounted) {
|
||||
// Registra FCM token dopo login riuscito
|
||||
try {
|
||||
final fcmToken = await NotificationService().getFcmToken();
|
||||
if (fcmToken != null) {
|
||||
await _authService.saveFcmToken(fcmToken);
|
||||
print('✓ FCM token registrato dopo login');
|
||||
}
|
||||
} catch (e) {
|
||||
print('⚠️ Errore registrazione FCM token: $e');
|
||||
// Non blocchiamo il login se la registrazione token fallisce
|
||||
}
|
||||
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(builder: (context) => const MainScreen()),
|
||||
);
|
||||
} else {
|
||||
setState(() {
|
||||
_errorMessage = 'Email o password non corretti';
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_errorMessage = 'Errore durante il login';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: AppColors.primaryGradient,
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildHeader(),
|
||||
const SizedBox(height: 60),
|
||||
_buildLoginCard(),
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
_buildFooter(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader() {
|
||||
return Column(
|
||||
children: [
|
||||
Container(
|
||||
width: 120,
|
||||
height: 120,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.2),
|
||||
blurRadius: 20,
|
||||
offset: const Offset(0, 10),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.terrain, size: 50, color: AppColors.primary),
|
||||
SizedBox(height: 4),
|
||||
Text('ASE', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: AppColors.primary)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
const Text('ASE Monitor', style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Colors.white)),
|
||||
const SizedBox(height: AppSizes.paddingS),
|
||||
Text('Advanced Slope Engineering', style: TextStyle(fontSize: 16, color: Colors.white.withOpacity(0.9))),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoginCard() {
|
||||
return Card(
|
||||
elevation: 8,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(AppSizes.radiusL)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingL),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text('Accedi', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), textAlign: TextAlign.center),
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(labelText: 'Email', hintText: 'nome@azienda.it', prefixIcon: Icon(Icons.email_outlined)),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) return 'Inserisci email';
|
||||
if (!value.contains('@')) return 'Email non valida';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
obscureText: _obscurePassword,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
prefixIcon: const Icon(Icons.lock_outline),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(_obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined),
|
||||
onPressed: () => setState(() => _obscurePassword = !_obscurePassword),
|
||||
),
|
||||
),
|
||||
validator: (value) => (value == null || value.isEmpty) ? 'Inserisci password' : null,
|
||||
onFieldSubmitted: (_) => _handleLogin(),
|
||||
),
|
||||
if (_errorMessage != null) ...[
|
||||
const SizedBox(height: AppSizes.paddingM),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(AppSizes.paddingM),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.critical.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(AppSizes.radiusS),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.error_outline, color: AppColors.critical, size: 20),
|
||||
const SizedBox(width: AppSizes.paddingS),
|
||||
Expanded(child: Text(_errorMessage!, style: const TextStyle(color: AppColors.critical))),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: AppSizes.paddingL),
|
||||
SizedBox(
|
||||
height: AppSizes.buttonHeight,
|
||||
child: ElevatedButton(
|
||||
onPressed: _isLoading ? null : _handleLogin,
|
||||
child: _isLoading
|
||||
? const SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2, valueColor: AlwaysStoppedAnimation(Colors.white)))
|
||||
: const Row(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(Icons.login), SizedBox(width: 8), Text('Accedi')]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFooter() {
|
||||
return Column(
|
||||
children: [
|
||||
Text('Versione ${AppConstants.appVersion}', style: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 12)),
|
||||
const SizedBox(height: 4),
|
||||
Text('© 2025 ${AppConstants.companyName}', style: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 12), textAlign: TextAlign.center),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user