ui_guard 1.0.2 copy "ui_guard: ^1.0.2" to clipboard
ui_guard: ^1.0.2 copied to clipboard

Easily manage role, permission, and condition-based access control in Flutter UIs — entirely in Dart.

UI Guard Package

Pub build Star on Github Flutter Website

Widgets that make role, permission, and condition-based UI control simple, scalable, and secure.
Built entirely in Dart to help you build smarter, access-aware Flutter apps.

✨ ui_guard works seamlessly with any role management logic or state management approach.

🔐 Why use ui_guard? #

In many apps, you need to control access to certain parts of your UI:

  • Show settings only to admins
  • Render upgrade buttons for guests
  • Show/hide widgets based on subscription level

ui_guard lets you do this easily and declaratively — using only Dart.


✨ Features #

  • ✅ Guard widgets or entire screens based on roles
  • 🧩 Combine roles, permissions, and runtime conditions
  • 🧪 Developer override mode for UI testing
  • 🔄 Easily update roles at runtime
  • 📦 Pure Dart — no platform dependencies
  • ♻️ Works with any state management

📦 Installation #

Add this to your pubspec.yaml:

dependencies:
  ui_guard: ^1.0.0

🧠 Core API #

A simple class to store and manage the current user's roles.

🔹 Guard

A simple class to store and manage the current user's roles.

final guard = Guard();
guard.setUserRoles(['admin']); // Set roles for current user

print(guard.currentRoles); // ['admin']

🔹 AccessGuard

Renders content conditionally based on required roles.

AccessGuard(
  guard: guard,
  requiredRoles: ['admin'],
  builder: (_) => const Text('Admin Panel'),
  fallbackBuilder: (_) => const Text('Access Denied'),
);

🔹 RoleBasedView

Use when you want to show/hide a single widget inline.

AccessGuard(
  guard: guard,
  requiredRoles: ['admin'],
  builder: (_) => const Text('Admin Panel'),
  fallbackBuilder: (_) => const Text('Access Denied'),
);

🔹 RoleGuard

Utility class with common access logic:

RoleGuard.hasAnyRole(['admin'], ['admin', 'user']); // true
RoleGuard.hasAllRoles(['admin', 'editor'], ['admin']); // true

🧪 Developer Override Mode

Bypass all restrictions during development or testing:

class GuardConfig {
  static bool developerOverrideEnabled = true; // Use in dev only
}

🧮 Combined Access Conditions

Create advanced rules using roles, permissions, and runtime checks:

CombinedGuard(
  guard: guard,
  requiredRoles: ['manager'],
  requiredPermissions: ['edit_team'],
  condition: () => organization.isInternalMode,
  builder: (_) => const TeamEditor(),
  fallbackBuilder: (_) => const Text('Access Restricted'),
);

📱 Example App #

Explore the full working example in the /example directory.

🧩 Use Cases #

Here are some common scenarios where ui_guard is useful:

Use Case Example
Admin-only screens requiredRoles: ['admin']
Feature restrictions Hide paid features from free users
Auth state UI Show "Login" or "Logout" buttons based on roles
Nested permissions Show moderator tools for ['moderator', 'admin'] roles
Read-only vs edit access Conditionally render buttons or fields
Subscription tiers Control access with ['free', 'premium', 'pro'] roles
Combined logic Use roles + permissions + runtime conditions
Developer override Skip restrictions in development or test

💬 Contributing #

Contributions are welcome!

To contribute:

  1. Fork the repository
  2. Create a new branch
  3. Commit your changes
  4. Submit a pull request

🛠️ Dart SDK Version #

This package requires Dart SDK version >=2.14.

Please ensure your Flutter and Dart versions meet this requirement.


👤 Maintainers #

  • MD. TANVIRUL ISLAM
9
likes
150
points
44
downloads

Publisher

verified publishertanvirdev.com

Weekly Downloads

Easily manage role, permission, and condition-based access control in Flutter UIs — entirely in Dart.

Homepage
Repository (GitHub)
View/report issues

Topics

#flutter #plugin #user #role-based #access-control

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on ui_guard