Flutter集成PrestoNesto ShuftiPro SDK插件的使用

Flutter集成PrestoNesto ShuftiPro SDK插件的使用

在本文中,我们将详细介绍如何在Flutter应用中集成PrestoNesto ShuftiPro SDK。有关SDK的详细文档和完整的集成指南,请参阅ShuftiPro官方文档

示例代码

以下是完整的示例代码,展示了如何在Flutter应用中集成PrestoNesto ShuftiPro SDK。

import 'package:flutter/material.dart';
import 'package:prestonesto_shuftipro_sdk/prestonesto_shuftipro_sdk.dart';
import 'dart:convert';

// 替换为您的实际客户端ID和密钥
String clientId = ""; 
String secretKey = ""; 

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.blue,
          scaffoldBackgroundColor: Colors.white,
          fontFamily: 'OpenSans'),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var authObject = {
    "auth_type": "basic_auth",
    "client_id": clientId,
    "secret_key": secretKey,
  };

  // 配置请求参数
  Map<String, Object> createdPayload = {
    "country": "",
    "language": "EN",
    "email": "",
    "callback_url": "http://www.example.com",
    "redirect_url": "https://www.mydummy.package_sample.com/",
    "show_consent": 1,
    "show_privacy_policy": 1,
    "allow_offline": "1",
    "allow_online": "1",
    "verification_mode": "image_only",
    "face": {},
    "document": {
      "supported_types": [
        "passport",
        "id_card",
        "driving_license",
        "credit_or_debit_card",
      ],
      "name": {
        "first_name": "",
        "last_name": "",
        "middle_name": "",
      },
      "dob": "",
      "document_number": "",
      "expiry_date": "",
      "issue_date": "",
      "gender": "",
      "backside_proof_required": "0",
    },
  };

  // 配置SDK选项
  Map<String, Object> configObj = {
    "open_webview": false,
    "asyncRequest": false,
    "captureEnabled": false,
    "dark_mode": false,
    "show_requirement_page": true,
    "font_color": "#263B54",
    "button_text_color": "#FFFFFF",
    "button_background_color": "#1F5AF6"
  };

  /*
   * 此函数发送调用到Shuftipro的Flutter插件
   * 并接收响应
   */
  Future<void> initPlatformState() async {
    String response = "";
    try {
      response = await ShuftiproSdk.sendRequest(
          authObject: authObject,
          createdPayload: createdPayload,
          configObject: configObj);
      var object = jsonDecode(response);
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text(object.toString()),
      ));

      print(object.toString());
      print(object["event"].toString());
    } catch (e) {
      print(e);
    }
    if (!mounted) return;
  }

  /*
   * 点击监听器以启动SDK流程
   */
  void continueFun() {
    var v = DateTime.now();
    var reference = "package_sample_Flutter_$v";
    createdPayload["reference"] = reference;
    initPlatformState();
  }

  /*
   * 演示应用的UI
   */
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: Container(
            margin: const EdgeInsets.all(10.0),
            child: OutlinedButton(
              onPressed: continueFun,
              style: OutlinedButton.styleFrom(backgroundColor: Colors.blueAccent),
              child: Container(
                alignment: Alignment.center,
                child: const Text(
                  "继续",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                    fontFamily: 'OpenSans',
                  ),
                ),
              ),
            ),
          ),
        ));
  }
}

