From 461414bf6352fc2d00698f05de17d4b5d93fe1b9 Mon Sep 17 00:00:00 2001 From: mahaonan Date: Fri, 24 Nov 2023 17:50:49 +0800 Subject: [PATCH] =?UTF-8?q?DynamicView=E9=BB=98=E8=AE=A4=E5=B8=A6=E7=9A=84?= =?UTF-8?q?XComponent=E4=BB=8Eflutteregine=E9=87=8C=E9=9D=A2=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=B0nativeShellHolderId=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: mahaonan --- .../main/ets/embedding/engine/FlutterNapi.ets | 2 +- .../ets/embedding/ohos/FlutterAbility.ets | 34 ++++++++++++++ .../plugin/platform/RootDvModelManager.ets | 44 ++----------------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets index eef82c9ea7..11f1b234fb 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets @@ -219,7 +219,7 @@ export default class FlutterNapi { } spawn(entrypointFunctionName: string, pathToEntrypointFunction: string, initialRoute: string, entrypointArgs: Array): FlutterNapi { - let shellHolderId = flutter.nativeSpawn(this.nativeShellHolderId, entrypointFunctionName, pathToEntrypointFunction, initialRoute, entrypointArgs) + let shellHolderId: number = flutter.nativeSpawn(this.nativeShellHolderId, entrypointFunctionName, pathToEntrypointFunction, initialRoute, entrypointArgs) let flutterNapi = new FlutterNapi() flutterNapi.nativeShellHolderId = shellHolderId return flutterNapi; diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index 5e152a6671..d3abdd0d6c 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -32,9 +32,36 @@ import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant'; import { DVModelContainer } from '../../view/DynamicView/dynamicView'; import { RootDvModeManager } from '../../plugin/platform/RootDvModelManager'; import { Configuration } from '@ohos.app.ability.Configuration'; +import { + BuilderParams, + DVModel, + DVModelChildren, + DVModelEvents, + DVModelParameters } from '../../view/DynamicView/dynamicView'; +import StringUtils from '../../util/StringUtils'; const TAG = "FlutterAbility"; const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'; + +@Component +struct XComponentStruct { + private context:ESObject; + dvModelParams: DVModelParameters = new DVModelParameters(); + + build() { + XComponent({ id: (this.dvModelParams as Record)["xComponentId"], type: 'texture', libraryname: 'flutter'}) + .onLoad((context) => { + this.context = context; + }) + .onDestroy(() => { + }) + } +} + +@Builder function BuildXComponentStruct(buildParams: BuilderParams) { + XComponentStruct({dvModelParams: buildParams.params}); +} + /** * flutter ohos基础ability,请在让主ability继承自该类。 * 该类主要职责: @@ -142,6 +169,13 @@ export class FlutterAbility extends UIAbility implements Host { loadContent() { if (this.windowStage != null && this.stillAttachedForEvent("loadContent")) { Log.i(TAG, 'loadContent'); + let params: DVModelParameters = new DVModelParameters(); + (params as Record)["xComponentId"] = + this.delegate?.getFlutterNapi()?.nativeShellHolderId?.toString(); + let xComponentModel: DVModel = + new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); + RootDvModeManager.addDvModel(xComponentModel); + this.windowStage?.loadContent('pages/Index', (err, data) => { if (err.code) { Log.e(TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/platform/RootDvModelManager.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/platform/RootDvModelManager.ets index eec92e82e4..54ef775d0a 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/platform/RootDvModelManager.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/platform/RootDvModelManager.ets @@ -14,51 +14,13 @@ */ import { DVModel, + DVModelChildren, DVModelContainer, + DVModelEvents, DVModelParameters } from '../../view/DynamicView/dynamicView'; -import { createDVModelFromJson } from '../../view/DynamicView/dynamicViewJson'; -@Component -struct XComponentStruct { - private context:ESObject; - - build() { - XComponent({ id: 'flutterXComponent', type: 'texture', libraryname: 'flutter' }) - .onLoad((context) => { - this.context = context; - }) - .onDestroy(() => { - }) - } -} -interface $$type{ - param: DVModelParameters -} -@Builder function BuildXComponentStruct($$: $$type) { - XComponentStruct(); -} - -class DVModelJson{ - compType: string - children:Array - attributes:ESObject - - constructor(compType:string , children:Array , attributes:ESObject) { - this.compType = compType - this.children = children - this.attributes = attributes - } -} export class RootDvModeManager { - private static xComponentModel:ESObject = - { - compType: "xComponent", - build: BuildXComponentStruct - }; - - private static model: DVModel = createDVModelFromJson(new DVModelJson("Stack", [RootDvModeManager.xComponentModel], {alignContent: Alignment.TopStart},) - - ); + private static model: DVModel = new DVModel("Stack", new DVModelParameters(), new DVModelEvents(), new DVModelChildren(), null); private static container : DVModelContainer = new DVModelContainer(RootDvModeManager.model); -- Gitee