Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit dfb6d63

Browse files
committed
put webview with transparent background
fix flutter/flutter#29300
1 parent e8c6a86 commit dfb6d63

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import android.annotation.TargetApi;
88
import android.content.Context;
9+
import android.graphics.Color;
910
import android.os.Build;
1011
import android.view.View;
1112
import android.webkit.WebStorage;
@@ -29,6 +30,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
2930
@SuppressWarnings("unchecked")
3031
FlutterWebView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {
3132
webView = new WebView(context);
33+
webView.setBackgroundColor(Color.TRANSPARENT);
3234
// Allow local storage.
3335
webView.getSettings().setDomStorageEnabled(true);
3436

@@ -99,6 +101,7 @@ public void onMethodCall(MethodCall methodCall, Result result) {
99101

100102
private void loadUrl(MethodCall methodCall, Result result) {
101103
String url = (String) methodCall.arguments;
104+
webView.setBackgroundColor(Color.TRANSPARENT);
102105
webView.loadUrl(url);
103106
result.success(null);
104107
}

packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebViewClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private boolean shouldOverrideUrlLoading(WebView view, String url) {
6969
private void onPageFinished(WebView view, String url) {
7070
Map<String, Object> args = new HashMap<>();
7171
args.put("url", url);
72+
view.setBackgroundColor(0x00000000);
7273
methodChannel.invokeMethod("onPageFinished", args);
7374
}
7475

packages/webview_flutter/example/lib/main.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import 'package:webview_flutter/webview_flutter.dart';
1010
void main() => runApp(MaterialApp(home: WebViewExample()));
1111

1212
const String kNavigationExamplePage = '''
13-
<!DOCTYPE html><html>
13+
<!DOCTYPE html><html style="background-color:transparent;">
1414
<head><title>Navigation Delegate Example</title></head>
15-
<body>
15+
<body style="background-color:transparent;">
1616
<p>
17-
The navigation delegate is set to block navigation to the youtube website.
17+
The navigation delegate is set to block navigation to the youtube website, and as the webview background is transparent, you can see scaffold background.
1818
</p>
1919
<ul>
2020
<ul><a href="https://www.youtube.com/">https://www.youtube.com/</a></ul>
@@ -31,6 +31,7 @@ class WebViewExample extends StatelessWidget {
3131
@override
3232
Widget build(BuildContext context) {
3333
return Scaffold(
34+
backgroundColor: Colors.green,
3435
appBar: AppBar(
3536
title: const Text('Flutter WebView example'),
3637
// This drop down menu demonstrates that Flutter widgets can be shown over the web view.

packages/webview_flutter/ios/Classes/FlutterWebView.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ - (instancetype)initWithFrame:(CGRect)frame
6666
configuration.userContentController = userContentController;
6767

6868
_webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
69+
_webView.opaque = NO;
70+
_webView.backgroundColor = UIColor.clearColor;
71+
_webView.scrollView.backgroundColor = UIColor.clearColor;
72+
6973
_navigationDelegate = [[FLTWKNavigationDelegate alloc] initWithChannel:_channel];
7074
_webView.navigationDelegate = _navigationDelegate;
7175
__weak __typeof__(self) weakSelf = self;

0 commit comments

Comments
 (0)