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

[webview_flutter] Add an option to set the background color of the webview #3431

Merged
merged 30 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
54de8c0
Add an option to set a transparent background for the webview
e-adrien Sep 30, 2021
bd7ac95
Update webview_flutter version
e-adrien Sep 30, 2021
02c3d27
Add tests for the configuration
e-adrien Sep 30, 2021
abba19d
Add integration test for the transparent background
e-adrien Sep 30, 2021
28f50f8
Add iOS native test for the transparent background
e-adrien Sep 30, 2021
56cac9b
Make functions statics, rename variables & add comment in iOS native …
e-adrien Sep 30, 2021
ef0d920
Replace the sleep call in iOS native test
e-adrien Sep 30, 2021
2c22d57
Rename opaque parameter to transparent
e-adrien Sep 30, 2021
99ae827
Android integration test for the transparent background
e-adrien Sep 30, 2021
9205b43
Replace the transparent parameter by a backgroundColor parameter
e-adrien Sep 30, 2021
ef562c3
Update CHANGELOG.md
e-adrien Sep 30, 2021
9657dea
Add Android native test for the transparent background
e-adrien Sep 30, 2021
9a58db8
Remove the integration test for the background color as we have XCUIT…
e-adrien Sep 30, 2021
c21abef
Add missing comments
e-adrien Sep 30, 2021
c2f7002
Move android native tests into the webview_flutter_android package
e-adrien Sep 30, 2021
754360f
Move ios native tests into the webview_flutter_wkwebview package
e-adrien Sep 30, 2021
768c915
Add ios native test for background colors with transparency
e-adrien Sep 30, 2021
feffcb6
Add missing dependency_overrides
e-adrien Sep 30, 2021
91e823e
Update changelogs
e-adrien Oct 6, 2021
d169d79
Fix failing iOS test
e-adrien Oct 7, 2021
c7436c4
Fix iOS implementation
e-adrien Oct 8, 2021
a561545
Update Android Espresso Test
e-adrien Oct 28, 2021
726d517
Merge branch 'main' into webview-transparent-background
stuartmorgan Nov 30, 2021
2ba4b91
Merge branch 'master' into webview-transparent-background
e-adrien Dec 1, 2021
3d3f66b
Update changelogs
e-adrien Dec 1, 2021
a164d03
Fix warnings
e-adrien Dec 1, 2021
5eff98a
Run mockito
e-adrien Dec 1, 2021
d0ef738
Merge branch 'master' into webview-transparent-background
e-adrien Dec 2, 2021
c8b0303
Merge branch 'main' into webview-transparent-background
stuartmorgan Dec 7, 2021
19ae1a6
Update to match #4584
stuartmorgan Dec 7, 2021
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
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