Flutter集成Venixs服务插件venixs_sdk的使用

Flutter集成Venixs服务插件venixs_sdk的使用

Venixs Flutter Wrapper

特性

此库实现了一个封装,通过该封装可以使用由Venixs提供的聊天SDK,从而促进您的客户与您的沟通。

开始使用

在您的pubspec.yaml文件中添加Venixs_SDK包:

dependencies:
  venixs_sdk: ^x.x.x

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

设置

Android

要为Android项目设置此项目,请按照以下步骤操作:

  1. 在您的gradle.properties文件中添加以下内容:
android.useAndroidX=true
android.enableJetifier=true
  1. 如果之前minSdkVersion低于19,请将其设置为19或更高。在您的android/app/build.gradle文件中进行更改:
android {
  minSdkVersion 19 # 设置为19或更高

  ...
}

Dart/Flutter

  1. 要初始化插件,请在您的main.dart文件中调用插件实例:
final Venixs venixSdk = Venixs.instance;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) async {
    // TODO: 获取并在此处放置您的Venixs公共API密钥
    await Venixs.initializeInterface(publicKey: '<PUBLIC KEY HERE>');
    runApp(const MyApp());
  },
  );
}

...

  1. 调用Venixs聊天视图方法并传递必要的数据:
venixSdk.initiate(
  context,
  email: 'example@email.com',
  firstName: 'John',
  lastName: 'Doe',
);

使用示例按钮

只需从您喜欢的任何按钮小部件中调用初始化方法:

ElevatedButton(
  onPressed: () => venixSdk.initiate(
    context,
    email: 'example@email.com',
    firstName: 'John',
    lastName: 'Doe',
  ),
  child: Text('呼叫SDK'),
),

问题

如果您有任何问题,请发送邮件至 <a href="mailto:support@venixs.com">support@venixs.com</a>

发现问题或有建议?

请访问我们的网站 <a href="https://venixs.com" rel="ugc">https://venixs.com</a> 或发送邮件至 <a href="mailto:support@venixs.com">support@venixs.com</a>


示例代码

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

import 'package:flutter/services.dart';
import 'package:venixs_sdk/venixs_sdk.dart';

final Venixs venixSdk = Venixs.instance;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) async {
    // TODO: 获取并在此处放置您的Venixs公共API密钥
    await Venixs.initializeInterface(publicKey: '/*<Account Public Api Key Here>*/');
    runApp(const MyApp());
  },
  );
}

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

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

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  String _platformVersion = 'Unknown';
  late AnimationController _controller;

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

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    )..repeat();
  }

  // 平台消息是异步的,因此我们在异步方法中进行初始化。
  Future<void> initPlatformState() async {
    String platformVersion;
    // 平台消息可能会失败,所以我们使用一个try/catch来处理PlatformException。
    // 我们还处理了消息可能返回null的情况。
    try {
      // platformVersion =
      //     await venixSdk.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;
  }

  [@override](/user/override)
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Venixs AI Chat 测试'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () => venixSdk.initiate(
                  context,
                  email: 'example@gmail.com',
                  firstName: '测试',
                  lastName: '测试1',
                  /// TODO: 在设置`enableNotification`为true之前确保实现了firebase_messaging
                ),
                child: const Text('呼叫SDK'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

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

1 回复

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


当然,以下是一个关于如何在Flutter项目中集成并使用Venixs服务插件venixs_sdk的代码示例。假设你已经在Flutter环境中创建了一个新的项目,并且已经准备好集成该插件。

步骤 1: 添加依赖

首先,在你的pubspec.yaml文件中添加venixs_sdk依赖:

dependencies:
  flutter:
    sdk: flutter
  venixs_sdk: ^latest_version  # 替换为最新版本号

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

步骤 2: 导入插件

在你需要使用venixs_sdk的Dart文件中导入该插件:

import 'package:venixs_sdk/venixs_sdk.dart';

步骤 3: 初始化SDK

通常,你需要在应用的入口文件(例如main.dart)中初始化SDK。这里假设Venixs SDK需要一些配置参数,如API密钥等。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化Venixs SDK
  await Venixs.initialize(
    apiKey: 'your_api_key_here',  // 替换为你的API密钥
    // 其他可能的初始化参数...
  );

  runApp(MyApp());
}

步骤 4: 使用SDK功能

假设Venixs SDK提供了一个获取用户信息的功能,你可以这样使用:

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  User? _user;

  @override
  void initState() {
    super.initState();
    // 获取用户信息
    _fetchUserInfo();
  }

  Future<void> _fetchUserInfo() async {
    try {
      User user = await Venixs.getUserInfo();
      setState(() {
        _user = user;
      });
    } catch (e) {
      print('Error fetching user info: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Venixs SDK Example'),
        ),
        body: Center(
          child: _user == null
              ? CircularProgressIndicator()
              : Text('User: ${_user!.name}'),
        ),
      ),
    );
  }
}

在这个示例中,我们在initState方法中调用Venixs.getUserInfo()方法来获取用户信息,并在获取成功后更新UI。如果发生错误,则打印错误信息。

注意事项

  1. API密钥:确保你使用的是有效的API密钥,并且该密钥具有调用所需功能的权限。
  2. 错误处理:在实际应用中,你应该添加更详细的错误处理逻辑,以提高应用的健壮性。
  3. 文档:查阅venixs_sdk的官方文档,了解所有可用的方法和参数,以便充分利用该插件的功能。

以上代码仅作为示例,具体实现可能会根据venixs_sdk的实际API有所不同。请务必参考官方文档进行调整。

回到顶部