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

Commit add3545

Browse files
committed
[webview_flutter] add option to set the background to transparent
1 parent c8e3aa1 commit add3545

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
@@ -87,7 +87,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
8787
DisplayManager displayManager =
8888
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
8989
displayListenerProxy.onPreWebViewInitialization(displayManager);
90-
webView = new InputAwareWebView(context, containerView);
90+
boolean opaque = (boolean) params.get("opaque");
91+
webView = new InputAwareWebView(context, containerView, opaque);
9192
displayListenerProxy.onPostWebViewInitialization(displayManager);
9293

9394
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
@@ -17,7 +17,7 @@ const String kNavigationExamplePage = '''
1717
<head><title>Navigation Delegate Example</title></head>
1818
<body>
1919
<p>
20-
The navigation delegate is set to block navigation to the youtube website.
20+
The navigation delegate is set to block navigation to the youtube website, and as the webview background is transparent, you can see scaffold background.
2121
</p>
2222
<ul>
2323
<ul><a href="https://www.youtube.com/">https://www.youtube.com/</a></ul>
@@ -45,6 +45,7 @@ class _WebViewExampleState extends State<WebViewExample> {
4545
@override
4646
Widget build(BuildContext context) {
4747
return Scaffold(
48+
backgroundColor: Colors.green,
4849
appBar: AppBar(
4950
title: const Text('Flutter WebView example'),
5051
// This drop down menu demonstrates that Flutter widgets can be shown over the web view.
@@ -82,6 +83,7 @@ class _WebViewExampleState extends State<WebViewExample> {
8283
print('Page finished loading: $url');
8384
},
8485
gestureNavigationEnabled: true,
86+
opaque: false,
8587
);
8688
}),
8789
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
@@ -219,6 +219,7 @@ class WebView extends StatefulWidget {
219219
this.userAgent,
220220
this.initialMediaPlaybackPolicy =
221221
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
222+
this.opaque = true,
222223
}) : assert(javascriptMode != null),
223224
assert(initialMediaPlaybackPolicy != null),
224225
super(key: key);
@@ -392,6 +393,9 @@ class WebView extends StatefulWidget {
392393
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
393394
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;
394395

396+
/// If set to `false`, the webview background will be transparent.
397+
final bool opaque;
398+
395399
@override
396400
State<StatefulWidget> createState() => _WebViewState();
397401
}
@@ -456,6 +460,7 @@ CreationParams _creationParamsfromWidget(WebView widget) {
456460
javascriptChannelNames: _extractChannelNames(widget.javascriptChannels),
457461
userAgent: widget.userAgent,
458462
autoMediaPlaybackPolicy: widget.initialMediaPlaybackPolicy,
463+
opaque: widget.opaque,
459464
);
460465
}
461466

0 commit comments

Comments
 (0)