permission_policy 1.0.2 permission_policy: ^1.0.2 copied to clipboard
Permission policy helps you manage permissions in your Flutter application.
Role and Permissions for Flutter #
Permission policy helps you manage role and permissions in your Flutter application. It works on Android, iOS, macOS, linux, windows and web.
Usage #
Simple to use #
// Add roles and permissions to the permission policy
RoleAndPermissions roleAndPermissions = {
"Admin": ['admin'],
"Sales Manager": ['view_revenue', 'view_apps'],
"Developer Manager": ['view_apps'],
"Marketing": ['view_media'],
"Project Manager": ["edit_projects"]
};
PermissionPolicy.instance.addRoles(roleAndPermissions);
// Get the users current role
await PermissionPolicy.getRole();
// Check if a user has a role
await PermissionPolicy.hasRole('Admin');
// Check if a user has a permission
await PermissionPolicy.hasPermission('view_revenue');
// Give the user a role
await PermissionPolicy.giveRole("Admin");
// Remove a role from the user
await PermissionPolicy.removeRole();
Widgets #
// [UserRole] This widget will show the users current role
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Permission Policy")),
body: SafeArea(
child: UserRole() // This widget will show the users current role
)
);
}
// [UserPermissions] This widget will show the users current permissions
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Permission Policy")),
body: SafeArea(
child: UserPermissions() // This widget will show the users current permissions
)
);
}
// [RoleSelector] This widget will allow the user to select a role
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Permission Policy")),
body: SafeArea(
child: RoleSelector(onUpdate: () {
// onUpdate is called after the user selects a role
}),
)
);
}
// [RoleView] This widget will display a widget based on the users current role
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Permission Policy")),
body: SafeArea(
child: RoleView(
widgetMap: () => {
"Admin": Text("The Admin UI"),
"Subscriber": Text("The Subscriber UI"),
"User": Text("The User UI")
},
defaultView: () => Text("The default UI")), // if the user does not have a role, the defaultView will be shown
)
);
}
// [PermissionView] This widget will show a widget if the user has the correct permissions
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Permission Policy")),
body: SafeArea(
child: PermissionView(
child: Text("Join the Pro plan"),
permissions: ['can_subscribe']
),
)
);
}
Features #
- ✅ Add roles and permissions to your Flutter application
- ✅ Check if a user has a role
- ✅ Check if a user has a permission
- ✅ Give a user a role
- ✅ Remove a role from a user
- ✅ Widgets to show a users current role and permissions
Getting started #
Installation #
Add the following to your pubspec.yaml
file:
dependencies:
permission_policy: ^1.0.2
or with Dart:
dart pub add permission_policy
How to use #
The package is very simple to use. You can add roles and permissions to the permission policy and then check if a user has a role or permission.
Add roles and permissions #
import 'package:flutter/material.dart';
import 'package:nylo_support/helpers/extensions.dart';
import 'package:permission_policy/permission_policy.dart';
void main() {
// Add roles and permissions to the permission policy
RoleAndPermissions roleAndPermissions = {
"Admin": ['admin'],
"Subscriber": ['can_unsubscribe', 'view_exclusive_content'],
"User": ['can_subcribe', 'view_content'],
};
PermissionPolicy.instance.addRoles(roleAndPermissions);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Permission Policy")),
body: SafeArea(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 8),
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ListView(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: [
Text("Your role").fontWeightBold(),
UserRole(), // This widget will show the users current role
Text("Your Permissions").fontWeightBold(),
UserPermissions(), // This widget will show the users current permissions
],
),
Expanded(
child: RoleSelector(onUpdate: () {
setState(() {});
}),
),
RoleView(
widgetMap: () => {
"Admin": Text("The Admin UI"),
"Subscriber": Text("The Subscriber UI"),
"User": Text("The User UI")
}),
PermissionView(
child: Text("Join the Pro plan"),
permissions: ['can_subscribe']),
PermissionView(
child: Text("Unsubscribe from the Pro plan"),
permissions: ['can_unsubscribe']),
MaterialButton(
onPressed: () async {
await PermissionPolicy.removeRole();
setState(() {});
},
child: Text("Clear Roles"),
)
],
),
),
),
);
}
}
If the user has the role of Admin, they will be able to see any PermissionView
widgets.
Try the example app to see how it works.
Changelog #
Please see CHANGELOG for more information what has changed recently.
Social #
Licence #
The MIT License (MIT). Please view the License File for more information.