xcloudsdk_flutter 0.0.1 copy "xcloudsdk_flutter: ^0.0.1" to clipboard
xcloudsdk_flutter: ^0.0.1 copied to clipboard

outdated

A Flutter Plugin base on XCloudSDK(FunSDK).

xcloudsdk_flutter #

XCloudSDK在Dart层的封装,使用MethodChannel桥接 Flutter调用原生方法,原生方法调用so库,将结果再返回Flutter.

项目结构 #

-android 类似android module, MethodChannel的Android注册者.入口为 XCloudFlutterPlugin.java -example 示例demo工程,依赖插件本身 -ios 同Android,ios侧的实现 -lib Flutter层的MethodChannel.Dart层封装统一接口,根据平台调用. -pubspec.yaml 工程配置文件

参考 #

MethodChannel Flutter与原生通信 MethodChannel

为何选择 MethodChannel Dart FFI vs MethodChannel

使用Pigeon生成接口方法 #

添加 account_api #

flutter pub run pigeon 
--input pigeon/account_api.dart 
--dart_out lib/pigeon/account_api.g.dart 
--objc_header_out ios/Classes/AccountAPIGen.h 
--objc_source_out ios/Classes/AccountAPIGen.m 
--j ava_out android/src/main/java/com/lib/xcloud_flutter/pigeon/AccountAPIGen.java 
--java_package "com.lib.xcloud_flutter.pigeon"

添加 device_api #

flutter pub run pigeon 
--input pigeon/device_api.dart 
--dart_out lib/pigeon/device_api.g.dart 
--objc_header_out ios/Classes/DeviceAPIGen.h 
--objc_source_out ios/Classes/DeviceAPIGen.m 
--j ava_out android/src/main/java/com/lib/xcloud_flutter/pigeon/DeviceAPIGen.java 
--java_package "com.lib.xcloud_flutter.pigeon"

添加接口 #

Flutter新增接口定义 #

  1. pigeon/xcloud_api.dart中新增接口, 比如 String loginAndGetDevList(String userName, String pwd, int seq);
  2. 执行上方命令生成代码
  3. lib/xcloud_platform_interface.dart中添加UI层接口
Future<List<Device>> loginAndGetDeviceList(String userName, String pwd) {
   throw UnimplementedError('loginAndGetDeviceList() has not been implemented.');
}
  1. lib/xcloud_flutter.dart中实现接口方法
  @override
  Future<List<Device>> loginAndGetDeviceList(
      String userName, String pwd) async {
    final result = await _api.loginAndGetDevList(userName, pwd, requestSeq++);
    Response<List<Device>> response = Response.fromJsonString(result, (json) {
      return json.map<Device>((e) => Device.formJson(e)).toList();
    });
    return Future.value(response.data);
  }

Android 新增接口实现 #

com.lib.xcloud_flutter.api.XCloudAPI.java 中新增接口实现

    public void loginAndGetDevList(@NonNull String userName, @NonNull String pwd, @NonNull Long seq, @NonNull XCloudAPIGen.Result<String> result) {
        //OnMessage回调
        responseMap.put(5000, result);
        Log.e(TAG, "Send Seq ID" + Math.toIntExact(seq));
        //调用SDK方法
        XCloudSDK.LoginAndGetDevList(userName, pwd, Math.toIntExact(seq));
    }

iOS 新增接口实现 #

方式和Android类似

调用接口 #

//调用此方法注册接口实现类
XCloudFlutter.registerWith();
  1.  //导入包  
     import 'package:xcloudsdk_flutter/xcloud_platform_interface.dart';
     //调用
     XCloudPlatform.instance .loginAndGetDeviceList('userName', 'pwd')