- Sauvegarde des fichiers critiques - Préparation transformation ApiService en singleton - Préparation création CurrentUserService et CurrentAmicaleService - Objectif: renommer Box users -> user
208 lines
6.2 KiB
Dart
208 lines
6.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:geosector_app/app.dart';
|
|
import 'package:geosector_app/core/services/app_info_service.dart';
|
|
import 'package:geosector_app/presentation/widgets/connectivity_indicator.dart';
|
|
import 'package:geosector_app/presentation/widgets/profile_dialog.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
/// AppBar personnalisée pour les tableaux de bord
|
|
class DashboardAppBar extends StatelessWidget implements PreferredSizeWidget {
|
|
/// Le titre principal de l'AppBar (généralement le nom de l'application)
|
|
final String title;
|
|
|
|
/// Le titre de la page actuelle (optionnel)
|
|
final String? pageTitle;
|
|
|
|
/// Indique si le bouton "Nouveau passage" doit être affiché
|
|
final bool showNewPassageButton;
|
|
|
|
/// Callback appelé lorsque le bouton "Nouveau passage" est pressé
|
|
final VoidCallback? onNewPassagePressed;
|
|
|
|
/// Indique si l'utilisateur est un administrateur
|
|
final bool isAdmin;
|
|
|
|
/// Callback appelé lorsque le bouton de déconnexion est pressé
|
|
final VoidCallback? onLogoutPressed;
|
|
|
|
const DashboardAppBar({
|
|
super.key,
|
|
required this.title,
|
|
this.pageTitle,
|
|
this.showNewPassageButton = true,
|
|
this.onNewPassagePressed,
|
|
this.isAdmin = false,
|
|
this.onLogoutPressed,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
|
|
return AppBar(
|
|
title: _buildTitle(context),
|
|
backgroundColor: theme.colorScheme.primary,
|
|
foregroundColor: theme.colorScheme.onPrimary,
|
|
elevation: 4,
|
|
leading: _buildLogo(),
|
|
actions: _buildActions(context),
|
|
);
|
|
}
|
|
|
|
/// Construction du logo dans l'AppBar
|
|
Widget _buildLogo() {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Image.asset(
|
|
'assets/images/logo-geosector-1024.png',
|
|
width: 40,
|
|
height: 40,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Construction des actions de l'AppBar
|
|
List<Widget> _buildActions(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final List<Widget> actions = [];
|
|
|
|
// Ajouter l'indicateur de connectivité
|
|
actions.add(
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 12.0),
|
|
child: ConnectivityIndicator(
|
|
showErrorMessage: false,
|
|
showConnectionType: true,
|
|
),
|
|
),
|
|
);
|
|
|
|
actions.add(const SizedBox(width: 8));
|
|
|
|
// Ajouter la version de l'application
|
|
actions.add(
|
|
Text(
|
|
"v${AppInfoService.version}",
|
|
style: const TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.white70,
|
|
),
|
|
),
|
|
);
|
|
|
|
actions.add(const SizedBox(width: 8));
|
|
|
|
actions.add(
|
|
TextButton.icon(
|
|
icon: const Icon(Icons.add_location_alt, color: Colors.white),
|
|
label: const Text('Nouveau passage', style: TextStyle(color: Colors.white)),
|
|
onPressed: onNewPassagePressed,
|
|
style: TextButton.styleFrom(
|
|
backgroundColor: theme.colorScheme.secondary,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
),
|
|
),
|
|
);
|
|
|
|
actions.add(const SizedBox(width: 8));
|
|
|
|
// Ajouter le bouton "Mon compte"
|
|
actions.add(
|
|
IconButton(
|
|
icon: const Icon(Icons.person),
|
|
tooltip: 'Mon compte',
|
|
onPressed: () {
|
|
// Afficher la boîte de dialogue de profil avec l'utilisateur actuel
|
|
final user = userRepository.currentUser;
|
|
if (user != null) {
|
|
ProfileDialog.show(context, user);
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: const Text('Erreur: Utilisateur non trouvé'),
|
|
backgroundColor: theme.colorScheme.error,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
|
|
actions.add(const SizedBox(width: 8));
|
|
|
|
// Ajouter le bouton de déconnexion
|
|
actions.add(
|
|
IconButton(
|
|
icon: const Icon(Icons.logout),
|
|
tooltip: 'Déconnexion',
|
|
onPressed: onLogoutPressed ??
|
|
() {
|
|
showDialog(
|
|
context: context,
|
|
builder: (dialogContext) => AlertDialog(
|
|
title: const Text('Déconnexion'),
|
|
content: const Text('Voulez-vous vraiment vous déconnecter ?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
|
child: const Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
// Fermer la dialog d'abord
|
|
Navigator.of(dialogContext).pop();
|
|
|
|
// Utiliser le context original de l'AppBar pour la navigation
|
|
final success = await userRepository.logout(context);
|
|
|
|
// Vérification supplémentaire et navigation forcée si nécessaire
|
|
if (success && context.mounted) {
|
|
// Attendre un court instant pour que les changements d'état se propagent
|
|
await Future.delayed(const Duration(milliseconds: 100));
|
|
|
|
// Navigation forcée vers la page d'accueil
|
|
context.go('/');
|
|
}
|
|
},
|
|
child: const Text('Déconnexion'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
|
|
actions.add(const SizedBox(width: 8)); // Espacement à droite
|
|
|
|
return actions;
|
|
}
|
|
|
|
/// Construction du titre de l'AppBar
|
|
Widget _buildTitle(BuildContext context) {
|
|
// Si aucun titre de page n'est fourni, afficher simplement le titre principal
|
|
if (pageTitle == null) {
|
|
return Text(title);
|
|
}
|
|
|
|
// Construire un titre composé en fonction du rôle de l'utilisateur
|
|
final String prefix = isAdmin ? 'Administration' : title;
|
|
return Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(prefix),
|
|
const Text(' - '),
|
|
Text(pageTitle!),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
|
}
|