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:
- Fork the repository
- Create a new branch
- Commit your changes
- 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!
Prefer mobile? Scan the QR code below to support me directly:
๐ค Maintainers
- MD. TANVIRUL ISLAM