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.
351 lines
10 KiB
351 lines
10 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:youmazgestion/Views/HandleProduct.dart';
|
|
import 'package:youmazgestion/Views/RoleListPage.dart';
|
|
import 'package:youmazgestion/Views/commandManagement.dart';
|
|
import 'package:youmazgestion/Views/historique.dart';
|
|
import 'package:youmazgestion/Views/bilanMois.dart';
|
|
import 'package:youmazgestion/Views/gestionStock.dart';
|
|
import 'package:youmazgestion/Views/listUser.dart';
|
|
import 'package:youmazgestion/Views/loginPage.dart';
|
|
import 'package:youmazgestion/Views/newCommand.dart';
|
|
import 'package:youmazgestion/Views/registrationPage.dart';
|
|
import 'package:youmazgestion/accueil.dart';
|
|
import 'package:youmazgestion/controller/userController.dart';
|
|
import 'package:youmazgestion/Views/pointage.dart';
|
|
|
|
class CustomDrawer extends StatelessWidget {
|
|
final UserController userController = Get.find<UserController>();
|
|
|
|
Future<void> clearUserData() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove('username');
|
|
await prefs.remove('role');
|
|
await prefs.remove('user_id');
|
|
|
|
userController.clearUserData();
|
|
}
|
|
|
|
CustomDrawer({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Drawer(
|
|
backgroundColor: Colors.white,
|
|
child: FutureBuilder(
|
|
future: _buildDrawerItems(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
return ListView(
|
|
padding: EdgeInsets.zero,
|
|
children: snapshot.data as List<Widget>,
|
|
);
|
|
} else {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<List<Widget>> _buildDrawerItems() async {
|
|
List<Widget> drawerItems = [];
|
|
|
|
drawerItems.add(
|
|
GetBuilder<UserController>(
|
|
builder: (controller) => Container(
|
|
padding: const EdgeInsets.only(top: 50, left: 20, bottom: 20),
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
colors: [Color.fromARGB(255, 4, 54, 95), Colors.blue],
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
const CircleAvatar(
|
|
radius: 30,
|
|
backgroundImage: AssetImage("assets/youmaz2.png"),
|
|
),
|
|
const SizedBox(width: 15),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
controller.name.isNotEmpty
|
|
? controller.name
|
|
: 'Utilisateur',
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
Text(
|
|
controller.role.isNotEmpty ? controller.role : 'Aucun rôle',
|
|
style: const TextStyle(
|
|
color: Colors.white70,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
drawerItems.add(
|
|
await _buildDrawerItem(
|
|
icon: Icons.home,
|
|
title: "Accueil",
|
|
color: Colors.blue,
|
|
permissionAction: 'view',
|
|
permissionRoute: '/accueil',
|
|
onTap: () => Get.to(const AccueilPage()),
|
|
),
|
|
);
|
|
|
|
List<Widget> gestionUtilisateursItems = [
|
|
await _buildDrawerItem(
|
|
icon: Icons.person_add,
|
|
title: "Ajouter un utilisateur",
|
|
color: Colors.green,
|
|
permissionAction: 'create',
|
|
permissionRoute: '/ajouter-utilisateur',
|
|
onTap: () => Get.to(const RegistrationPage()),
|
|
),
|
|
await _buildDrawerItem(
|
|
icon: Icons.supervised_user_circle,
|
|
title: "Gérer les utilisateurs",
|
|
color: const Color.fromARGB(255, 4, 54, 95),
|
|
permissionAction: 'update',
|
|
permissionRoute: '/modifier-utilisateur',
|
|
onTap: () => Get.to(const ListUserPage()),
|
|
),
|
|
await _buildDrawerItem(
|
|
icon: Icons.timer,
|
|
title: "Gestion des pointages",
|
|
color: const Color.fromARGB(255, 4, 54, 95),
|
|
permissionAction: 'update',
|
|
permissionRoute: '/pointage',
|
|
onTap: () => Get.to(const PointagePage()),
|
|
)
|
|
];
|
|
|
|
if (gestionUtilisateursItems.any((item) => item is ListTile)) {
|
|
drawerItems.add(
|
|
const Padding(
|
|
padding: EdgeInsets.only(left: 20, top: 15, bottom: 5),
|
|
child: Text(
|
|
"GESTION UTILISATEURS",
|
|
style: TextStyle(
|
|
color: Colors.grey,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
drawerItems.addAll(gestionUtilisateursItems);
|
|
}
|
|
|
|
List<Widget> gestionProduitsItems = [
|
|
await _buildDrawerItem(
|
|
icon: Icons.inventory,
|
|
title: "Gestion des produits",
|
|
color: Colors.indigoAccent,
|
|
permissionAction: 'create',
|
|
permissionRoute: '/ajouter-produit',
|
|
onTap: () => Get.to(const ProductManagementPage()),
|
|
),
|
|
await _buildDrawerItem(
|
|
icon: Icons.storage,
|
|
title: "Gestion de stock",
|
|
color: Colors.blueAccent,
|
|
permissionAction: 'update',
|
|
permissionRoute: '/gestion-stock',
|
|
onTap: () => Get.to(const GestionStockPage()),
|
|
),
|
|
];
|
|
|
|
if (gestionProduitsItems.any((item) => item is ListTile)) {
|
|
drawerItems.add(
|
|
const Padding(
|
|
padding: EdgeInsets.only(left: 20, top: 15, bottom: 5),
|
|
child: Text(
|
|
"GESTION PRODUITS",
|
|
style: TextStyle(
|
|
color: Colors.grey,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
drawerItems.addAll(gestionProduitsItems);
|
|
}
|
|
|
|
List<Widget> gestionCommandesItems = [
|
|
await _buildDrawerItem(
|
|
icon: Icons.add_shopping_cart,
|
|
title: "Nouvelle commande",
|
|
color: Colors.orange,
|
|
permissionAction: 'create',
|
|
permissionRoute: '/nouvelle-commande',
|
|
onTap: () => Get.to(const NouvelleCommandePage()),
|
|
),
|
|
await _buildDrawerItem(
|
|
icon: Icons.list_alt,
|
|
title: "Gérer les commandes",
|
|
color: Colors.deepPurple,
|
|
permissionAction: 'manage',
|
|
permissionRoute: '/gerer-commandes',
|
|
onTap: () => Get.to(const GestionCommandesPage()),
|
|
),
|
|
];
|
|
|
|
if (gestionCommandesItems.any((item) => item is ListTile)) {
|
|
drawerItems.add(
|
|
const Padding(
|
|
padding: EdgeInsets.only(left: 20, top: 15, bottom: 5),
|
|
child: Text(
|
|
"GESTION COMMANDES",
|
|
style: TextStyle(
|
|
color: Colors.grey,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
drawerItems.addAll(gestionCommandesItems);
|
|
}
|
|
|
|
List<Widget> rapportsItems = [
|
|
await _buildDrawerItem(
|
|
icon: Icons.bar_chart,
|
|
title: "Bilan mensuel",
|
|
color: Colors.teal,
|
|
permissionAction: 'read',
|
|
permissionRoute: '/bilan',
|
|
onTap: () => Get.to(const BilanMois()),
|
|
),
|
|
await _buildDrawerItem(
|
|
icon: Icons.history,
|
|
title: "Historique",
|
|
color: Colors.blue,
|
|
permissionAction: 'read',
|
|
permissionRoute: '/historique',
|
|
onTap: () => Get.to(HistoryPage()),
|
|
),
|
|
];
|
|
|
|
if (rapportsItems.any((item) => item is ListTile)) {
|
|
drawerItems.add(
|
|
const Padding(
|
|
padding: EdgeInsets.only(left: 20, top: 15, bottom: 5),
|
|
child: Text(
|
|
"RAPPORTS",
|
|
style: TextStyle(
|
|
color: Colors.grey,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
drawerItems.addAll(rapportsItems);
|
|
}
|
|
|
|
List<Widget> administrationItems = [
|
|
await _buildDrawerItem(
|
|
icon: Icons.admin_panel_settings,
|
|
title: "Gérer les rôles",
|
|
color: Colors.redAccent,
|
|
permissionAction: 'admin',
|
|
permissionRoute: '/gerer-roles',
|
|
onTap: () => Get.to(const RoleListPage()),
|
|
),
|
|
];
|
|
|
|
if (administrationItems.any((item) => item is ListTile)) {
|
|
drawerItems.add(
|
|
const Padding(
|
|
padding: EdgeInsets.only(left: 20, top: 15, bottom: 5),
|
|
child: Text(
|
|
"ADMINISTRATION",
|
|
style: TextStyle(
|
|
color: Colors.grey,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
drawerItems.addAll(administrationItems);
|
|
}
|
|
|
|
drawerItems.add(const Divider());
|
|
|
|
drawerItems.add(
|
|
ListTile(
|
|
leading: const Icon(Icons.logout, color: Colors.red),
|
|
title: const Text("Déconnexion"),
|
|
onTap: () {
|
|
Get.defaultDialog(
|
|
title: "Déconnexion",
|
|
content: const Text("Voulez-vous vraiment vous déconnecter ?"),
|
|
actions: [
|
|
TextButton(
|
|
child: const Text("Non"),
|
|
onPressed: () => Get.back(),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red,
|
|
),
|
|
child: const Text("Oui"),
|
|
onPressed: () async {
|
|
await clearUserData();
|
|
Get.offAll(const LoginPage());
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
|
|
return drawerItems;
|
|
}
|
|
|
|
Future<Widget> _buildDrawerItem({
|
|
required IconData icon,
|
|
required String title,
|
|
required Color color,
|
|
String? permissionAction,
|
|
String? permissionRoute,
|
|
required VoidCallback onTap,
|
|
}) async {
|
|
if (permissionAction != null && permissionRoute != null) {
|
|
bool hasPermission =
|
|
await userController.hasPermission(permissionAction, permissionRoute);
|
|
if (!hasPermission) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
}
|
|
|
|
return ListTile(
|
|
leading: Icon(icon, color: color),
|
|
title: Text(title),
|
|
trailing: permissionAction != null
|
|
? const Icon(Icons.chevron_right, color: Colors.grey)
|
|
: null,
|
|
onTap: onTap,
|
|
);
|
|
}
|
|
}
|
|
|