107 lines
3.3 KiB
Dart
107 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:firebase_auth/firebase_auth.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;
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _signInWithEmailAndPassword() async {
|
|
setState(() {
|
|
_errorMessage = '';
|
|
_isLoading = true;
|
|
});
|
|
try {
|
|
await FirebaseAuth.instance.signInWithEmailAndPassword(
|
|
email: _emailController.text.trim(),
|
|
password: _passwordController.text,
|
|
);
|
|
// Navigation vers la page principale après connexion réussie (à implémenter)
|
|
// Par exemple : Navigator.of(context).pushReplacementNamed('/home');
|
|
} on FirebaseAuthException catch (e) {
|
|
setState(() {
|
|
_errorMessage = "Erreur de connexion: ${e.message}";
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Connexion'),
|
|
),
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
// Pour éviter le débordement sur les petits écrans
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Image.asset(
|
|
'assets/EM2_NsurB.jpg',
|
|
height: 150,
|
|
),
|
|
const SizedBox(height: 20),
|
|
const Text(
|
|
'Bienvenue !',
|
|
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 20),
|
|
TextField(
|
|
controller: _emailController,
|
|
keyboardType: TextInputType.emailAddress,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
TextField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Mot de passe',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: _isLoading
|
|
? null
|
|
: _signInWithEmailAndPassword, // Désactiver pendant le chargement
|
|
child: _isLoading
|
|
? const CircularProgressIndicator() // Indicateur de chargement
|
|
: const Text('Se connecter'),
|
|
),
|
|
const SizedBox(height: 10),
|
|
const SizedBox(height: 10),
|
|
if (_errorMessage.isNotEmpty)
|
|
Text(
|
|
_errorMessage,
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|