170 lines
5.3 KiB
Dart
170 lines
5.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:geosector_app/app.dart';
|
|
import 'package:geosector_app/presentation/widgets/connectivity_indicator.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;
|
|
|
|
/// Actions supplémentaires à afficher dans l'AppBar
|
|
final List<Widget>? additionalActions;
|
|
|
|
/// 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({
|
|
Key? key,
|
|
required this.title,
|
|
this.pageTitle,
|
|
this.additionalActions,
|
|
this.showNewPassageButton = true,
|
|
this.onNewPassagePressed,
|
|
this.isAdmin = false,
|
|
this.onLogoutPressed,
|
|
}) : super(key: key);
|
|
|
|
@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: Image.asset(
|
|
'assets/images/geosector-logo-80.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(
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 12.0),
|
|
child: const ConnectivityIndicator(
|
|
showErrorMessage: false,
|
|
showConnectionType: true,
|
|
),
|
|
),
|
|
);
|
|
|
|
// Ajouter les actions supplémentaires si elles existent
|
|
if (additionalActions != null && additionalActions!.isNotEmpty) {
|
|
actions.addAll(additionalActions!);
|
|
} else if (showNewPassageButton) {
|
|
// Ajouter le bouton "Nouveau passage" en haut à droite
|
|
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),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// Ajouter le bouton de déconnexion
|
|
actions.add(
|
|
IconButton(
|
|
icon: const Icon(Icons.logout),
|
|
tooltip: 'Déconnexion',
|
|
onPressed: onLogoutPressed ??
|
|
() {
|
|
// Si aucun callback n'est fourni, utiliser le userRepository global
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Déconnexion'),
|
|
content:
|
|
const Text('Voulez-vous vraiment vous déconnecter ?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
Navigator.of(context).pop();
|
|
// Appeler la méthode logout du userRepository
|
|
// qui nettoie les hive boxes, lance la requête API logout
|
|
// et supprime user.sessionId
|
|
await userRepository.logout();
|
|
|
|
// Rediriger vers la landing page
|
|
if (context.mounted) {
|
|
// Utiliser go_router pour la navigation
|
|
context.go('/public');
|
|
}
|
|
},
|
|
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);
|
|
}
|