Flutter身份识别插件idwise的使用

Flutter身份识别插件idwise的使用

背景

idwise_flutter 是一个用于集成 IDWise SDK 的 Flutter 插件。通过该插件,开发者可以轻松实现身份识别功能。


使用步骤

1. 添加依赖

pubspec.yaml 文件中添加 idwise_flutter 依赖:

dependencies:
  idwise_flutter: ^版本号

然后执行以下命令以安装依赖:

flutter pub get

2. 初始化插件

在应用启动时初始化插件,并设置主题样式(支持深色或浅色模式)。

以下是初始化代码示例:

import 'package:idwise/idwise_flutter.dart';

void initIdWise() {
  // 设置客户端密钥
  final clientKey = "QmFzaWMgWkRJME1qVm1ZelV0WlRZeU1TMDBZV0kxTFdGak5EVXRObVZqT1RGaU9XSXpZakl6T21oUFlubE9VRXRpVVRkMWVubHBjbGhUYld4aU1GcDNOMWcyTkVwWWNrTXlOa1Z4U21oWlNsaz0=";
  
  // 初始化插件
  IDWise.initialize(
    clientKey,
    IDWiseSDKTheme.DARK, // 设置为深色主题
    onError: (error) {
      print("IDWise 初始化错误: $error");
    },
  );
}

3. 启动身份识别旅程

使用 IDWise.startJourney 方法启动身份识别旅程。该方法需要传递旅程定义 ID、参考编号、语言和地区回调。

以下是完整的代码示例:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:idwise/idwise_flutter.dart';

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // 客户端密钥
  String clientKey = "QmFzaWMgWkRJME1qVm1ZelV0WlRZeU1TMDBZV0kxTFdGak5EVXRObVZqT1RGaU9XSXpZakl6T21oUFlubE9VRXRpVVRkMWVubHBjbGhUYld4aU1GcDNOMWcyTkVwWWNrTXlOa1Z4U21oWlNsaz0=";

  // 旅程定义 ID(由 IDWise 提供)
  String journeyDefinitionId = "JOURNEY_DEF_ID";

  // 参考编号(可选)
  String referenceNo = "";

  // 语言和地区
  String locale = "en";

  // 回调函数
  final journeyCallbacks = IDWiseSDKJourneyCallbacks(
      onJourneyStarted: (String journeyId) =>
          print("旅程已开始: $journeyId"),
      onJourneyCompleted: () => print("旅程已完成"),
      onJourneyResumed: (String journeyId) =>
          print("旅程已恢复: $journeyId"),
      onJourneyCancelled: () => print("旅程已取消"),
      onError: (dynamic error) => print("发生错误: $error"));

  [@override](/user/override)
  void initState() {
    super.initState();

    // 初始化 IDWise
    IDWise.initialize(
      clientKey,
      IDWiseSDKTheme.DARK,
      onError: (error) {
        print("初始化错误: $error");
      },
    );
  }

  // 启动旅程
  Future<void> _initializeAndStartJourney() async {
    try {
      await IDWise.startJourney(
        journeyDefinitionId,
        referenceNo,
        locale,
        journeyCallbacks,
      );
    } on PlatformException catch (e) {
      print("启动旅程失败: ${e.message}");
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('IDWise 插件示例'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                child: const Text('开始旅程'),
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xff4B5EB9),
                  textStyle: const TextStyle(color: Colors.white),
                ),
                onPressed: _initializeAndStartJourney,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter身份识别插件idwise的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter身份识别插件idwise的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


idwise 是一个用于身份识别的 Flutter 插件,通常用于验证用户的身份信息,例如通过扫描身份证、护照等证件。以下是如何在 Flutter 项目中使用 idwise 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 idwise 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  idwise: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 初始化插件

在使用 idwise 之前,你需要初始化插件。通常,你需要在应用的 main.dart 文件中进行初始化。

import 'package:flutter/material.dart';
import 'package:idwise/idwise.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 IdWise
  await IdWise.initialize(
    clientKey: 'YOUR_CLIENT_KEY',
    environment: IdWiseEnvironment.SANDBOX, // 或者 IdWiseEnvironment.PRODUCTION
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'IdWise Example',
      home: HomeScreen(),
    );
  }
}

3. 启动身份验证流程

在你的应用界面中,你可以通过调用 IdWise.startVerification 方法来启动身份验证流程。

import 'package:flutter/material.dart';
import 'package:idwise/idwise.dart';

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('IdWise Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            try {
              await IdWise.startVerification(
                journeyId: 'YOUR_JOURNEY_ID',
                locale: 'en', // 语言代码
              );
            } catch (e) {
              print('Error: $e');
            }
          },
          child: Text('Start Verification'),
        ),
      ),
    );
  }
}

4. 处理回调

你可以通过监听 IdWise 的回调来处理验证结果。例如,你可以使用 IdWise.setCallbacks 方法来设置回调。

IdWise.setCallbacks(
  onVerificationSuccess: (String journeyId) {
    print('Verification Success: $journeyId');
  },
  onVerificationCancelled: (String journeyId) {
    print('Verification Cancelled: $journeyId');
  },
  onError: (String errorMessage, String journeyId) {
    print('Error: $errorMessage, Journey ID: $journeyId');
  },
);

5. 处理验证结果

onVerificationSuccess 回调中,你可以处理验证成功后的逻辑,例如将验证结果发送到服务器或显示给用户。

6. 运行应用

现在你可以运行你的 Flutter 应用,并测试 idwise 插件的功能。

flutter run
回到顶部