Ajout d'utilisateur OK
Ajout bouton de deconnexion
This commit is contained in:
65
em2rp/lib/widgets/custom_app_bar.dart
Normal file
65
em2rp/lib/widgets/custom_app_bar.dart
Normal file
@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:em2rp/providers/local_user_provider.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
|
||||
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final String title;
|
||||
final List<Widget>? actions;
|
||||
final bool showLogoutButton;
|
||||
|
||||
const CustomAppBar({
|
||||
Key? key,
|
||||
required this.title,
|
||||
this.actions,
|
||||
this.showLogoutButton = true,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AppBar(
|
||||
title: Text(title),
|
||||
backgroundColor: AppColors.rouge,
|
||||
actions: [
|
||||
if (showLogoutButton)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout, color: AppColors.blanc),
|
||||
onPressed: () async {
|
||||
// Afficher une boîte de dialogue de confirmation
|
||||
final shouldLogout = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Déconnexion'),
|
||||
content:
|
||||
const Text('Voulez-vous vraiment vous déconnecter ?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Déconnexion'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (shouldLogout == true) {
|
||||
// Déconnexion
|
||||
await Provider.of<LocalUserProvider>(context, listen: false)
|
||||
.signOut();
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pushReplacementNamed('/login');
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
if (actions != null) ...actions!,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:em2rp/models/role_model.dart';
|
||||
import 'package:em2rp/providers/local_user_provider.dart';
|
||||
|
||||
class PermissionGate extends StatelessWidget {
|
||||
final Widget child;
|
||||
final List<Permission> requiredPermissions;
|
||||
final bool requireAll;
|
||||
final Widget? fallback;
|
||||
|
||||
const PermissionGate({
|
||||
super.key,
|
||||
required this.child,
|
||||
required this.requiredPermissions,
|
||||
this.requireAll = true,
|
||||
this.fallback,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<LocalUserProvider>(
|
||||
builder: (context, localUserProvider, _) {
|
||||
final currentUser = localUserProvider.currentUser;
|
||||
if (currentUser == null) {
|
||||
return fallback ?? const SizedBox.shrink();
|
||||
}
|
||||
|
||||
final userRole = Roles.fromString(currentUser.role);
|
||||
final hasPermission = requireAll
|
||||
? userRole.hasAllPermissions(requiredPermissions)
|
||||
: userRole.hasAnyPermission(requiredPermissions);
|
||||
|
||||
if (hasPermission) {
|
||||
return child;
|
||||
}
|
||||
|
||||
return fallback ?? const SizedBox.shrink();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user