flutter_tflite_mobile 3.0.0
flutter_tflite_mobile: ^3.0.0 copied to clipboard
A Flutter package for face detection, liveness verification, and document scanning using TensorFlow Lite and Google ML Kit. Supports identity verification with face capture sequences and DNI document [...]
example/example.dart
// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_tflite_mobile/flutter_tflite_mobile.dart';
import 'package:api_rest_flutter_mobile/api_rest.dart';
/// Example application demonstrating flutter_tflite_mobile usage
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter TFLite Mobile Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter TFLite Mobile'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FaceDetectionExample(),
),
);
},
icon: const Icon(Icons.face),
label: const Text('Face Detection'),
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DocumentScanExample(),
),
);
},
icon: const Icon(Icons.document_scanner),
label: const Text('Document Scanning'),
),
],
),
),
);
}
}
/// Example of Face Detection usage
class FaceDetectionExample extends StatelessWidget {
const FaceDetectionExample({super.key});
@override
Widget build(BuildContext context) {
// Note: You need to provide your own ApiRest instance
// configured with your backend endpoints
return BlocProvider(
create: (context) => ReconocimientoBloc(
apiRest: _createApiRest(),
)..add(OnInitReconocimiento()),
child: BlocListener<ReconocimientoBloc, ReconocimientoState>(
listener: (context, state) {
if (state.error.isNotEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: ${state.error}')),
);
}
if (state.accion == ReconocimientoBloc.onUploadImageSelfie) {
if (state.responseValdiacionSelfie.isNotEmpty) {
print('Selfie validation response: ${state.responseValdiacionSelfie}');
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Face validation completed!')),
);
}
}
},
child: const FaceDetectorView(),
),
);
}
}
/// Example of Document Scanning usage
class DocumentScanExample extends StatelessWidget {
const DocumentScanExample({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => ReconocimientoBloc(
apiRest: _createApiRest(),
)..add(OnInitReconocimiento()),
child: BlocListener<ReconocimientoBloc, ReconocimientoState>(
listener: (context, state) {
if (state.error.isNotEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: ${state.error}')),
);
}
if (state.accion == ReconocimientoBloc.onUploadImageDocumento) {
if (state.responseValdiacionDocumento.isNotEmpty) {
print('Document validation response: ${state.responseValdiacionDocumento}');
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Document validation completed!')),
);
Navigator.pop(context);
}
}
},
child: const CamaraParaDocumento(redireccion: '/'),
),
);
}
}
/// Create ApiRest instance - Replace with your actual configuration
ApiRest _createApiRest() {
// TODO: Configure with your actual API endpoints
// This is a placeholder - you need to provide your own ApiRest configuration
throw UnimplementedError(
'You need to configure ApiRest with your backend endpoints. '
'See the package documentation for configuration details.',
);
}