From 89d2e0b56b1ddc37934d6f7892f5663bdc0dcda3 Mon Sep 17 00:00:00 2001 From: Matthew Lloyd Date: Sat, 27 Apr 2019 03:50:45 -0400 Subject: [PATCH] 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. --- README.md | 1 + .../flutter_inappbrowser/InAppWebView/InAppWebView.java | 5 +++++ .../InAppWebView/InAppWebViewOptions.java | 2 +- ios/Classes/InAppWebView.swift | 6 ++++++ ios/Classes/InAppWebViewOptions.swift | 1 + lib/flutter_inappbrowser.dart | 1 + 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3898a649..d357d9be 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,7 @@ All platforms support: - __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`. - __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: diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java index 235a43d0..568df3ad 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java @@ -3,6 +3,7 @@ package com.pichillilorenzo.flutter_inappbrowser.InAppWebView; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.Picture; import android.os.Build; import android.util.AttributeSet; @@ -183,6 +184,10 @@ public class InAppWebView extends WebView { settings.setUseWideViewPort(options.useWideViewPort); settings.setSupportZoom(options.supportZoom); settings.setTextZoom(options.textZoom); + + if (options.transparentBackground) { + setBackgroundColor(Color.TRANSPARENT); + } } public void loadUrl(String url, MethodChannel.Result result) { diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewOptions.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewOptions.java index 5410e7ec..550a90e5 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewOptions.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewOptions.java @@ -22,5 +22,5 @@ public class InAppWebViewOptions extends Options { public boolean domStorageEnabled = false; public boolean useWideViewPort = true; public boolean safeBrowsingEnabled = true; - + public boolean transparentBackground = false; } diff --git a/ios/Classes/InAppWebView.swift b/ios/Classes/InAppWebView.swift index 31b1a5bf..961c699d 100644 --- a/ios/Classes/InAppWebView.swift +++ b/ios/Classes/InAppWebView.swift @@ -113,6 +113,12 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi configuration.userContentController = WKUserContentController() configuration.preferences = WKPreferences() + if (options?.transparentBackground)! { + isOpaque = false + backgroundColor = UIColor.clear + scrollView.backgroundColor = UIColor.clear + } + // prevent webView from bouncing if (options?.disallowOverScroll)! { if responds(to: #selector(getter: scrollView)) { diff --git a/ios/Classes/InAppWebViewOptions.swift b/ios/Classes/InAppWebViewOptions.swift index e9b5e6f3..dfaf0eb3 100644 --- a/ios/Classes/InAppWebViewOptions.swift +++ b/ios/Classes/InAppWebViewOptions.swift @@ -28,6 +28,7 @@ public class InAppWebViewOptions: Options { var ignoresViewportScaleLimits = false var allowsInlineMediaPlayback = false var allowsPictureInPictureMediaPlayback = true + var transparentBackground = false override init(){ super.init() diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart index b03db32b..95bdef7c 100644 --- a/lib/flutter_inappbrowser.dart +++ b/lib/flutter_inappbrowser.dart @@ -171,6 +171,7 @@ class InAppBrowser { /// - __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`. /// - __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: ///