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'),
);

โฑ๏ธ Timed Access Control

Use TimedAccessGuard to control UI visibility based on time. Ideal for:

  • ๐ŸŽ Limited-time offers & flash sales
  • ๐Ÿงช Beta or trial feature access
  • ๐Ÿ”ง Maintenance or downtime notices
  • ๐Ÿ“… Event-specific content
  • ๐Ÿ›๏ธ Daily/weekly deals
  • ๐Ÿ“ข Time-based announcements
  • ๐Ÿข Business-hour-only features
TimedAccessGuard(
  start: DateTime(2025, 7, 18, 9),
  end: DateTime(2025, 7, 18, 13),
  checkInterval: Duration(seconds: 1),
  onTimeUpdate: (remaining) {
    debugPrint("โฑ๏ธ Time left: ${remaining.inSeconds}s");
  },
  builder: (_) => PromoBanner(), // Active content
  fallbackBuilder: (_) => SizedBox.shrink(), // Hidden or fallback
),

๐Ÿ“ฑ 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
Time-based access Display banners or UI only within a defined time range TimedAccessGuard

๐Ÿ’ฌ 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.


โ˜• Support My Work

If you find ui_guard helpful, consider supporting me!

Buy Me a Coffee

Prefer mobile? Scan the QR code below to support me directly:

Buy Me a Coffee QR Code

๐Ÿ‘ค Maintainers

  • MD. TANVIRUL ISLAM

Libraries

ui_guard