You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
5.8 KiB
183 lines
5.8 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:youmazgestion/Views/particles.dart' show ParticleBackground;
|
|
import 'package:youmazgestion/accueil.dart';
|
|
import 'package:youmazgestion/Services/authDatabase.dart';
|
|
|
|
import '../Models/users.dart';
|
|
import '../controller/userController.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
_LoginPageState createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
late TextEditingController _usernameController;
|
|
late TextEditingController _passwordController;
|
|
final UserController userController = Get.put(UserController());
|
|
bool _isErrorVisible = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_usernameController = TextEditingController();
|
|
_passwordController = TextEditingController();
|
|
checkUserCount();
|
|
}
|
|
|
|
void checkUserCount() async {
|
|
final userCount = await AuthDatabase.instance.getUserCount();
|
|
if (userCount == 0) {
|
|
// No user found, redirect to home page
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const AccueilPage()),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
Future<void> saveUser(String? username,String? role)async{
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('username', username!);
|
|
await prefs.setString('role', role!);
|
|
}
|
|
void _login() async {
|
|
final String username = _usernameController.text;
|
|
final String password = _passwordController.text;
|
|
print(username);
|
|
print(password);
|
|
|
|
try {
|
|
bool isValidUser =
|
|
await AuthDatabase.instance.verifyUser(username, password);
|
|
Users user = await AuthDatabase.instance.getUser(username);
|
|
|
|
Map<String, String>? getUserCredentials = await AuthDatabase.instance.getUserCredentials(username,password);
|
|
print(isValidUser);
|
|
if (isValidUser) {
|
|
print('User is valid');
|
|
print(user);
|
|
userController.setUser(user);
|
|
|
|
setState(() {
|
|
_isErrorVisible = false;
|
|
});
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const AccueilPage()),
|
|
);
|
|
saveUser(getUserCredentials?['username'], getUserCredentials?['role']);
|
|
} else {
|
|
setState(() {
|
|
_isErrorVisible = true;
|
|
});
|
|
}
|
|
} catch (error) {
|
|
print(error);
|
|
setState(() {
|
|
_isErrorVisible = true;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text(
|
|
'Login',
|
|
style: TextStyle(color:Colors.white),
|
|
),
|
|
backgroundColor: const Color.fromARGB(255, 4, 54, 95),
|
|
centerTitle: true,
|
|
),
|
|
body: ParticleBackground(
|
|
child: Center(
|
|
child: Container(
|
|
width: MediaQuery.of(context).size.width * 0.5,
|
|
height: MediaQuery.of(context).size.height * 0.8,
|
|
padding: const EdgeInsets.all(16.0),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
shape: BoxShape.rectangle,
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
|
child: const Icon(
|
|
Icons.lock_outline,
|
|
size: 100.0,
|
|
color: Color.fromARGB(255, 4, 54, 95),
|
|
),
|
|
),
|
|
TextField(
|
|
controller: _usernameController,
|
|
decoration: InputDecoration(
|
|
labelText: 'Username',
|
|
prefixIcon:
|
|
const Icon(Icons.person, color: Colors.blueAccent),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
TextField(
|
|
controller: _passwordController,
|
|
decoration: InputDecoration(
|
|
labelText: 'Password',
|
|
prefixIcon: const Icon(Icons.lock, color: Colors.redAccent),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
),
|
|
obscureText: true,
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
Visibility(
|
|
visible: _isErrorVisible,
|
|
child: const Text(
|
|
'Invalid username or password',
|
|
style: TextStyle(
|
|
color: Colors.red,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
ElevatedButton(
|
|
onPressed: _login,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF0015B7), // Nouvelle couleur
|
|
elevation: 5.0,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
),
|
|
child: const Text(
|
|
'Login',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|