diff --git a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/OhosWebView.ets b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/OhosWebView.ets index 449b13b190bc7fb96c4c402c83261947eb7aee27..20660a3ad1750ac7837903874cdcc77673b00ee4 100644 --- a/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/OhosWebView.ets +++ b/flutter_inappwebview_ohos/ohos/src/main/ets/components/plugin/webview/in_app_webview/OhosWebView.ets @@ -21,6 +21,7 @@ import { FlutterWebView } from './FlutterWebView' import InAppWebView from './InAppWebView' import EventConstant from '../../EventConstant' import { JSON } from '@kit.ArkTS'; +import { KeyboardAvoidMode, window } from '@kit.ArkUI'; @Component export struct OhosWebView { @@ -31,6 +32,8 @@ export struct OhosWebView { @State isEnableRefresh: boolean = false @State startScripts: Array = []; @State cacheEnabled: boolean = true + @State webViewHeight: number | string = '100%'; + inheritHeight: Length | undefined; aboutToAppear(): void { this.isEnableRefresh = this.inAppWebView!.getPullToRefreshLayout().settings.enabled; @@ -48,14 +51,43 @@ export struct OhosWebView { context.eventHub.on(EventConstant.EVENT_UPDATE_CACHEENABLE, (enable : boolean) => { this.cacheEnabled = enable; }); + + window.getLastWindow(getContext(this)).then(currentWindow => { + // 监视软键盘的弹出和收起 + currentWindow.on('avoidAreaChange', (data) => { + let property = currentWindow.getWindowProperties(); + let avoidArea = currentWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD); + // this.context.area. + // this.screenHeight = px2vp(property.windowRect.height - avoidArea.bottomRect.height); + if( this.inheritHeight != undefined ) { + let maxBoundingHeight = px2vp(property.windowRect.height - avoidArea.bottomRect.height) // 转为的是px单位 + + if( this.inheritHeight <= maxBoundingHeight ) { + this.webViewHeight = '100%'; + } else { + // maxBoundingHeight; // vp2px(maxBoundingHeight);// ((maxBoundingHeight / px2vp(property.windowRect.height)) * 100) + "%"; + // this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.OFFSET) + this.webViewHeight = px2vp(property.windowRect.height); // 触发产生布局刷新; + } + } + }); + }) } aboutToDisappear(): void { + this.dispose(); let context = getContext() as common.UIAbilityContext; context.eventHub.off(EventConstant.EVENT_PULL_SETREFRESHING); context.eventHub.off(EventConstant.EVENT_UPDATE_STARTSCRIPTS); } + dispose(): void { + window.getLastWindow(getContext(this)).then(currentWindow => { + // 关闭-监视软键盘的弹出和收起 + currentWindow.off('avoidAreaChange'); + }) + } + build() { Column() { if (this.isEnableRefresh) { @@ -71,7 +103,10 @@ export struct OhosWebView { } else { this.buildWeb() } - } + }.height('100%').onSizeChange((oldSize, newSize) => { // NOTE: 获取并记录继承的高度 + console.log("newSize height:" + newSize.height); + this.inheritHeight = newSize.height; + }) } @Builder @@ -173,6 +208,7 @@ export struct OhosWebView { .layoutMode(this.inAppWebView!.customSettings.layoutMode) .enableNativeEmbedMode(this.inAppWebView!.customSettings.enableNativeEmbedMode) .javaScriptOnDocumentStart(this.startScripts) + .height(this.webViewHeight) } }