flutter_policy_engine 2.0.0
flutter_policy_engine: ^2.0.0 copied to clipboard
A lightweight, extensible policy engine for Flutter enabling RBAC and basic ABAC with Clean Architecture, injectable logger, and persistent storage.
import 'package:flutter/material.dart';
import 'package:flutter_policy_engine_example/policy_engine_demo.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Policy Engine v2 Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Policy Engine v2'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
const Icon(Icons.security, size: 64, color: Colors.blue),
const SizedBox(height: 16),
Text(
'Flutter Policy Engine',
style: Theme.of(context).textTheme.headlineMedium,
textAlign: TextAlign.center,
),
const SizedBox(height: 4),
Text(
'v2 — Clean Architecture, RBAC + ABAC',
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(color: Colors.grey),
textAlign: TextAlign.center,
),
],
),
),
),
const SizedBox(height: 24),
_DemoButton(
label: 'Basic RBAC Demo',
subtitle: 'Inline policy map, PolicyGate widget',
icon: Icons.play_arrow,
color: Colors.blue,
onTap: () => Navigator.push(
context,
MaterialPageRoute<void>(
builder: (_) => const PolicyEngineDemo(),
),
),
),
const SizedBox(height: 12),
_DemoButton(
label: 'Role Management Demo',
subtitle: 'Add / update / remove roles at runtime',
icon: Icons.admin_panel_settings,
color: Colors.orange,
onTap: () => Navigator.push(
context,
MaterialPageRoute<void>(
builder: (_) => const RoleManagementDemo(),
),
),
),
const SizedBox(height: 12),
_DemoButton(
label: 'JSON Assets Demo',
subtitle: 'Load policies from a bundled JSON asset',
icon: Icons.file_copy,
color: Colors.teal,
onTap: () => Navigator.push(
context,
MaterialPageRoute<void>(
builder: (_) => const JsonAssetsDemo(),
),
),
),
],
),
),
);
}
}
class _DemoButton extends StatelessWidget {
const _DemoButton({
required this.label,
required this.subtitle,
required this.icon,
required this.color,
required this.onTap,
});
final String label;
final String subtitle;
final IconData icon;
final Color color;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onTap,
style: ElevatedButton.styleFrom(
backgroundColor: color,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
alignment: Alignment.centerLeft,
),
child: Row(
children: [
Icon(icon, size: 28),
const SizedBox(width: 16),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
Text(
subtitle,
style: const TextStyle(fontSize: 12, color: Colors.white70),
),
],
),
],
),
);
}
}