flutter_liveness_detection 1.0.1
flutter_liveness_detection: ^1.0.1 copied to clipboard
A free and secure Flutter face liveness detection plugin for biometric authentication, KYC verification, face recognition, and anti-spoof real-time motion detection.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_liveness_detection/flutter_liveness_detection.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:camera/camera.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await requestCameraPermission();
runApp(const MyApp());
}
Future<void> requestCameraPermission() async {
final status = await Permission.camera.request();
if (!status.isGranted) {
runApp(const PermissionDeniedApp());
}
}
class PermissionDeniedApp extends StatelessWidget {
const PermissionDeniedApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text("Permission Denied")),
body: Center(
child: AlertDialog(
title: const Text("Permission Denied"),
content: const Text("Camera access is required for verification."),
actions: [
TextButton(
onPressed: () => SystemNavigator.pop(),
child: const Text("OK"),
),
],
),
),
),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Material App',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final FlutterLivenessDetection _livenessDetection =
FlutterLivenessDetection();
File? imageFile;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.blueAccent,
toolbarHeight: 70,
centerTitle: true,
title: const Text('Identify'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Please click the button below to start verification',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20),
),
const SizedBox(height: 30),
(imageFile != null)
? SizedBox(
height: 200,
width: 150,
child: Image.file(imageFile!),
)
: const SizedBox(),
ElevatedButton(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
foregroundColor: Colors.black,
backgroundColor: Colors.blueAccent,
),
onPressed: () async {
final List<CameraDescription> cameras =
await availableCameras();
if (cameras.isNotEmpty) {
List<Expression> expressions = [
Expression.smile, // Ask user to smile
Expression.eyeblink, // Ask user to blink
// Expression.leftPose, // Turn head left
// Expression.rightPose, // Turn head right
];
// Ignore the analyzer warning for the next line only
// ignore: use_build_context_synchronously
final XFile? result = await _livenessDetection.start(context: context, expressions: expressions);
if (result != null) {
setState(() {
imageFile = File(result.path);
});
// Ignore the analyzer warning for the next line only
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Verification Successful!')),
);
}
} else {
// Ignore the analyzer warning for the next line only
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Camera not active!')),
);
}
},
child: const Text(
'Verify Now',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
),
],
),
),
);
}
}