From 21f4838f415498f4f36f00e1672aa55036a0e0af Mon Sep 17 00:00:00 2001 From: Daniel Kao Date: Mon, 27 Jul 2020 17:52:37 +0800 Subject: [PATCH] use PlatformViewLink widget for Android WebView --- .../android/app/src/main/AndroidManifest.xml | 6 ++ lib/src/in_app_webview.dart | 61 +++++++++---------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index b6bd289b3..fefd2becc 100755 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -85,5 +85,11 @@ android:resource="@xml/provider_paths"/> + + + + diff --git a/lib/src/in_app_webview.dart b/lib/src/in_app_webview.dart index c031c3f3a..19462ba97 100755 --- a/lib/src/in_app_webview.dart +++ b/lib/src/in_app_webview.dart @@ -3,6 +3,7 @@ import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/gestures.dart'; @@ -339,41 +340,37 @@ class _InAppWebViewState extends State { @override Widget build(BuildContext context) { if (defaultTargetPlatform == TargetPlatform.android) { - return AndroidView( + return PlatformViewLink( viewType: 'com.pichillilorenzo/flutter_inappwebview', - onPlatformViewCreated: _onPlatformViewCreated, - gestureRecognizers: widget.gestureRecognizers, - layoutDirection: TextDirection.rtl, - creationParams: { - 'initialUrl': '${Uri.parse(widget.initialUrl)}', - 'initialFile': widget.initialFile, - 'initialData': widget.initialData?.toMap(), - 'initialHeaders': widget.initialHeaders, - 'initialOptions': widget.initialOptions?.toMap() ?? {}, - 'contextMenu': widget.contextMenu?.toMap() ?? {}, - 'windowId': widget.windowId + surfaceFactory: ( + BuildContext context, + PlatformViewController controller, + ) { + return AndroidViewSurface( + controller: controller, + gestureRecognizers: widget.gestureRecognizers ?? const >{}, + hitTestBehavior: PlatformViewHitTestBehavior.opaque, + ); + }, + onCreatePlatformView: (PlatformViewCreationParams params) { + return PlatformViewsService.initSurfaceAndroidView( + id: params.id, + viewType: 'com.pichillilorenzo/flutter_inappwebview', + layoutDirection: TextDirection.rtl, + creationParams: { + 'initialUrl': '${Uri.parse(widget.initialUrl)}', + 'initialFile': widget.initialFile, + 'initialData': widget.initialData?.toMap(), + 'initialHeaders': widget.initialHeaders, + 'initialOptions': widget.initialOptions?.toMap() ?? {} + }, + creationParamsCodec: const StandardMessageCodec(), + ) + ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated) + ..addOnPlatformViewCreatedListener((id) => _onPlatformViewCreated(id)) + ..create(); }, - creationParamsCodec: const StandardMessageCodec(), ); - // onLongPress issue: https://github.com/flutter/plugins/blob/f31d16a6ca0c4bd6849cff925a00b6823973696b/packages/webview_flutter/lib/src/webview_android.dart#L31 - /*return GestureDetector( - onLongPress: () {}, - excludeFromSemantics: true, - child: AndroidView( - viewType: 'com.pichillilorenzo/flutter_inappwebview', - onPlatformViewCreated: _onPlatformViewCreated, - gestureRecognizers: widget.gestureRecognizers, - layoutDirection: TextDirection.rtl, - creationParams: { - 'initialUrl': '${Uri.parse(widget.initialUrl)}', - 'initialFile': widget.initialFile, - 'initialData': widget.initialData?.toMap(), - 'initialHeaders': widget.initialHeaders, - 'initialOptions': initialOptions - }, - creationParamsCodec: const StandardMessageCodec(), - ), - );*/ } else if (defaultTargetPlatform == TargetPlatform.iOS) { return UiKitView( viewType: 'com.pichillilorenzo/flutter_inappwebview',