Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
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
4 changes: 4 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.3

* Add common support for Android WebView initializing(`FlutterWebView.setCommonIniter()`) [flutter/issues/70731](https://github.com/flutter/flutter/issues/70731).

## 2.0.2

* Fixes bug where text fields are hidden behind the keyboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@

public class FlutterWebView implements PlatformView, MethodCallHandler {
private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames";
private static FlutterWebViewIniter commonIniter;

/**
* Set common initializing logic for all Flutter WebView's. When the WebView is created and the
* original initialization is done, {@link FlutterWebViewIniter#initWebView(WebView)} would be
* called.
*
* <p>Suggestion: you can call this method in {@link android.app.Application} to avoid memory
* leak.
*/
public static void setCommonIniter(FlutterWebViewIniter initer) {
FlutterWebView.commonIniter = initer;
}

private final WebView webView;
private final MethodChannel methodChannel;
private final FlutterWebViewClient flutterWebViewClient;
Expand Down Expand Up @@ -128,6 +142,7 @@ public void onProgressChanged(WebView view, int progress) {
String userAgent = (String) params.get("userAgent");
updateUserAgent(userAgent);
}
if (commonIniter != null) commonIniter.initWebView(webView);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At present, websettings lacks many functions, and its behavior is quite different from that of IOS. It is recommended to agree to the change of PR and wait until webview_flutter had all these settings are satisfied, the initWebView method can be deprecated.

if (params.containsKey("initialUrl")) {
String url = (String) params.get("initialUrl");
webView.loadUrl(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.webviewflutter;

import android.webkit.WebView;

/**
* Interface to initialize Flutter WebView in native Android code.
*
* <p>Why? In most cases, an APP has custom COMMON WebView initialization logic, like {@link
* android.webkit.WebSettings#setBuiltInZoomControls(boolean)}, {@link
* android.webkit.WebSettings#setMixedContentMode(int)}, {@link
* android.webkit.WebSettings#setTextZoom(int)} etc.
*
* <p>Adding all these settings to Flutter WebView params is tedious, and unnecessary mostly, so we
* can put these initialization on our APP's self native code as you like.
*/
public interface FlutterWebViewIniter {
/** How to initialize WebView */
void initWebView(WebView webView);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<application
android:icon="@mipmap/ic_launcher"
android:label="webview_flutter_example"
android:name="io.flutter.app.FlutterApplication">
android:name=".App">
<meta-data
android:name="flutterEmbedding"
android:value="2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.flutter.plugins.webviewflutterexample;

import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;
import io.flutter.app.FlutterApplication;
import io.flutter.plugins.webviewflutter.FlutterWebView;
import io.flutter.plugins.webviewflutter.FlutterWebViewIniter;

public class App extends FlutterApplication implements FlutterWebViewIniter {
@Override
public void onCreate() {
super.onCreate();

FlutterWebView.setCommonIniter(this);
}

@Override
public void initWebView(WebView webView) {
WebSettings settings = webView.getSettings();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
}
}
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter
version: 2.0.2
version: 2.0.3

environment:
sdk: ">=2.12.0-259.9.beta <3.0.0"
Expand Down