flutter_facelytical 0.0.1
flutter_facelytical: ^0.0.1 copied to clipboard
A powerful Flutter plugin for real-time face detection and capture using native Android (Jetpack Compose + CameraX) and iOS (SwiftUI + AVFoundation) implementations.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_facelytical/flutter_facelytical.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _platformVersion = 'Unknown';
bool _hasCameraPermission = false;
String? _capturedImageBase64;
final _flutterFacelyticalPlugin = FlutterFacelytical();
@override
void initState() {
super.initState();
initPlatformState();
checkCameraPermission();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion =
await _flutterFacelyticalPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
// Check if camera permission is granted
Future<void> checkCameraPermission() async {
try {
final hasPermission =
await _flutterFacelyticalPlugin.hasCameraPermission();
setState(() {
_hasCameraPermission = hasPermission;
});
} on PlatformException catch (e) {
print('Error checking camera permission: ${e.message}');
}
}
// Request camera permission
Future<void> requestCameraPermission() async {
try {
final granted = await _flutterFacelyticalPlugin.requestCameraPermission();
setState(() {
_hasCameraPermission = granted;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
granted
? 'Camera permission granted!'
: 'Camera permission denied.',
),
duration: const Duration(seconds: 2),
),
);
} on PlatformException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error requesting permission: ${e.message}'),
duration: const Duration(seconds: 2),
),
);
}
}
// Present face detection view
Future<void> detectFace() async {
try {
final imageBase64 =
await _flutterFacelyticalPlugin.presentFaceDetection();
print("BASE64STRING: $imageBase64");
setState(() {
_capturedImageBase64 =
Platform.isAndroid
? imageBase64?.replaceAll('\n', '')
: imageBase64;
});
} on PlatformException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error detecting face: ${e.message}'),
duration: const Duration(seconds: 2),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('FaceLytical Example'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Platform info
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Device Information',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text('Platform: $_platformVersion'),
Text(
'Camera Permission: ${_hasCameraPermission ? "Granted" : "Not Granted"}',
),
],
),
),
),
const SizedBox(height: 20),
// Camera permissions section
const Text(
'Camera Permissions',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: checkCameraPermission,
child: const Text('Check Permission'),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton(
onPressed: requestCameraPermission,
child: const Text('Request Permission'),
),
),
],
),
const SizedBox(height: 20),
// Face detection section
const Text(
'Face Detection',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: detectFace,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text('Detect Face'),
),
const SizedBox(height: 20),
// Captured image display
const Text(
'Captured Image',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Container(
height: 300,
decoration: BoxDecoration(
color: Colors.grey[200],
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child:
_capturedImageBase64 != null
? ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.memory(
base64Decode(_capturedImageBase64!),
fit: BoxFit.contain,
),
)
: const Center(child: Text('No image captured yet')),
),
],
),
),
),
);
}
}