Adds a transparentBackground option for iOS and Android

This is useful to prevent a brief white flash on initialization
for apps that use a dark theme.
This commit is contained in:
Matthew Lloyd 2019-04-27 03:50:45 -04:00
parent 11da457d51
commit 89d2e0b56b
6 changed files with 15 additions and 1 deletions

View File

@ -240,6 +240,7 @@ All platforms support:
- __javaScriptEnabled__: Set to `true` to enable JavaScript. The default value is `true`. - __javaScriptEnabled__: Set to `true` to enable JavaScript. The default value is `true`.
- __javaScriptCanOpenWindowsAutomatically__: Set to `true` to allow JavaScript open windows without user interaction. The default value is `false`. - __javaScriptCanOpenWindowsAutomatically__: Set to `true` to allow JavaScript open windows without user interaction. The default value is `false`.
- __mediaPlaybackRequiresUserGesture__: Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`. - __mediaPlaybackRequiresUserGesture__: Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`.
- __transparentBackground__: Set to `true` to make the background of the WebView transparent. If your app has a dark theme, this can prevent a white flash on initialization. The default value is `false`.
**Android** supports these additional options: **Android** supports these additional options:

View File

@ -3,6 +3,7 @@ package com.pichillilorenzo.flutter_inappbrowser.InAppWebView;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Picture; import android.graphics.Picture;
import android.os.Build; import android.os.Build;
import android.util.AttributeSet; import android.util.AttributeSet;
@ -183,6 +184,10 @@ public class InAppWebView extends WebView {
settings.setUseWideViewPort(options.useWideViewPort); settings.setUseWideViewPort(options.useWideViewPort);
settings.setSupportZoom(options.supportZoom); settings.setSupportZoom(options.supportZoom);
settings.setTextZoom(options.textZoom); settings.setTextZoom(options.textZoom);
if (options.transparentBackground) {
setBackgroundColor(Color.TRANSPARENT);
}
} }
public void loadUrl(String url, MethodChannel.Result result) { public void loadUrl(String url, MethodChannel.Result result) {

View File

@ -22,5 +22,5 @@ public class InAppWebViewOptions extends Options {
public boolean domStorageEnabled = false; public boolean domStorageEnabled = false;
public boolean useWideViewPort = true; public boolean useWideViewPort = true;
public boolean safeBrowsingEnabled = true; public boolean safeBrowsingEnabled = true;
public boolean transparentBackground = false;
} }

View File

@ -113,6 +113,12 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
configuration.userContentController = WKUserContentController() configuration.userContentController = WKUserContentController()
configuration.preferences = WKPreferences() configuration.preferences = WKPreferences()
if (options?.transparentBackground)! {
isOpaque = false
backgroundColor = UIColor.clear
scrollView.backgroundColor = UIColor.clear
}
// prevent webView from bouncing // prevent webView from bouncing
if (options?.disallowOverScroll)! { if (options?.disallowOverScroll)! {
if responds(to: #selector(getter: scrollView)) { if responds(to: #selector(getter: scrollView)) {

View File

@ -28,6 +28,7 @@ public class InAppWebViewOptions: Options {
var ignoresViewportScaleLimits = false var ignoresViewportScaleLimits = false
var allowsInlineMediaPlayback = false var allowsInlineMediaPlayback = false
var allowsPictureInPictureMediaPlayback = true var allowsPictureInPictureMediaPlayback = true
var transparentBackground = false
override init(){ override init(){
super.init() super.init()

View File

@ -171,6 +171,7 @@ class InAppBrowser {
/// - __toolbarTopBackgroundColor__: Set the custom background color of the toolbar at the top. /// - __toolbarTopBackgroundColor__: Set the custom background color of the toolbar at the top.
/// - __hideUrlBar__: Set to `true` to hide the url bar on the toolbar at the top. The default value is `false`. /// - __hideUrlBar__: Set to `true` to hide the url bar on the toolbar at the top. The default value is `false`.
/// - __mediaPlaybackRequiresUserGesture__: Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`. /// - __mediaPlaybackRequiresUserGesture__: Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`.
/// - __transparentBackground__: Set to `true` to make the background of the WebView transparent. If your app has a dark theme, this can prevent a white flash on initialization. The default value is `false`.
/// ///
/// - **Android** supports these additional options: /// - **Android** supports these additional options:
/// ///