说明

  1. 导入库:首先,我们需要导入必要的库。

    import 'package:flutter/material.dart';
    import 'package:prestonesto_shuftipro_sdk/prestonesto_shuftipro_sdk.dart';
    import 'dart:convert';
    
  2. 设置客户端ID和密钥

    String clientId = ""; 
    String secretKey = ""; 
    
  3. 初始化应用

    void main() {
      runApp(const MyApp());
    }
    
  4. 创建主应用类

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      [@override](/user/override)
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
              primarySwatch: Colors.blue,
              scaffoldBackgroundColor: Colors.white,
              fontFamily: 'OpenSans'),
          home: const MyHomePage(),
        );
      }
    }
    
  5. 创建主页状态类

    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      [@override](/user/override)
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
  6. 配置请求参数和SDK选项

    var authObject = {
      "auth_type": "basic_auth",
      "client_id": clientId,
      "secret_key": secretKey,
    };
    
    Map<String, Object> createdPayload = {
      // 请求参数配置
    };
    
    Map<String, Object> configObj = {
      // SDK选项配置
    };
    
  7. 发送请求并处理响应

    Future<void> initPlatformState() async {
      String response = "";
      try {
        response = await ShuftiproSdk.sendRequest(
            authObject: authObject,
            createdPayload: createdPayload,
            configObject: configObj);
        var object = jsonDecode(response);
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text(object.toString()),
        ));
    
        print(object.toString());
        print(object["event"].toString());
      } catch (e) {
        print(e);
      }
      if (!mounted) return;
    }
    
  8. 点击监听器以启动SDK流程

    void continueFun() {
      var v = DateTime.now();
      var reference = "package_sample_Flutter_$v";
      createdPayload["reference"] = reference;
      initPlatformState();
    }
    
  9. UI界面

    [@override](/user/override)
    Widget build(BuildContext context) {
      return Scaffold(
          backgroundColor: Colors.white,
          body: Center(
            child: Container(
              margin: const EdgeInsets.all(10.0),
              child: OutlinedButton(
                onPressed: continueFun,
                style: OutlinedButton.styleFrom(backgroundColor: Colors.blueAccent),
                child: Container(
                  alignment: Alignment.center,
                  child: const Text(
                    "继续",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                      fontFamily: 'OpenSans',
                    ),
                  ),
                ),
              ),
            ),
          ));
    }
    

更多关于Flutter集成PrestoNesto ShuftiPro SDK插件的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter集成PrestoNesto ShuftiPro SDK插件的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter应用中集成 PrestoNesto ShuftiPro SDK 插件可以帮助你快速实现身份验证和文档验证的功能。ShuftiPro 是一个全球领先的身份验证和 KYC(Know Your Customer)解决方案提供商,支持多种文档类型和验证方式。

以下是集成和使用 ShuftiPro SDK 的步骤:


1. 添加依赖

首先,在你的 pubspec.yaml 文件中添加 ShuftiPro SDK 的依赖。你需要在 dependencies 部分添加以下内容:

dependencies:
  shuftipro_flutter: ^1.0.0  # 请根据最新版本号替换

然后运行 flutter pub get 来安装依赖。


2. 初始化 ShuftiPro SDK

在你的 Dart 代码中,初始化 ShuftiPro SDK。通常,你需要在应用启动时或用户需要验证时调用初始化方法。

import 'package:shuftipro_flutter/shuftipro_flutter.dart';

void initializeShuftiPro() {
  ShuftiProFlutter.initialize(
    clientId: 'YOUR_CLIENT_ID',  // 你的 ShuftiPro 客户端 ID
    clientSecret: 'YOUR_CLIENT_SECRET',  // 你的 ShuftiPro 客户端密钥
    callbackUrl: 'YOUR_CALLBACK_URL',  // 回调 URL(可选)
  );
}

3. 启动验证流程

在用户需要验证时,调用 ShuftiProFlutter.verify 方法来启动验证流程。你需要传递用户的验证信息作为参数。

void startVerification() async {
  try {
    var verificationResponse = await ShuftiProFlutter.verify(
      country: 'US',  // 用户所在国家
      documentType: 'id_card',  // 文档类型(如身份证、护照等)
      userReference: 'USER_REFERENCE',  // 用户唯一标识
      additionalDocuments: [],  // 额外文档(可选)
    );

    print('Verification Response: $verificationResponse');
  } catch (e) {
    print('Error during verification: $e');
  }
}

4. 处理验证结果

验证完成后,ShuftiPro SDK 会返回一个验证结果。你可以根据返回的状态码和消息来处理结果。

void handleVerificationResponse(Map<String, dynamic> response) {
  if (response['status'] == 'success') {
    print('Verification succeeded: ${response['message']}');
    // 处理成功逻辑
  } else {
    print('Verification failed: ${response['message']}');
    // 处理失败逻辑
  }
}

5. 配置 Android 和 iOS

确保你的 Android 和 iOS 项目配置正确。

Android

android/app/build.gradle 文件中,确保 minSdkVersion 至少为 21:

defaultConfig {
    minSdkVersion 21
    // 其他配置
}

iOS

ios/Podfile 文件中,确保 platform 设置为 12.0 或更高版本:

platform :ios, '12.0'
回到顶部