import 'package:flutter/material.dart'; import 'package:p_tits_pas/services/auth_service.dart'; class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidget { final int selectedIndex; final ValueChanged onTabChange; const DashboardAppBarAdmin({Key? key, required this.selectedIndex, required this.onTabChange}) : super(key: key); void _Logout(BuildContext context) async { try { final authS = AuthService(); await authS.logout(); Navigator.of(context).pushReplacementNamed('/login'); } catch (e) { print('Erreur lors de la déconnexion: $e'); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Erreur lors de la déconnexion'), backgroundColor: Colors.red, ), ); } } Future _getUserName() async { final authS = AuthService(); final userName = await authS.getUserNameById(); return userName ?? 'Admin'; } @override Size get preferredSize => const Size.fromHeight(kToolbarHeight + 10); @override Widget build(BuildContext context) { final isMobile = MediaQuery.of(context).size.width < 768; return AppBar( elevation: 0, automaticallyImplyLeading: false, title: Row( children: [ SizedBox(width: MediaQuery.of(context).size.width * 0.19), const Text( "P'tit Pas", style: TextStyle( color: Color(0xFF9CC5C0), fontWeight: FontWeight.bold, fontSize: 18, ), ), SizedBox(width: MediaQuery.of(context).size.width * 0.1), // Navigation principale // _buildNavItem(context, 'Gestionnaires', 0), // const SizedBox(width: 24), // _buildNavItem(context, 'Parents', 1), // const SizedBox(width: 24), // _buildNavItem(context, 'Assistantes maternelles', 2), // const SizedBox(width: 24), // _buildNavItem(context, 'Administrateurs', 4), ], ), actions: isMobile ? [_buildMobileMenu(context)] : [ // Nom de l'utilisateur Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Center( child: FutureBuilder( future: _getUserName(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Text("Chargement..."); } else if (snapshot.hasError) { return const Text("Erreur"); } else { return Text(snapshot.data ?? "Admin"); } } ), ), ), // Bouton déconnexion Padding( padding: const EdgeInsets.only(right: 16), child: TextButton( onPressed: () => _handleLogout(context), style: TextButton.styleFrom( backgroundColor: const Color(0xFF9CC5C0), foregroundColor: Colors.white, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5), ), ), child: const Text('Se déconnecter'), ), ), SizedBox(width: MediaQuery.of(context).size.width * 0.1), ], ); } // Widget _buildNavItem(BuildContext context, String title, int index) { // final bool isActive = index == selectedIndex; // return InkWell( // onTap: () => onTabChange(index), // child: Container( // padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), // decoration: BoxDecoration( // color: isActive ? const Color(0xFF9CC5C0) : Colors.transparent, // borderRadius: BorderRadius.circular(20), // border: isActive ? null : Border.all(color: Colors.black26), // ), // child: Text( // title, // style: TextStyle( // color: isActive ? Colors.white : Colors.black, // fontWeight: isActive ? FontWeight.w600 : FontWeight.normal, // fontSize: 14, // ), // ), // ), // ); // } Widget _buildMobileMenu(BuildContext context) { return PopupMenuButton( icon: const Icon(Icons.menu, color: Colors.white), onSelected: (value) { if (value == 4) { _handleLogout(context); } }, itemBuilder: (context) => [ const PopupMenuItem(value: 0, child: Text("Gestionnaires")), const PopupMenuItem(value: 1, child: Text("Parents")), const PopupMenuItem(value: 2, child: Text("Assistantes maternelles")), const PopupMenuItem(value: 3, child: Text("Administrateurs")), const PopupMenuDivider(), const PopupMenuItem(value: 4, child: Text("Se déconnecter")), ], ); } void _handleLogout(BuildContext context) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Déconnexion'), content: const Text('Êtes-vous sûr de vouloir vous déconnecter ?'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('Annuler'), ), ElevatedButton( onPressed: () { // Navigator.pop(context); _Logout(context); }, child: const Text('Déconnecter'), ), ], ), ); } }