-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.dart
193 lines (185 loc) · 7.1 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:secure_application/secure_application.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool failedAuth = false;
double blurr = 20;
double opacity = 0.6;
StreamSubscription<bool>? subLock;
List<String> history = [];
@override
void initState() {
super.initState();
}
@override
void dispose() {
subLock?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
var width = MediaQuery.of(context).size.width * 0.8;
return MaterialApp(
home: SecureApplication(
nativeRemoveDelay: 1000,
onNeedUnlock: (secure) async {
print(
'need unlock maybe use biometric to confirm and then sercure.unlock() or you can use the lockedBuilder');
// var authResult = authMyUser();
// if (authResul) {
// secure.unlock();
// return SecureApplicationAuthenticationStatus.SUCCESS;
//}
// else {
// return SecureApplicationAuthenticationStatus.FAILED;
//}
return null;
},
onAuthenticationFailed: () async {
// clean you data
setState(() {
failedAuth = true;
});
print('auth failed');
},
onAuthenticationSucceed: () async {
// clean you data
setState(() {
failedAuth = false;
});
print('auth success');
},
child: Builder(builder: (context) {
if (subLock == null)
subLock = SecureApplicationProvider.of(context, listen: false)
?.lockEvents
.listen((s) => history.add(
'${DateTime.now().toIso8601String()} - ${s ? 'locked' : 'unlocked'}'));
return SecureGate(
blurr: blurr,
opacity: opacity,
lockedBuilder: (context, secureNotifier) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Text('UNLOCK'),
onPressed: () => secureNotifier?.authSuccess(unlock: true),
),
ElevatedButton(
child: Text('FAIL AUTHENTICATION'),
onPressed: () => secureNotifier?.authFailed(unlock: true),
),
],
)),
child: Scaffold(
appBar: AppBar(
title: const Text('Secure Window Example'),
),
body: Center(
child: Builder(builder: (context) {
var valueNotifier = SecureApplicationProvider.of(context);
if (valueNotifier == null)
throw new Exception(
'Unable to find secure application context');
return ListView(
children: <Widget>[
Text('This is secure content'),
ValueListenableBuilder<SecureApplicationState>(
valueListenable: valueNotifier,
builder: (context, state, _) => state.secured
? Column(
children: <Widget>[
ElevatedButton(
onPressed: () => valueNotifier.open(),
child: Text('Open app'),
),
state.paused
? ElevatedButton(
onPressed: () =>
valueNotifier.unpause(),
child: Text('resume security'),
)
: ElevatedButton(
onPressed: () =>
valueNotifier.pause(),
child: Text('pause security'),
),
],
)
: ElevatedButton(
onPressed: () => valueNotifier.secure(),
child: Text('Secure app'),
),
),
failedAuth
? Text(
'Auth failed we cleaned sensitive data',
style: TextStyle(color: Colors.red),
)
: Text(
'Auth success',
style: TextStyle(color: Colors.green),
),
FlutterLogo(
size: width,
),
StreamBuilder(
stream: valueNotifier.authenticationEvents,
builder: (context, snapshot) =>
Text('Last auth status is: ${snapshot.data}'),
),
ElevatedButton(
onPressed: () => valueNotifier.lock(),
child: Text('manually lock'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Text('Blurr:'),
Expanded(
child: Slider(
value: blurr,
min: 0,
max: 100,
onChanged: (v) => setState(() => blurr = v)),
),
Text(blurr.floor().toString()),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Text('opacity:'),
Expanded(
child: Slider(
value: opacity,
min: 0,
max: 1,
onChanged: (v) =>
setState(() => opacity = v)),
),
Text((opacity * 100).floor().toString() + "%"),
],
),
),
...history.map<Widget>((h) => Text(h)).toList(),
],
);
}),
),
),
);
}),
),
);
}
}