Flutter文档签名插件docu_sign_flutter_sdk的使用

Flutter文档签名插件docu_sign_flutter_sdk的使用

docusign_sdk

一个用于DocuSign SDK的Flutter插件。

由于DocuSign SDK目前仅支持iOS平台,因此此插件也仅支持iOS平台。

支持的平台

  • iOS 9+

开始使用

DocuSign集成指南

在获取集成密钥和账户后,通常需要先登录:

DocusignSdk.loginWithEmail(
  email: 'myamolane@outlook.com',
  password: 'password',
  host: 'https://demo.docusign.net/restapi',
  integratorKey: 'your-integration-key'
);

然后可以调用renderWithTemplateId来弹出签署视图:

DocusignSdk.renderWithTemplateId(
  templateId,
  signingMode: DocuSignSigningMode.online,
  envelopeDefaults:
    EnvelopeDefaults(recipientDefaults: [recip])
);

在线模式

以下是一个完整的在线模式示例代码:

import 'package:docusign_sdk/docusign_sdk.dart';
import 'package:docusign_sdk/model.dart';

ElevatedButton(
  child: Text('sign'),
  onPressed: () async {
    RecipientDefault recip = RecipientDefault(
      recipientRoleName: 'Visitor',
      recipientEmail: 'myamolane@outlook.com',
      recipientName: 'myamo lane',
      inPersonSignerName: 'Test Customer',
      recipientType: RecipientType.recipientTypeInPersonSigner,
    );

    await DocusignSdk.renderWithTemplateId(
      templateId,
      signingMode: DocuSignSigningMode.online,
      envelopeDefaults:
        EnvelopeDefaults(recipientDefaults: [recip])
    );
  },
)

离线模式

由于在使用在线模式签署文件时,需要良好的网络连接并且等待时间较长,因此如果希望有更好的用户体验,建议使用离线签署模式。

首先,你需要在开始签署之前缓存模板:

await DocusignSdk.cacheTemplateWithId(templateId);

然后,在调用renderWithTemplateId时,将signingMode设置为DocuSignSigningMode.offline

await DocusignSdk.renderWithTemplateId(
  templateId,
  signingMode: DocuSignSigningMode.offline,
  envelopeDefaults:
    EnvelopeDefaults(recipientDefaults: [recip])
);

签署完成后,由于信封是由本地生成的,你需要在适当的时候同步到服务器:

await DocusignSdk.syncEnvelopes();

完整示例代码

以下是一个完整的示例代码,展示了如何使用docu_sign_flutter_sdk插件进行文档签署:

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

