Skip to content

Commit

Permalink
[webview_flutter] Add an option to set the background color of the we…
Browse files Browse the repository at this point in the history
…bview (flutter#3431)
  • Loading branch information
e-adrien authored and amantoux committed Dec 11, 2021
1 parent dc0f9c6 commit 1b86943
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.5.0

* Adds an option to set the background color of the webview.

## 2.4.0

* Adds support for the `loadFile` and `loadHtmlString` methods.
Expand Down
38 changes: 38 additions & 0 deletions packages/webview_flutter/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ const String kLocalExamplePage = '''
</html>
''';

const String kTransparentBackgroundPage = '''
<!DOCTYPE html>
<html>
<head>
<title>Transparent background test</title>
</head>
<style type="text/css">
body { background: transparent; margin: 0; padding: 0; }
#container { position: relative; margin: 0; padding: 0; width: 100vw; height: 100vh; }
#shape { background: red; width: 200px; height: 200px; margin: 0; padding: 0; position: absolute; top: calc(50% - 100px); left: calc(50% - 100px); }
p { text-align: center; }
</style>
<body>
<div id="container">
<p>Transparent background test</p>
<div id="shape"></div>
</div>
</body>
</html>
''';

class WebViewExample extends StatefulWidget {
@override
_WebViewExampleState createState() => _WebViewExampleState();
Expand All @@ -68,6 +89,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 @@ -106,6 +128,7 @@ class _WebViewExampleState extends State<WebViewExample> {
print('Page finished loading: $url');
},
gestureNavigationEnabled: true,
backgroundColor: const Color(0x00000000),
);
}),
floatingActionButton: favoriteButton(),
Expand Down Expand Up @@ -155,6 +178,7 @@ enum MenuOptions {
navigationDelegate,
loadLocalFile,
loadHtmlString,
transparentBackground,
}

class SampleMenu extends StatelessWidget {
Expand All @@ -170,6 +194,7 @@ class SampleMenu extends StatelessWidget {
builder:
(BuildContext context, AsyncSnapshot<WebViewController> controller) {
return PopupMenuButton<MenuOptions>(
key: const ValueKey<String>('ShowPopupMenu'),
onSelected: (MenuOptions value) {
switch (value) {
case MenuOptions.showUserAgent:
Expand Down Expand Up @@ -199,6 +224,9 @@ class SampleMenu extends StatelessWidget {
case MenuOptions.loadHtmlString:
_onLoadHtmlStringExample(controller.data!, context);
break;
case MenuOptions.transparentBackground:
_onTransparentBackground(controller.data!, context);
break;
}
},
itemBuilder: (BuildContext context) => <PopupMenuItem<MenuOptions>>[
Expand Down Expand Up @@ -239,6 +267,11 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.loadLocalFile,
child: Text('Load local file'),
),
const PopupMenuItem<MenuOptions>(
key: ValueKey<String>('ShowTransparentBackgroundExample'),
value: MenuOptions.transparentBackground,
child: Text('Transparent background example'),
),
],
);
},
Expand Down Expand Up @@ -327,6 +360,11 @@ class SampleMenu extends StatelessWidget {
await controller.loadHtmlString(kLocalExamplePage);
}

Future<void> _onTransparentBackground(
WebViewController controller, BuildContext context) async {
await controller.loadHtmlString(kTransparentBackgroundPage);
}

Widget _getCookieList(String cookies) {
if (cookies == null || cookies == '""') {
return Container();
Expand Down
8 changes: 8 additions & 0 deletions packages/webview_flutter/webview_flutter/lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class WebView extends StatefulWidget {
this.initialMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.allowsInlineMediaPlayback = false,
this.backgroundColor,
}) : assert(javascriptMode != null),
assert(initialMediaPlaybackPolicy != null),
assert(allowsInlineMediaPlayback != null),
Expand Down Expand Up @@ -286,6 +287,12 @@ class WebView extends StatefulWidget {
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;

/// The background color of the [WebView].
///
/// When `null` the platform's webview default background color is used. By
/// default [backgroundColor] is `null`.
final Color? backgroundColor;

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

Expand Down
8 changes: 4 additions & 4 deletions packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
repository: https://github.com/flutter/plugins/tree/master/packages/webview_flutter/webview_flutter
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 2.4.0
version: 2.5.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -19,9 +19,9 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_android: ^2.4.0
webview_flutter_platform_interface: ^1.5.2
webview_flutter_wkwebview: ^2.4.0
webview_flutter_android: ^2.5.0
webview_flutter_platform_interface: ^1.7.0
webview_flutter_wkwebview: ^2.5.0

dev_dependencies:
build_runner: ^2.1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,34 @@ void main() {
});
});

group('Background color', () {
testWidgets('Defaults to null', (WidgetTester tester) async {
await tester.pumpWidget(const WebView());

final CreationParams params = captureBuildArgs(
mockWebViewPlatform,
creationParams: true,
).single as CreationParams;

expect(params.backgroundColor, null);
});

testWidgets('Can be transparent', (WidgetTester tester) async {
const Color transparentColor = Color(0x00000000);

await tester.pumpWidget(const WebView(
backgroundColor: transparentColor,
));

final CreationParams params = captureBuildArgs(
mockWebViewPlatform,
creationParams: true,
).single as CreationParams;

expect(params.backgroundColor, transparentColor);
});
});

group('Custom platform implementation', () {
setUp(() {
WebView.platform = MyWebViewPlatform();
Expand Down

0 comments on commit 1b86943

Please sign in to comment.