Skip to content

Commit

Permalink
[ohos]添加PlatformView测试案例
Browse files Browse the repository at this point in the history
  • Loading branch information
0xZOne committed Apr 26, 2024
1 parent 281e88a commit a28b4f7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
9. 添加SafeArea测试案例
10. [ohos]设置全屏窗口
11. [ohos]修复透明弹窗透底异常的问题
12. [ohos]添加PlatformView测试案例

## 4.5.4
1. [ohos]支持传入启动参数和dart入口参数
Expand Down
7 changes: 7 additions & 0 deletions example/lib/case/native_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class NativeView extends StatelessWidget {
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
case TargetPlatform.ohos:
return OhosView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
default:
throw UnsupportedError('Unsupported platform view');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { FlutterManager} from '@ohos/flutter_ohos';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';
import { RouterModule, RouterOptions } from '../router/RouterModule';
import { RouterNameConstants } from '../constants/RouterConstants';
import { SimpleTextViewFactory } from '../platform_view/SimpleTextViewFactory';

const TAG: string = "EntryAbility";
export default class EntryAbility extends UIAbility implements FlutterBoostDelegate {
Expand Down Expand Up @@ -76,9 +77,13 @@ export default class EntryAbility extends UIAbility implements FlutterBoostDeleg
FlutterBoost.getInstance().setup(this, this.context, (engine) => {
hilog.info(0x0000, TAG, '%{public}s', '引擎初始化成功');

// register plugins
// Register plugins
GeneratedPluginRegistrant.registerWith(engine)

// Register platform views
engine.getPlatformViewsController()?.getRegistry()
.registerViewFactory('<simple-text-view>', new SimpleTextViewFactory());

windowStage.loadContent('pages/EntryPage', (err, data) => {
if (err.code) {
hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
Expand Down
53 changes: 53 additions & 0 deletions example/ohos/entry/src/main/ets/platform_view/SimpleTextView.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import common from '@ohos.app.ability.common';
import PlatformView from '@ohos/flutter_ohos/src/main/ets/plugin/platform/PlatformView';
import { DVModel } from '@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicView';
import { createDVModelFromJson } from '@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicViewJson';

export class SimpleTextView extends PlatformView {
private viewId: number;
private model: DVModel;

constructor(context: common.Context, viewId: number, args: ESObject) {
super()
this.viewId = viewId;
this.model = this.createDVModel();
}

private createDVModel(): DVModel {
return createDVModelFromJson(
{
compType: "Column",
attributes: {
backgroundColor: '#F1F3F5',
height: '100%',
width: '100%'
},
children: [
{
compType: "Text",
attributes: {
value: "PlatformView: #" + this.viewId.toString(),
fontSize: 18,
},
},
{
compType: "Text",
attributes: {
value: "Hi, there! I'm from ArkUI.",
fontColor: '#0D9FFB',
fontSize: 18,
marginTop: 20,
},
}
],
}
);
}

getView(): DVModel {
return this.model;
}

dispose(): void {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import common from '@ohos.app.ability.common';
import {
PlatformView,
PlatformViewFactory,
StandardMessageCodec
} from '@ohos/flutter_ohos';
import { SimpleTextView } from './SimpleTextView';

export class SimpleTextViewFactory extends PlatformViewFactory {
constructor() {
super(StandardMessageCodec.INSTANCE);
}

public create(context: common.Context, viewId: number, args: Object): PlatformView {
return new SimpleTextView(context, viewId, args);
}
}

0 comments on commit a28b4f7

Please sign in to comment.