1
+ import 'dart:developer' ;
2
+
1
3
import 'package:flutter/material.dart' ;
4
+ import 'package:flutter_policy_engine/flutter_policy_engine.dart' ;
2
5
3
6
void main () {
4
7
runApp (const MyApp ());
@@ -9,61 +12,77 @@ class MyApp extends StatelessWidget {
9
12
@override
10
13
Widget build (BuildContext context) {
11
14
return MaterialApp (
12
- title: 'Flutter Demo' ,
15
+ title: 'Policy Engine Demo' ,
16
+ debugShowCheckedModeBanner: false ,
13
17
theme: ThemeData (
14
18
colorScheme: ColorScheme .fromSeed (seedColor: Colors .deepPurple),
15
19
useMaterial3: true ,
16
20
),
17
- home: const MyHomePage (title : 'Flutter Demo Home Page' ),
21
+ home: const PolicyEngineDemo ( ),
18
22
);
19
23
}
20
24
}
21
25
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});
26
28
27
29
@override
28
- State <MyHomePage > createState () => _MyHomePageState ();
30
+ State <StatefulWidget > createState () {
31
+ return _PolicyEngineDemoState ();
32
+ }
29
33
}
30
34
31
- class _MyHomePageState extends State <MyHomePage > {
32
- int _counter = 0 ;
35
+ class _PolicyEngineDemoState extends State <PolicyEngineDemo > {
36
+ late PolicyManager policyManager;
37
+ bool _isInitialized = false ;
33
38
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
+ }
38
63
}
39
64
40
65
@override
41
66
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' ),
60
71
),
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' ),
67
86
);
68
87
}
69
88
}
0 commit comments