From cdad5427d669d195e618761b3fa2be101314d2e7 Mon Sep 17 00:00:00 2001 From: aengu Date: Thu, 18 Jul 2024 23:28:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E4=BB=8E=E8=BE=93=E5=85=A5=E6=A1=86?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E6=96=87=E5=AD=97=E4=B9=8B=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E7=A2=B0=E8=A7=A6=E6=8C=89=E9=92=AE=E6=88=96?= =?UTF-8?q?=E7=A9=BA=E7=99=BD=E5=8C=BA=E5=9F=9F=E6=97=B6=EF=BC=8C=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E9=94=AE=E7=9B=98=E6=B6=88=E5=A4=B1=E5=90=8E=EF=BC=8C?= =?UTF-8?q?webview=E7=9A=84=E9=AB=98=E5=BA=A6=E5=8D=B4=E8=BF=98=E6=98=AF?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=81=A2=E5=A4=8D=E5=8E=9F=E6=9C=89=E7=9A=84?= =?UTF-8?q?=E9=AB=98=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: aengu --- .../webview/in_app_webview/OhosWebView.ets | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) 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 449b13b1..20660a3a 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) } } -- Gitee