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

[webview_flutter] add option to set the background to transparent #2895

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
DisplayManager displayManager =
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
displayListenerProxy.onPreWebViewInitialization(displayManager);
webView = new InputAwareWebView(context, containerView);
boolean opaque = (boolean) params.get("opaque");
webView = new InputAwareWebView(context, containerView, opaque);
displayListenerProxy.onPostWebViewInitialization(displayManager);

platformThreadHandler = new Handler(context.getMainLooper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ final class InputAwareWebView extends WebView {
private ThreadedInputConnectionProxyAdapterView proxyAdapterView;
private View containerView;

InputAwareWebView(Context context, View containerView) {
InputAwareWebView(Context context, View containerView, boolean opaque) {
super(context);
this.containerView = containerView;
if (!opaque) {
setBackgroundColor(0x00000000);
}
}

void setContainerView(View containerView) {
Expand Down
4 changes: 3 additions & 1 deletion packages/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const String kNavigationExamplePage = '''
<head><title>Navigation Delegate Example</title></head>
<body>
<p>
The navigation delegate is set to block navigation to the youtube website.
The navigation delegate is set to block navigation to the youtube website, and as the webview background is transparent, you can see scaffold background.
</p>
<ul>
<ul><a href="https://www.youtube.com/">https://www.youtube.com/</a></ul>
Expand Down Expand Up @@ -45,6 +45,7 @@ class _WebViewExampleState extends State<WebViewExample> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(
title: const Text('Flutter WebView example'),
// This drop down menu demonstrates that Flutter widgets can be shown over the web view.
Expand Down Expand Up @@ -82,6 +83,7 @@ class _WebViewExampleState extends State<WebViewExample> {
print('Page finished loading: $url');
},
gestureNavigationEnabled: true,
opaque: false,
);
}),
floatingActionButton: favoriteButton(),
Expand Down
5 changes: 5 additions & 0 deletions packages/webview_flutter/ios/Classes/FlutterWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ - (instancetype)initWithFrame:(CGRect)frame
inConfiguration:configuration];

_webView = [[FLTWKWebView alloc] initWithFrame:frame configuration:configuration];
if (![args[@"opaque"] boolValue]) {
_webView.opaque = NO;
_webView.backgroundColor = UIColor.clearColor;
_webView.scrollView.backgroundColor = UIColor.clearColor;
}
_navigationDelegate = [[FLTWKNavigationDelegate alloc] initWithChannel:_channel];
_webView.UIDelegate = self;
_webView.navigationDelegate = _navigationDelegate;
Expand Down
4 changes: 4 additions & 0 deletions packages/webview_flutter/lib/platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class CreationParams {
this.userAgent,
this.autoMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.opaque,
}) : assert(autoMediaPlaybackPolicy != null);

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

/// If set to `false`, the webview background will be transparent.
final bool opaque;

@override
String toString() {
return '$runtimeType(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent)';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
'javascriptChannelNames': creationParams.javascriptChannelNames.toList(),
'userAgent': creationParams.userAgent,
'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index,
'opaque': creationParams.opaque,
};
}
}
5 changes: 5 additions & 0 deletions packages/webview_flutter/lib/webview_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class WebView extends StatefulWidget {
this.userAgent,
this.initialMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.opaque = true,
}) : assert(javascriptMode != null),
assert(initialMediaPlaybackPolicy != null),
super(key: key);
Expand Down Expand Up @@ -396,6 +397,9 @@ class WebView extends StatefulWidget {
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;

/// If set to `false`, the webview background will be transparent.
final bool opaque;

@override
State<StatefulWidget> createState() => _WebViewState();
}
Expand Down Expand Up @@ -460,6 +464,7 @@ CreationParams _creationParamsfromWidget(WebView widget) {
javascriptChannelNames: _extractChannelNames(widget.javascriptChannels),
userAgent: widget.userAgent,
autoMediaPlaybackPolicy: widget.initialMediaPlaybackPolicy,
opaque: widget.opaque,
);
}

Expand Down