Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AndroidInAppWebViewOption for using Hybrid Composition #1

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ Instead, on the `onLoadStop` WebView event, you can use `callHandler` directly:

##### `InAppWebView` Android-specific options

* `useHybridComposition`: Set to `true` to use Flutter's new Hybrid Composition rendering method, which fixes all issues [here](https://github.com/flutter/flutter/issues/61133). The default value is `false`. Note that this option requires Flutter v1.20+ and should only be used on Android 10+ for release apps, as animations will drop frames on < Android 10.
* `useShouldInterceptRequest`: Set to `true` to be able to listen at the `androidShouldInterceptRequest` event. The default value is `false`.
* `useOnRenderProcessGone`: Set to `true` to be able to listen at the `androidOnRenderProcessGone` event. The default value is `false`.
* `textZoom`: Sets the text zoom of the page in percent. The default value is `100`.
Expand Down
4 changes: 3 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<activity android:theme="@style/AppTheme" android:name="com.pichillilorenzo.flutter_inappwebview.InAppBrowser.InAppBrowserActivity" android:configChanges="orientation|screenSize"></activity>
<activity android:theme="@style/ThemeTransparent" android:name="com.pichillilorenzo.flutter_inappwebview.ChromeCustomTabs.ChromeCustomTabsActivity" android:configChanges="orientation|screenSize"></activity>
<receiver android:name="com.pichillilorenzo.flutter_inappwebview.ChromeCustomTabs.ActionBroadcastReceiver" />
<meta-data
android:name="io.flutter.embedded_views_preview"
android:value="true" />
</application>

</manifest>
81 changes: 50 additions & 31 deletions lib/src/in_app_webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -340,37 +340,56 @@ class _InAppWebViewState extends State<InAppWebView> {
@override
Widget build(BuildContext context) {
if (defaultTargetPlatform == TargetPlatform.android) {
return PlatformViewLink(
viewType: 'com.pichillilorenzo/flutter_inappwebview',
surfaceFactory: (
BuildContext context,
PlatformViewController controller,
) {
return AndroidViewSurface(
controller: controller,
gestureRecognizers: widget.gestureRecognizers ?? const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: 'com.pichillilorenzo/flutter_inappwebview',
layoutDirection: TextDirection.rtl,
creationParams: <String, dynamic>{
'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();
},
);
if (widget.initialOptions.android.useHybridComposition) {
return PlatformViewLink(
viewType: 'com.pichillilorenzo/flutter_inappwebview',
surfaceFactory: (
BuildContext context,
PlatformViewController controller,
) {
return AndroidViewSurface(
controller: controller,
gestureRecognizers: widget.gestureRecognizers ?? const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: 'com.pichillilorenzo/flutter_inappwebview',
layoutDirection: TextDirection.rtl,
creationParams: <String, dynamic>{
'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();
},
);
} else {
return AndroidView(
viewType: 'com.pichillilorenzo/flutter_inappwebview',
onPlatformViewCreated: _onPlatformViewCreated,
gestureRecognizers: widget.gestureRecognizers,
layoutDirection: TextDirection.rtl,
creationParams: <String, dynamic>{
'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
},
creationParamsCodec: const StandardMessageCodec(),
);
}
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: 'com.pichillilorenzo/flutter_inappwebview',
Expand Down
8 changes: 8 additions & 0 deletions lib/src/webview_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ class AndroidInAppWebViewOptions
///If the url request of a subframe matches the regular expression, then the request of that subframe is canceled.
String regexToCancelSubFramesLoading;

///Set to `true` to enable Flutter's new Hybrid Composition. The default value is `false`.
///Hybrid Composition is supported starting with Flutter v1.20.
///**NOTE**: It is recommended to use Hybrid Composition only on Android 10+ for a release app, as it can cause framerate drops on animations in Android 9 and lower.
bool useHybridComposition;

///Set to `true` to be able to listen at the [WebView.androidShouldInterceptRequest] event. The default value is `false`.
bool useShouldInterceptRequest;

Expand Down Expand Up @@ -556,6 +561,7 @@ class AndroidInAppWebViewOptions
this.initialScale = 0,
this.supportMultipleWindows = false,
this.regexToCancelSubFramesLoading,
this.useHybridComposition = false,
this.useShouldInterceptRequest = false,
this.useOnRenderProcessGone = false,
this.overScrollMode = AndroidOverScrollMode.OVER_SCROLL_IF_CONTENT_SCROLLS,
Expand Down Expand Up @@ -613,6 +619,7 @@ class AndroidInAppWebViewOptions
"thirdPartyCookiesEnabled": thirdPartyCookiesEnabled,
"hardwareAcceleration": hardwareAcceleration,
"supportMultipleWindows": supportMultipleWindows,
"useHybridComposition": useHybridComposition,
"regexToCancelSubFramesLoading": regexToCancelSubFramesLoading,
"useShouldInterceptRequest": useShouldInterceptRequest,
"useOnRenderProcessGone": useOnRenderProcessGone,
Expand Down Expand Up @@ -676,6 +683,7 @@ class AndroidInAppWebViewOptions
options.supportMultipleWindows = map["supportMultipleWindows"];
options.regexToCancelSubFramesLoading =
map["regexToCancelSubFramesLoading"];
options.useHybridComposition = map["useHybridComposition"];
options.useShouldInterceptRequest = map["useShouldInterceptRequest"];
options.useOnRenderProcessGone = map["useOnRenderProcessGone"];
options.overScrollMode =
Expand Down