Skip to content

Commit 70aa451

Browse files
feat(policy): integrate PolicyManager into example app and enhance UI for policy loading
1 parent 1c5d4d5 commit 70aa451

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

example/lib/main.dart

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import 'dart:developer';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:flutter_policy_engine/flutter_policy_engine.dart';
25

36
void main() {
47
runApp(const MyApp());
@@ -9,61 +12,77 @@ class MyApp extends StatelessWidget {
912
@override
1013
Widget build(BuildContext context) {
1114
return MaterialApp(
12-
title: 'Flutter Demo',
15+
title: 'Policy Engine Demo',
16+
debugShowCheckedModeBanner: false,
1317
theme: ThemeData(
1418
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
1519
useMaterial3: true,
1620
),
17-
home: const MyHomePage(title: 'Flutter Demo Home Page'),
21+
home: const PolicyEngineDemo(),
1822
);
1923
}
2024
}
2125

22-
class MyHomePage extends StatefulWidget {
23-
const MyHomePage({super.key, required this.title});
24-
25-
final String title;
26+
class PolicyEngineDemo extends StatefulWidget {
27+
const PolicyEngineDemo({super.key});
2628

2729
@override
28-
State<MyHomePage> createState() => _MyHomePageState();
30+
State<StatefulWidget> createState() {
31+
return _PolicyEngineDemoState();
32+
}
2933
}
3034

31-
class _MyHomePageState extends State<MyHomePage> {
32-
int _counter = 0;
35+
class _PolicyEngineDemoState extends State<PolicyEngineDemo> {
36+
late PolicyManager policyManager;
37+
bool _isInitialized = false;
3338

34-
void _incrementCounter() {
35-
setState(() {
36-
_counter++;
37-
});
39+
@override
40+
void initState() {
41+
super.initState();
42+
_initializePolicyManager();
43+
}
44+
45+
Future<void> _initializePolicyManager() async {
46+
policyManager = PolicyManager();
47+
final policies = {
48+
"admin": ["LoginPage", "Dashboard", "UserManagement", "Settings"],
49+
"user": ["LoginPage", "Dashboard"],
50+
"guest": ["LoginPage"]
51+
};
52+
try {
53+
await policyManager.initialize(policies);
54+
setState(() {
55+
_isInitialized = true;
56+
});
57+
} catch (e) {
58+
log(e.toString());
59+
setState(() {
60+
_isInitialized = false;
61+
});
62+
}
3863
}
3964

4065
@override
4166
Widget build(BuildContext context) {
42-
return Scaffold(
43-
appBar: AppBar(
44-
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
45-
title: Text(widget.title),
46-
),
47-
body: Center(
48-
child: Column(
49-
//
50-
mainAxisAlignment: MainAxisAlignment.center,
51-
children: <Widget>[
52-
const Text(
53-
'You have pushed the button this many times:',
54-
),
55-
Text(
56-
'$_counter',
57-
style: Theme.of(context).textTheme.headlineMedium,
58-
),
59-
],
67+
if (!_isInitialized) {
68+
return Scaffold(
69+
appBar: AppBar(
70+
title: const Text('Policy Engine Demo'),
6071
),
61-
),
62-
floatingActionButton: FloatingActionButton(
63-
onPressed: _incrementCounter,
64-
tooltip: 'Increment',
65-
child: const Icon(Icons.add),
66-
), // This trailing comma makes auto-formatting nicer for build methods.
72+
body: const Center(
73+
child: Column(
74+
mainAxisAlignment: MainAxisAlignment.center,
75+
children: [
76+
CircularProgressIndicator(),
77+
SizedBox(height: 16),
78+
Text('Loading policies...'),
79+
],
80+
),
81+
),
82+
);
83+
}
84+
return const Center(
85+
child: Text('Policies loaded'),
6786
);
6887
}
6988
}

lib/flutter_policy_engine.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
library flutter_policy_engine;
22

3-
/// A Calculator.
4-
class Calculator {
5-
/// Returns [value] plus 1.
6-
int addOne(int value) => value + 1;
7-
}
3+
export 'src/core/policy_manager.dart';

0 commit comments

Comments
 (0)