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

Commit 69b5562

Browse files
committed
[webview] add option to set the background to transparent
1 parent 7d4a918 commit 69b5562

File tree

7 files changed

+24
-3
lines changed

7 files changed

+24
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
4242
DisplayManager displayManager =
4343
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
4444
displayListenerProxy.onPreWebViewInitialization(displayManager);
45-
webView = new InputAwareWebView(context, containerView);
45+
boolean opaque = (boolean) params.get("opaque");
46+
webView = new InputAwareWebView(context, containerView, opaque);
4647
displayListenerProxy.onPostWebViewInitialization(displayManager);
4748

4849
platformThreadHandler = new Handler(context.getMainLooper());

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ final class InputAwareWebView extends WebView {
3131
private ThreadedInputConnectionProxyAdapterView proxyAdapterView;
3232
private View containerView;
3333

34-
InputAwareWebView(Context context, View containerView) {
34+
InputAwareWebView(Context context, View containerView, boolean opaque) {
3535
super(context);
3636
this.containerView = containerView;
37+
if (!opaque) {
38+
setBackgroundColor(0x00000000);
39+
}
3740
}
3841

3942
void setContainerView(View containerView) {

packages/webview_flutter/example/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const String kNavigationExamplePage = '''
1616
<head><title>Navigation Delegate Example</title></head>
1717
<body>
1818
<p>
19-
The navigation delegate is set to block navigation to the youtube website.
19+
The navigation delegate is set to block navigation to the youtube website, and as the webview background is transparent, you can see scaffold background.
2020
</p>
2121
<ul>
2222
<ul><a href="https://www.youtube.com/">https://www.youtube.com/</a></ul>
@@ -38,6 +38,7 @@ class _WebViewExampleState extends State<WebViewExample> {
3838
@override
3939
Widget build(BuildContext context) {
4040
return Scaffold(
41+
backgroundColor: Colors.green,
4142
appBar: AppBar(
4243
title: const Text('Flutter WebView example'),
4344
// This drop down menu demonstrates that Flutter widgets can be shown over the web view.
@@ -75,6 +76,7 @@ class _WebViewExampleState extends State<WebViewExample> {
7576
print('Page finished loading: $url');
7677
},
7778
gestureNavigationEnabled: true,
79+
opaque: false,
7880
);
7981
}),
8082
floatingActionButton: favoriteButton(),

packages/webview_flutter/ios/Classes/FlutterWebView.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ - (instancetype)initWithFrame:(CGRect)frame
9292
inConfiguration:configuration];
9393

9494
_webView = [[FLTWKWebView alloc] initWithFrame:frame configuration:configuration];
95+
if (![args[@"opaque"] boolValue]) {
96+
_webView.opaque = NO;
97+
_webView.backgroundColor = UIColor.clearColor;
98+
_webView.scrollView.backgroundColor = UIColor.clearColor;
99+
}
95100
_navigationDelegate = [[FLTWKNavigationDelegate alloc] initWithChannel:_channel];
96101
_webView.UIDelegate = self;
97102
_webView.navigationDelegate = _navigationDelegate;

packages/webview_flutter/lib/platform_interface.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class CreationParams {
432432
this.userAgent,
433433
this.autoMediaPlaybackPolicy =
434434
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
435+
this.opaque,
435436
}) : assert(autoMediaPlaybackPolicy != null);
436437

437438
/// The initialUrl to load in the webview.
@@ -465,6 +466,9 @@ class CreationParams {
465466
/// Which restrictions apply on automatic media playback.
466467
final AutoMediaPlaybackPolicy autoMediaPlaybackPolicy;
467468

469+
/// If set to `false`, the webview background will be transparent.
470+
final bool opaque;
471+
468472
@override
469473
String toString() {
470474
return '$runtimeType(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent)';

packages/webview_flutter/lib/src/webview_method_channel.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
197197
'javascriptChannelNames': creationParams.javascriptChannelNames.toList(),
198198
'userAgent': creationParams.userAgent,
199199
'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index,
200+
'opaque': creationParams.opaque,
200201
};
201202
}
202203
}

packages/webview_flutter/lib/webview_flutter.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class WebView extends StatefulWidget {
156156
this.userAgent,
157157
this.initialMediaPlaybackPolicy =
158158
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
159+
this.opaque = true,
159160
}) : assert(javascriptMode != null),
160161
assert(initialMediaPlaybackPolicy != null),
161162
super(key: key);
@@ -329,6 +330,9 @@ class WebView extends StatefulWidget {
329330
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
330331
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;
331332

333+
/// If set to `false`, the webview background will be transparent.
334+
final bool opaque;
335+
332336
@override
333337
State<StatefulWidget> createState() => _WebViewState();
334338
}
@@ -393,6 +397,7 @@ CreationParams _creationParamsfromWidget(WebView widget) {
393397
javascriptChannelNames: _extractChannelNames(widget.javascriptChannels),
394398
userAgent: widget.userAgent,
395399
autoMediaPlaybackPolicy: widget.initialMediaPlaybackPolicy,
400+
opaque: widget.opaque,
396401
);
397402
}
398403

0 commit comments

Comments
 (0)