@@ -32,44 +32,54 @@ class MyHomePage extends StatefulWidget {
3232}
3333
3434class _MyHomePageState extends State <MyHomePage > {
35- FlutterLiveness ? liveness;
36- LivenessResult ? result;
35+ FlutterLiveness ? _liveness;
36+
37+ LivenessResult ? _result;
38+
39+ bool _isLoading = false ;
3740
3841 @override
3942 void initState () {
4043 super .initState ();
41- _init ();
44+ WidgetsBinding .instance.addPostFrameCallback ((_) {
45+ _createLiveness ();
46+ });
4247 }
4348
4449 @override
4550 void dispose () {
46- liveness ? .dispose ();
51+ _liveness ? .dispose ();
4752 super .dispose ();
4853 }
4954
50- Future <void > _init () async {
51- final l = await FlutterLiveness .create ();
52- setState (() => liveness = l);
55+ Future <void > _createLiveness () async {
56+ setState (() => _isLoading = true );
57+ _liveness = await FlutterLiveness .create ();
58+ setState (() => _isLoading = false );
5359 }
5460
5561 Future <void > _run () async {
62+ setState (() => _isLoading = true );
5663 // Replace with your face crop bytes (asset/file/camera)
5764 final bytes = await rootBundle.load ('assets/sample_face_2.jpg' );
5865 final img = imglib.decodeImage (bytes.buffer.asUint8List ());
59- if (img == null || liveness == null ) return ;
60- final res = await liveness! .analyze (img);
61- setState (() => result = res);
66+ if (img == null || _liveness == null ) return ;
67+ final result = await _liveness! .analyze (img);
68+ setState (() {
69+ _result = result;
70+ _isLoading = false ;
71+ });
6272 }
6373
6474 @override
6575 Widget build (BuildContext context) {
6676 final String message;
67- if (result != null ) {
77+ if (_result != null ) {
6878 message = [
69- 'Liveness: ${result !.isLive ? 'LIVE' : 'SPOOF' }' ,
70- 'Score: ${result !.score .toStringAsFixed (3 )}' ,
71- 'Laplacian: ${result !.laplacian .toStringAsFixed (1 )}' ,
72- 'Duration: ${result !.duration .inMilliseconds } ms' ,
79+ 'Liveness: ${_result !.isLive ? 'LIVE' : 'SPOOF' }' ,
80+ 'Score: ${_result !.score .toStringAsFixed (3 )}' ,
81+ 'Laplacian: ${_result !.laplacian .toStringAsFixed (1 )}' ,
82+ 'Duration: ${_result !.duration .inMilliseconds } ms' ,
7383 ].join ('\n ' );
7484 } else {
7585 message = 'Tap ▶ to analyze' ;
@@ -80,13 +90,15 @@ class _MyHomePageState extends State<MyHomePage> {
8090 title: const Text ('flutter_liveness demo' ),
8191 ),
8292 body: Center (
83- child: Text (
84- message,
85- textAlign: TextAlign .center,
86- ),
93+ child: _isLoading
94+ ? const CircularProgressIndicator ()
95+ : Text (
96+ message,
97+ textAlign: TextAlign .center,
98+ ),
8799 ),
88100 floatingActionButton: FloatingActionButton (
89- onPressed: _run,
101+ onPressed: _isLoading ? null : _run,
90102 child: const Icon (Icons .play_arrow),
91103 ),
92104 );
0 commit comments