how to close the application on clicking cancel in local auth and also when maximum tries exceeds in flutter

1.8k views Asked by At

I'm new in flutter. I wanted to create an application with local biometrics I have used local auth and i need to have help with

  1. close the application on the click of cancel button in local_auth,
  2. close the application when maximum tries are done.
  3. pause the background untill authentication complete

my code is

import 'dart:async';
import 'package:LogInSignIn.dart';
import 'package:flutter/material.dart';
import 'package:cashhub/homescreen.dart';
import 'package:local_auth/local_auth.dart';
import 'package:flutter/services.dart';

void main() {
  setupLocator();
  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    home: new SplashScreen(),
    routes: <String, WidgetBuilder>{
      '/HomeScreen': (BuildContext context) => new LogInSignIn(),
      

    },
  ));
}

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  //final LocalAuthenticationService _localAuth = locator<LocalAuthenticationService>();
  final LocalAuthentication auth = LocalAuthentication();
  bool _canCheckBiometrics;
  List<BiometricType> _availableBiometrics;
  String _authorized = 'Not Authorized';
  bool _isAuthenticating = false;

  startTime() async {
    var _duration = new Duration(seconds: 4);
    return new Timer(_duration, navigationPage);
  }




  Future<void> _authenticate() async {
    bool authenticated = false;
    try {
      setState(() {
        _isAuthenticating = true;
        _authorized = 'Authenticating';
      });
      authenticated = await auth.authenticateWithBiometrics(
          localizedReason: 'Scan your fingerprint to authenticate',
          useErrorDialogs: true,
          stickyAuth: true);
      setState(() {
        _isAuthenticating = false;
        _authorized = 'Authenticating';
      });
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    final String message = authenticated ? 'Authorized' : 'Not Authorized';
    // if( message == "Not Authorized"){
    //   SystemNavigator.pop();
    // }
    setState(() {
      _authorized = message;
    });

  }

  void navigationPage() {
    Navigator.of(context).pushReplacementNamed('/HomeScreen');
  }
  @override
  void initState() {
    _authenticate();
    //autho();
    super.initState();

    startTime();

  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Image.asset('assets/splashlogo.png',
        ),
      ),
    );
  }
}

anyone please help me with this 3 queries..

1

There are 1 answers

1
skahandz On

you can close the app with cancel click like this

setState(() {
  if (isAuthorized) {
    _authorizedOrNot = "Authorized";
  } else {
    _authorizedOrNot = "Not Authorized";
    exit(0);
  }
});

just so you know exit(0) need to impot dart:io