154 lines
4.7 KiB
Dart
154 lines
4.7 KiB
Dart
import 'package:em2rp/views/widgets/auth/mail_textfield.dart';
|
|
import 'package:em2rp/views/widgets/error_message.dart';
|
|
import 'package:em2rp/views/widgets/auth/forgot_password_button.dart';
|
|
import 'package:em2rp/views/widgets/image/big_image_left.dart';
|
|
import 'package:em2rp/views/widgets/auth/login_button.dart';
|
|
import 'package:em2rp/views/widgets/image/em2_logo_n_sur_b.dart';
|
|
import 'package:em2rp/views/widgets/auth/password_textfield.dart';
|
|
import 'package:em2rp/views/widgets/auth/welcome_text.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
String _errorMessage = '';
|
|
bool _isLoading = false;
|
|
bool _obscurePassword = true;
|
|
bool _highlightPasswordField = false;
|
|
bool _highlightEmailField = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _signInWithEmailAndPassword() async {
|
|
setState(() {
|
|
_errorMessage = '';
|
|
_isLoading = true;
|
|
_highlightPasswordField = false;
|
|
_highlightEmailField = false;
|
|
});
|
|
try {
|
|
await FirebaseAuth.instance.signInWithEmailAndPassword(
|
|
email: _emailController.text.trim(),
|
|
password: _passwordController.text,
|
|
);
|
|
Navigator.of(context).pushReplacementNamed('/calendar');
|
|
print('Connexion réussie!');
|
|
} on FirebaseAuthException catch (e) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
if (e.code == 'user-not-found' || e.code == 'wrong-password') {
|
|
_errorMessage = "Email ou mot de passe incorrect.";
|
|
_highlightPasswordField = true;
|
|
_highlightEmailField = true;
|
|
} else if (e.code == 'invalid-email') {
|
|
_errorMessage = "Adresse email invalide.";
|
|
_highlightEmailField = true;
|
|
} else {
|
|
_errorMessage = "Erreur de connexion. Veuillez réessayer.";
|
|
}
|
|
});
|
|
print('Erreur de connexion: ${e.code} - ${e.message}');
|
|
}
|
|
}
|
|
|
|
void _togglePasswordVisibility() {
|
|
setState(() {
|
|
_obscurePassword = !_obscurePassword;
|
|
});
|
|
}
|
|
|
|
void _forgotPassword() {
|
|
// TODO: Implémenter la logique de mot de passe oublié
|
|
print("Mot de passe oublié cliqué !");
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
if (constraints.maxWidth > 900) {
|
|
return _buildLargeScreenLayout(context);
|
|
} else {
|
|
return _buildSmallScreenLayout(context);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildLargeScreenLayout(BuildContext context) {
|
|
return Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
flex: 6,
|
|
child:
|
|
const ImageSectionWidget(), // Utilise le composant ImageSection
|
|
),
|
|
Expanded(
|
|
flex: 4,
|
|
child: Padding(
|
|
padding:
|
|
const EdgeInsets.symmetric(horizontal: 50.0, vertical: 30.0),
|
|
child: _buildLoginForm(context),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildSmallScreenLayout(BuildContext context) {
|
|
return Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: _buildLoginForm(context),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildLoginForm(BuildContext context) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
const LogoWidget(), // Utilise le widget LogoWidget
|
|
const SizedBox(height: 30),
|
|
const WelcomeTextWidget(), // Utilise le widget WelcomeTextWidget
|
|
const SizedBox(height: 40),
|
|
EmailTextFieldWidget(
|
|
emailController: _emailController,
|
|
highlightEmailField: _highlightEmailField,
|
|
),
|
|
const SizedBox(height: 20),
|
|
PasswordTextFieldWidget(
|
|
passwordController: _passwordController,
|
|
obscurePassword: _obscurePassword,
|
|
highlightPasswordField: _highlightPasswordField,
|
|
onTogglePasswordVisibility: _togglePasswordVisibility,
|
|
),
|
|
ForgotPasswordButtonWidget(onPressed: _forgotPassword),
|
|
const SizedBox(height: 30),
|
|
LoginButtonWidget(
|
|
isLoading: _isLoading,
|
|
onPressed: _signInWithEmailAndPassword,
|
|
),
|
|
const SizedBox(height: 20),
|
|
ErrorMessageWidget(errorMessage: _errorMessage),
|
|
],
|
|
);
|
|
}
|
|
}
|