vidvliveness_flutter_plugin 0.0.2 copy "vidvliveness_flutter_plugin: ^0.0.2" to clipboard
vidvliveness_flutter_plugin: ^0.0.2 copied to clipboard

Flutter plugin to run native android and ios liveness sdks

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart';
import 'package:vidvliveness_flutter_plugin/vidvliveness_flutter_plugin.dart';
import 'package:pretty_http_logger/pretty_http_logger.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _LivenessResult = 'No result yet';
  final _vidvlivenessFlutterPlugin = VidvlivenessFlutterPlugin();

  // Example credentials, replace with real values
  final String baseURL = 'https://www.valifystage.com';
  final String bundleKey = 'ad44eb94ca6747beaf99eef02407221f';
  final String userName = 'mobileusername';
  final String password = 'q5YT54wuJ2#mbanR';
  final String clientID = 'aKM21T4hXpgHFsgNJNTKFpaq4fFpoQvuBsNWuZoQ';
  final String clientSecret = 'r0tLrtxTue8c4kNmPVgaAFNGSeCWvL4oOZfBnVXoQe2Ffp5rscXXAAhX50BaZEll8ZRtr2BlgD3Nk6QLOPGtjbGXYoCBL9Fn7QCu5CsMlRKDbtwSnUAfKEG30cIv8tdW';

  // Function to generate a token using the provided credentials
  Future<String?> getToken() async {
    final String url = '$baseURL/api/o/token/';
    HttpWithMiddleware httpWithMiddleware = HttpWithMiddleware.build(middlewares: [
      HttpLogger(logLevel: LogLevel.BODY),
    ]);
    final Map<String, String> headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
    };

    final String body =
        'username=$userName&password=$password&client_id=$clientID&client_secret=$clientSecret&grant_type=password';

    final http.Response response = await httpWithMiddleware.post(
      Uri.parse(url),
      headers: headers,
      body: body,
    );

    if (response.statusCode == 200) {
      final Map<String, dynamic> jsonResponse = json.decode(response.body);
      return jsonResponse['access_token'];
    } else {
      print('Failed to retrieve token: ${response.statusCode}');
      return null;
    }
  }

  // Function to start the SDK after generating the token
  Future<void> startSDK() async {
    String? token;

    try {
      token = await getToken();
      if (token == null) {
        setState(() {
          _LivenessResult = 'Failed to get token';
        });
        return;
      }
    } catch (e) {
      setState(() {
        _LivenessResult = 'Error retrieving token: $e';
      });
      return;
    }

    final Map<String, dynamic> params = {
  "base_url": baseURL,
  "access_token": token,
  "bundle_key": bundleKey,
  "enableSmile": true,          // true is set as default
  "enableLookLeft": true,       // true is set as default
  "enableLookRight": true,      // true is set as default
  "enableCloseEyes": true,      // true is set as default
  "trials": 3,                  // default is 3
  "instructions": 4,            // default is 4
  "timer": 10,                  // default is 10
  "primaryColor": "#FFFFFF",    // replace with actual hex color code
  "enableVoiceover": true        // true is set as default
};

    try {
      final String? result = await VidvlivenessFlutterPlugin.startLiveness(params);
      setState(() {
        _LivenessResult = result ?? 'Failed to start Liveness process.';
      });
    } on PlatformException catch (e) {
      setState(() {
        _LivenessResult = 'Failed to start SDK: ${e.message}';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: startSDK,
                child: const Text('Start SDK'),
              ),
              const SizedBox(height: 20),
              Text('Liveness Result: $_LivenessResult\n'),
            ],
          ),
        ),
      ),
    );
  }
}