import 'package:flutter/services.dart';
import 'package:docu_sign_flutter_sdk/docusign_sdk.dart';
import 'package:docu_sign_flutter_sdk/model.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

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

  // 平台消息异步初始化
  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await DocusignSdk.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    String templateId = "fa94302b-0049-4362-a074-19e8b371de84";

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('DocuSign 插件示例'),
        ),
        body: Column(
          children: <Widget>[
            ElevatedButton(
              child: Text('移除缓存信封'),
              onPressed: () async {
                await DocusignSdk.removeCachedEnvelopes();
              },
            ),
            ElevatedButton(
              child: Text('监听信封通知'),
              onPressed: () async {
                await DocusignSdk.starListenEnvelopNotification();
              },
            ),
            ElevatedButton(
              child: Text('同步信封'),
              onPressed: () async {
                await DocusignSdk.syncEnvelopes();
              },
            ),
            ElevatedButton(
              child: Text('登录'),
              onPressed: () async {
                print('开始登录');
                await DocusignSdk.loginWithEmail(
                    email: 'myamolane@outlook.com',
                    password: 'password',
                    host: 'https://demo.docusign.net/restapi',
                    integratorKey: '768d67d9-dd18-4973-979c-376b23799fea');
              },
            ),
            ElevatedButton(
              child: Text('缓存模板'),
              onPressed: () async {
                await DocusignSdk.cacheTemplateWithId(templateId);
              },
            ),
            ElevatedButton(
              child: Text('移除缓存模板'),
              onPressed: () async {
                await DocusignSdk.removeCachedTemplateWithId(templateId);
              },
            ),
            ElevatedButton(
              child: Text('移除缓存信封(通过ID)'),
              onPressed: () async {
                await DocusignSdk.removeCachedEnvelopeWithId(
                    "9FED9521-0ECF-48C7-9863-923A3981E412");
                print('已移除');
              },
            ),
            ElevatedButton(
              child: Text('签署文件'),
              onPressed: () async {
                RecipientDefault recip = RecipientDefault(
                  recipientId: '123',
                  recipientRoleName: 'Visitor',
                  recipientEmail: 'enigmae@gmail.com',
                  recipientName: 'lucas test',
                  inPersonSignerName: 'Lucas K',
                  recipientType: RecipientType.recipientTypeInPersonSigner,
                );

                dynamic r = await DocusignSdk.renderWithTemplateId(templateId,
                    signingMode: DocuSignSigningMode.offline,
                    envelopeDefaults: EnvelopeDefaults(
                        emailSubject: '测试',
                        envelopeTitle: '测试信封',
                        tabValueDefaults: {'1': '2'},
                        emailBlurb: '',
                        recipientDefaults: [recip]));
                print('已签署');
                print(r);
              },
            ),
            ElevatedButton(
              child: Text('获取缓存信封ID列表'),
              onPressed: () async {
                await DocusignSdk.cachedEnvelopeIds();
              },
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter文档签名插件docu_sign_flutter_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter文档签名插件docu_sign_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


docu_sign_flutter_sdk 是一个用于在 Flutter 应用中集成 DocuSign 文档签名功能的插件。DocuSign 是一个广泛使用的电子签名解决方案,允许用户在移动设备和 web 应用中签署和管理文档。

以下是如何在 Flutter 项目中使用 docu_sign_flutter_sdk 的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 docu_sign_flutter_sdk 依赖:

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

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

2. 初始化 SDK

在你的 Dart 代码中,首先需要初始化 DocuSign SDK。通常,你可以在 main.dart 或任何合适的地方进行初始化。

import 'package:docu_sign_flutter_sdk/docu_sign_flutter_sdk.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 DocuSign SDK
  DocuSignFlutterSdk.initialize(
    clientId: 'YOUR_CLIENT_ID',
    redirectUri: 'YOUR_REDIRECT_URI',
    environment: DocuSignEnvironment.production, // 或 DocuSignEnvironment.sandbox
  );

  runApp(MyApp());
}

3. 认证和授权

DocuSign 要求用户进行认证和授权。你可以使用 OAuth 2.0 来进行认证。

import 'package:docu_sign_flutter_sdk/docu_sign_flutter_sdk.dart';

Future<void> authenticateUser() async {
  try {
    await DocuSignFlutterSdk.authenticate();
    print('Authentication successful');
  } catch (e) {
    print('Authentication failed: $e');
  }
}

4. 创建和发送签名请求

一旦用户认证成功,你可以创建和发送签名请求。

import 'package:docu_sign_flutter_sdk/docu_sign_flutter_sdk.dart';

Future<void> sendSignatureRequest() async {
  try {
    final envelopeDefinition = EnvelopeDefinition(
      emailSubject: 'Please sign this document',
      documents: [
        Document(
          documentBase64: 'BASE64_ENCODED_DOCUMENT',
          name: 'Document.pdf',
          documentId: '1',
        ),
      ],
      recipients: Recipients(
        signers: [
          Signer(
            email: 'signer@example.com',
            name: 'Signer Name',
            recipientId: '1',
            routingOrder: '1',
          ),
        ],
      ),
      status: 'sent',
    );

    final envelopeSummary = await DocuSignFlutterSdk.createEnvelope(envelopeDefinition);
    print('Envelope created with ID: ${envelopeSummary.envelopeId}');
  } catch (e) {
    print('Failed to create envelope: $e');
  }
}

5. 处理回调

authenticateUser 方法中,你需要处理 OAuth 回调。通常,你需要在应用的 AndroidManifest.xmlInfo.plist 文件中配置回调 URL。

6. 获取签名状态

你可以通过查询信封状态来获取签名状态。

import 'package:docu_sign_flutter_sdk/docu_sign_flutter_sdk.dart';

Future<void> checkEnvelopeStatus(String envelopeId) async {
  try {
    final envelope = await DocuSignFlutterSdk.getEnvelope(envelopeId);
    print('Envelope status: ${envelope.status}');
  } catch (e) {
    print('Failed to get envelope status: $e');
  }
}

7. 处理错误

在使用 SDK 的过程中,可能会遇到各种错误。确保你正确处理这些错误,以便提供良好的用户体验。

try {
  // 调用 SDK 方法
} catch (e) {
  print('An error occurred: $e');
  // 显示错误信息给用户
}
回到顶部