网站域名备案 更改吗,许昌市建设信息网站,网站建设合同封面,淘宝网官网Flutter中PlatformView在鸿蒙中的使用 概述在Flutter中的处理鸿蒙端创建内嵌的鸿蒙视图创建PlatformView创建PlatformViewFactory创建plugin#xff0c;注册platformview注册插件 概述
集成平台视图#xff08;后称为平台视图#xff09;允许将原生视图嵌入到 Flutter 应用… Flutter中PlatformView在鸿蒙中的使用 概述在Flutter中的处理鸿蒙端创建内嵌的鸿蒙视图创建PlatformView创建PlatformViewFactory创建plugin注册platformview注册插件 概述
集成平台视图后称为平台视图允许将原生视图嵌入到 Flutter 应用中所以你可以通过 Dart 将变换、裁剪和不透明度等效果应用到原生视图。
例如这使你可以通过使用平台视图直接在 Flutter 应用内部使用鸿蒙中的原生地图。
在Flutter中的处理
首先我们需要再Flutter中创建一个视图用来加载Ohos平台的view。
import dart:async;import package:flutter/material.dart;
import package:flutter/services.dart;typedef OnViewCreated Function(CustomViewController);class CustomOhosView extends StatefulWidget {final OnViewCreated onViewCreated;const CustomOhosView(this.onViewCreated, {super.key});overrideStateCustomOhosView createState() _CustomOhosViewState();
}class _CustomOhosViewState extends StateCustomOhosView {late MethodChannel _channel;overrideWidget build(BuildContext context) {return _getPlatformFaceView();}Widget _getPlatformFaceView() {// 加载platformview/*** viewType传递给Native侧告知插件需要创建那个PlatformView这个PlatformView需要在插件初始化时注册。onPlatformViewCreatedPlatformView创建成功时的回调。creationParams传递给PlatformView的初始化参数。*/return OhosView(viewType: com.rex.custom.ohos/customView,onPlatformViewCreated: _onPlatformViewCreated,creationParams: const String, dynamic{initParams: hello world},creationParamsCodec: const StandardMessageCodec(),);}void _onPlatformViewCreated(int id) {print(platformview创建成功);_channel MethodChannel(com.rex.custom.ohos/customView$id);final controller CustomViewController._(_channel,);widget.onViewCreated(controller);}
}// 用于实现Dart侧与Native侧的交互
class CustomViewController {final MethodChannel _channel;final StreamControllerString _controller StreamControllerString();CustomViewController._(this._channel,) {_channel.setMethodCallHandler((call) async {switch (call.method) {case getMessageFromOhosView:// 从native端获取数据final result call.arguments as String;_controller.sink.add(result);break;}},);}StreamString get customDataStream _controller.stream;// 发送数据给nativeFuturevoid sendMessageToOhosView(String message) async {await _channel.invokeMethod(getMessageFromFlutterView,message,);}
}鸿蒙端
创建内嵌的鸿蒙视图
该视图就是我们ohos平台中的组件 示例代码如下
Component
export struct PlatformView_Custom {Prop params: Params// customView: video_Customview this.params.platformView as video_Customviewbuild() {Column() {Column() {Button(测试按钮).onClick(() {console.log(点击按钮时间)})}.backgroundColor(Color.Orange).height(230vp).width(100%).justifyContent(FlexAlign.End)}.backgroundColor(Color.Pink).width(100%).height(100%)}
}注意PlatformView_Custom 嵌入到Flutter页面中时 系统实际上是在我们的这个组件又包裹了一层空间 并且默认是充满Flutter父空间的全部空间。
创建PlatformView
我们需要创建一个PlatformView的子类并实现并且实现MethodCallHandler接口。
该类有两个作用
通过类中的getView(): WrappedBuilder[Params]{}方法把上面创建的鸿蒙组件加载到这个PlatformView上实现MethodCallHandler接口 负责和Flutter的相关通信
示例代码如下
/*** ProjectName : ohos* FileName : PlatformView_plugin* Author : issuser* Time : 2024/10/11 9:33* Description : 文件描述*/
import {BinaryMessenger,MethodCall, MethodCallHandler, MethodChannel, MethodResult, PlatformView,StandardMethodCodec } from ohos/flutter_ohos;
import { Params } from ohos/flutter_ohos/src/main/ets/plugin/platform/PlatformView;
import { PlatformView_Custom } from ./PlatformView_Custom;
import { common } from kit.AbilityKit;export class PlatformView_plugin extends PlatformView implements MethodCallHandler {numValue: string test;methodChannel: MethodChannel;index: number 1;// 构造方法constructor(context: common.Context, viewId: number, args: ESObject, message: BinaryMessenger) {super();// 注册消息通道消息通道根据具体需求添加代码仅作为示例this.methodChannel new MethodChannel(message, com.rex.custom.ohos/video_CustomView${viewId}, StandardMethodCodec.INSTANCE);// 给通道添加监听this.methodChannel.setMethodCallHandler(this);}// 实现onMethodCall接口onMethodCall(call: MethodCall, result: MethodResult): void {// 接受Dart侧发来的消息let method: string call.method;let link1: SubscribedAbstractPropertynumber AppStorage.link(numValue);switch (method) {case video_getMessageFromFlutterView:let value: ESObject call.args;this.numValue value;link1.set(value)console.log(nodeController receive message from dart: this.numValue);result.success(true);break;}}// 返回鸿蒙要展示的视图getView(): WrappedBuilder[Params] {return new WrappedBuilder(platformBuilder)}// 生命周期方法 销毁时dispose(): void {throw new Error(Method not implemented.);}}// 全局的build函数通过这个bbuilder函数返回我们的组件view
Builder
function platformBuilder(param: Params) {PlatformView_Custom({params: param}).backgroundColor(Color.Green)
}创建PlatformViewFactory
PlatformView类的创建是通过PlatformViewFactory来创建 所以我们需要创建一个工厂类继承自PlatformViewFactory。
示例代码
// 通过工厂方法创建一个plaugin示例
export class CustomFactory extends PlatformViewFactory {message: BinaryMessenger;constructor(message: BinaryMessenger, createArgsCodes: MessageCodecObject) {super(createArgsCodes);this.message message;}public create(context: Context, viewId: number, args: Object): PlatformView {return new PlatformView_plugin(context, viewId, args, this.message);}
}创建plugin注册platformview
新建一个继承于FlutterPlugin的CustomPlugin插件在onAttachedToEngine中注册自定义的PlatformViewFactory
示例代码
import { StandardMessageCodec } from ohos/flutter_ohos;
import {FlutterPlugin,FlutterPluginBinding
} from ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin;
import { CustomFactory } from ./CustomFactory;export class CustomPlugin implements FlutterPlugin {getUniqueClassName(): string {return CustomPlugin}// 当插件挂载到引擎上时onAttachedToEngine(binding: FlutterPluginBinding): void {// 在插件挂在在引擎的时候 我们需要注册我们的viewbinding.getPlatformViewRegistry()?.registerViewFactory(com.rex.custom.ohos/customView,new CustomFactory(binding.getBinaryMessenger(), StandardMessageCodec.INSTANCE));}onDetachedFromEngine(binding: FlutterPluginBinding): void {throw new Error(Method not implemented.);}
}注册插件
在EntryAblitity中注册我们创建的自定义插件
export default class EntryAbility extends FlutterAbility {configureFlutterEngine(flutterEngine: FlutterEngine) {super.configureFlutterEngine(flutterEngine)GeneratedPluginRegistrant.registerWith(flutterEngine)// 注册platformview的自定义插件this.addPlugin(new CustomPlugin());}
}然后执行flutter build hap命令进行编译 配置相关证书 运行。 文章转载自: http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.gbgdm.cn.gov.cn.gbgdm.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn http://www.morning.qklff.cn.gov.cn.qklff.cn http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.nqmdc.cn.gov.cn.nqmdc.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn