/
main.dart
427 lines (385 loc) · 16.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:idwise_flutter_sdk/idwise_flutter.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
import 'my_store.dart'; // Import your store class
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => MyStore(),
child: MaterialApp(
title: 'IDWise Flutter Application',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.indigo,
),
home: const MyHomePage(title: 'IDWise Flutter Demo'),
));
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const methodChannelName = "com.idwise.fluttersampleproject/idwise";
static const platformChannel = MethodChannel(methodChannelName);
// final MyStore myStore = MyStore();
static const JOURNEY_ID = "JOURNEY_ID";
static const STEP_ID_DOCUMENT = '10';
static const STEP_SELFIE = '20';
static const IDWISE_CLIENT_KEY = "CLIENT-KEY"; // Provided by IDWise
static const JOURNEY_DEFINITION_ID = "FLOW_ID"; // Provided by IDWise
static const LOCALE = "en";
String? _imageBytes;
late IDWiseSDKJourneyCallbacks _journeyCallbacks;
late IDWiseSDKStepCallbacks _stepCallbacks;
@override
void initState() {
super.initState();
setupCallbacks();
startResumeJourney();
}
void setupCallbacks() {
_journeyCallbacks = IDWiseSDKJourneyCallbacks(
onJourneyStarted: (dynamic journeyInfo) {
print("Method: onJourneyStarted, $journeyInfo");
context.read<MyStore>().setJourneyStatus(true);
saveJourneyId(journeyInfo["journeyId"]);
context.read<MyStore>().setJourneyId(journeyInfo["journeyId"]);
getJourneySummary();
},
onJourneyCompleted: (dynamic journeyInfo) =>
print("onJourneyCompleted: $journeyInfo"),
onJourneyCancelled: (dynamic journeyInfo) =>
print("onJourneyCancelled: $journeyInfo"),
onJourneyResumed: (dynamic journeyInfo) {
print("Method: onJourneyResumed, ${journeyInfo["journeyId"]}");
context.read<MyStore>().setJourneyStatus(true);
context.read<MyStore>().setJourneyId(journeyInfo["journeyId"]);
getJourneySummary();
},
onError: (dynamic error) => print("onError $error"));
_stepCallbacks = IDWiseSDKStepCallbacks(onStepCaptured: (dynamic response) {
print("Method: onStepCaptured, ${response["stepId"]}");
print("Method: capturedImage, ${response["capturedImage"]}");
/*setState(() {
_imageBytes = response["capturedImage"];
});*/
}, onStepResult: (dynamic response) async {
print("Method: onStepResult, $response");
getJourneySummary();
}, onStepCancelled: (dynamic response) async {
print("Method: onStepCancelled, $response");
}, onStepSkipped: (dynamic response) async {
print("Method: onStepSkipped, $response");
});
}
// bool _isButtonEnabled = true;
Future<void> clearSaved() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
}
Future<void> saveJourneyId(String journeyId) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(JOURNEY_ID, journeyId);
}
Future<String?> retrieveJourneyId() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(JOURNEY_ID);
}
Future<void> _navigateStep(String stepId) async {
print("StepId: $stepId");
IDWise.startStep(stepId);
}
void _skipStep(String stepId) async {
print("StepId: $stepId");
IDWise.skipStep(stepId);
}
Future<void> unloadSDK() async {
print("unloadSDK");
IDWise.unloadSDK();
}
Future<void> startResumeJourney() async {
try {
context.read<MyStore>().setJourneyStatus(false);
print("Initializing SDK");
initializeSDK();
String? journeyId = await retrieveJourneyId();
if (journeyId == null) {
print("Starting new journey...");
startDynamicJourney();
} else {
print("Resuming journey...");
resumeDynamicJourney(journeyId);
}
} on PlatformException catch (e) {
print("Failed : '${e.message}'.");
}
print("End");
}
Future<void> initializeSDK() async {
try {
IDWise.initialize(IDWISE_CLIENT_KEY, IDWiseSDKTheme.DARK,
onError: (error) {
print("onError in _idwiseFlutterPlugin: $error");
});
} on PlatformException catch (e) {
print("Failed : '${e.message}'.");
}
}
Future<void> startDynamicJourney() async {
try {
final referenceNo = 'idwise_test_' + const Uuid().v4();
IDWise.startDynamicJourney(JOURNEY_DEFINITION_ID, referenceNo, LOCALE,
_journeyCallbacks, _stepCallbacks);
} on PlatformException catch (e) {
print("Failed : '${e.message}'.");
}
}
Future<void> resumeDynamicJourney(String journeyId) async {
try {
IDWise.resumeDynamicJourney(JOURNEY_DEFINITION_ID, journeyId, LOCALE,
_journeyCallbacks, _stepCallbacks);
} on PlatformException catch (e) {
print("Failed : '${e.message}'.");
}
}
Future<void> getJourneySummary() async {
try {
IDWise.getJourneySummary(onJourneySummary: (dynamic response) {
handleJourneySummary(response);
});
} on PlatformException catch (e) {
print("Failed : '${e.message}'.");
}
}
Future<void> handleJourneySummary(dynamic response) async {
try {
print("handleJourneySummary $response");
print("JourneySummary - Details: ${response["summary"].toString()}");
print("JourneySummary - Error: ${response["error"].toString()}");
bool isCompleted = response["summary"]["is_completed"];
print("JourneySummary - Completed: $isCompleted");
print(
"JourneySummary - interim_rules: ${response["summary"]["result"]["interim_rule_details"].toString()}");
if (isCompleted) {
context.read<MyStore>().setJourneyCompleted(true);
clearSaved();
}
} catch (e) {
print("Exception : JourneySummary: $e");
}
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
body: SafeArea(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Column(
children: const [
Padding(
padding: EdgeInsets.only(top: 16.0),
child: Text(
"Welcome to",
style:
TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
)),
Padding(
padding: EdgeInsets.only(top: 16.0),
child: Text("Verification Journey",
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold))),
Padding(
padding: EdgeInsets.only(top: 16.0),
child: Text("Please take some time to verify your identity",
style: TextStyle(
fontSize: 16, fontWeight: FontWeight.bold)))
],
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<MyStore>(
builder: (context, store, child) {
return FractionallySizedBox(
widthFactor: 0.5,
child: SizedBox(
height: 45,
child: ElevatedButton(
child: const Text('ID Document',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold)),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
10.0), // Set the BorderRadius
),
backgroundColor: store.isJourneyStarted
? const Color(0xff2A4CD0)
: const Color(0xff6C6C70),
textStyle:
const TextStyle(color: Colors.white)),
onPressed: store.isJourneyStarted
? () {
_navigateStep(STEP_ID_DOCUMENT);
}
: null)));
},
),
const Padding(padding: EdgeInsets.all(5.0)),
Consumer<MyStore>(
builder: (context, store, child) {
return FractionallySizedBox(
widthFactor: 0.5,
child: SizedBox(
height: 45,
child: ElevatedButton(
child: const Text('Selfie',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold)),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
10.0), // Set the BorderRadius
),
backgroundColor: store.isJourneyStarted
? const Color(0xff2A4CD0)
: const Color(0xff6C6C70),
textStyle:
const TextStyle(color: Colors.white)),
onPressed: store.isJourneyStarted
? () {
_skipStep(STEP_SELFIE);
}
: null)));
},
),
SizedBox(
height: 200,
child: _imageBytes != null
? Image.memory(base64Decode(_imageBytes!))
: Text("")),
Consumer<MyStore>(
builder: (context, model, child) {
return Visibility(
visible: model.isJourneyCompleted,
//_isButtonEnabled
child: Padding(
padding: const EdgeInsets.only(
top: 50.0, left: 15.0, right: 15.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.check,
color: Color(0xff248A3D), size: 36.0),
Padding(
padding: EdgeInsets.only(top: 10.0),
child: Text(
"Congratulations! Your verification has been completed.",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold)))
])),
);
},
),
],
)),
Consumer<MyStore>(
builder: (context, model, child) {
return Visibility(
visible: model.isJourneyStarted, //_isButtonEnabled
child: Padding(
padding: const EdgeInsets.only(bottom: 15.0),
child: Text('Journey Id: ' + model.journeyId,
style: const TextStyle(
fontSize: 16, fontWeight: FontWeight.bold))),
);
},
),
Padding(
padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 10.0),
child: FractionallySizedBox(
widthFactor: 1,
child: SizedBox(
height: 55,
child: ElevatedButton(
child: const Text('Test New Journey',
style: TextStyle(
color: Color(0xff2A4CD0),
fontSize: 18,
fontWeight: FontWeight.bold)),
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
10.0), // Set the BorderRadius
),
side: const BorderSide(
width: 1.0, color: Color(0xff2A4CD0)),
backgroundColor: Colors.white,
textStyle:
const TextStyle(color: Colors.black)),
onPressed: () {
clearSaved();
unloadSDK();
startResumeJourney();
}))))
])),
);
}
}