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

Commit c01c5b0

Browse files
committed
put webview with transparent background
fix flutter/flutter#29300
1 parent 432f775 commit c01c5b0

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-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;
@@ -28,6 +29,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
2829
@SuppressWarnings("unchecked")
2930
FlutterWebView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {
3031
webView = new WebView(context);
32+
webView.setBackgroundColor(Color.TRANSPARENT);
3133
// Allow local storage.
3234
webView.getSettings().setDomStorageEnabled(true);
3335

@@ -100,6 +102,7 @@ public void onMethodCall(MethodCall methodCall, Result result) {
100102

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
7070
return true;
7171
}
7272

73+
@Override
74+
public void onPageFinished(WebView view, String url)
75+
{
76+
view.setBackgroundColor(0x00000000);
77+
}
78+
7379
private void notifyOnNavigationRequest(
7480
String url, Map<String, String> headers, WebView webview, boolean isMainFrame) {
7581
HashMap<String, Object> args = new HashMap<>();

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)