import 'package:flutter/material.dart'; import 'package:p_tits_pas/services/auth_service.dart'; class DashboardAppBar extends StatelessWidget implements PreferredSizeWidget { final int selectedIndex; final ValueChanged onTabChange; const DashboardAppBar({Key? key, required this.selectedIndex, required this.onTabChange}) : super(key: key); @override Size get preferredSize => const Size.fromHeight(kToolbarHeight + 10); Future _getUserName() async { final authS = AuthService(); final userName = await authS.getUserNameById(); return userName ?? 'Bienvenue'; } void _logout (BuildContext context) { try { final authS = AuthService(); 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, ), ); } } @override Widget build(BuildContext context) { final isMobile = MediaQuery.of(context).size.width < 768; return AppBar( // backgroundColor: Colors.white, elevation: 0, 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, 'Mon tableau de bord', 0), const SizedBox(width: 24), _buildNavItem(context, 'Trouver une nounou', 1), const SizedBox(width: 24), _buildNavItem(context, 'Paramètres', 2), ], ), 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 == 3) { _handleLogout(context); } }, itemBuilder: (context) => [ const PopupMenuItem(value: 0, child: Text("Mon tableau de bord")), const PopupMenuItem(value: 1, child: Text("Trouver une nounou")), const PopupMenuItem(value: 2, child: Text("Paramètres")), const PopupMenuDivider(), const PopupMenuItem(value: 3, 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); // TODO: Implémenter la logique de déconnexion }, child: const Text('Déconnecter'), ), ], ), ); } }