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

Commit 509d3e2

Browse files
authored
[webview_flutter] Add a backgroundColor option to the webview platform interface (#4567)
Part of #3431 Part of flutter/flutter#29300
1 parent 936257f commit 509d3e2

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.7.0
2+
3+
* Add an option to set the background color of the webview.
4+
15
## 1.6.1
26

37
* Revert deprecation of `clearCookies` in WebViewPlatform for later deprecation.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
266266
'userAgent': creationParams.userAgent,
267267
'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index,
268268
'usesHybridComposition': usesHybridComposition,
269+
'backgroundColor': creationParams.backgroundColor?.value,
269270
'cookies': creationParams.cookies
270271
.map((WebViewCookie cookie) => cookie.toJson())
271272
.toList()

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/creation_params.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/widgets.dart';
56
import 'package:webview_flutter_platform_interface/src/types/types.dart';
67

78
import 'auto_media_playback_policy.dart';
@@ -22,6 +23,7 @@ class CreationParams {
2223
this.userAgent,
2324
this.autoMediaPlaybackPolicy =
2425
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
26+
this.backgroundColor,
2527
this.cookies = const <WebViewCookie>[],
2628
}) : assert(autoMediaPlaybackPolicy != null);
2729

@@ -56,11 +58,16 @@ class CreationParams {
5658
/// Which restrictions apply on automatic media playback.
5759
final AutoMediaPlaybackPolicy autoMediaPlaybackPolicy;
5860

61+
/// The background color of the webview.
62+
///
63+
/// When null the platform's webview default background color is used.
64+
final Color? backgroundColor;
65+
5966
/// The initial set of cookies to set before the webview does its first load.
6067
final List<WebViewCookie> cookies;
6168

6269
@override
6370
String toString() {
64-
return 'CreationParams(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent, cookies: $cookies)';
71+
return 'CreationParams(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent, backgroundColor: $backgroundColor, cookies: $cookies)';
6572
}
6673
}

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 1.6.1
7+
version: 1.7.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:typed_data';
66

7+
import 'package:flutter/cupertino.dart';
78
import 'package:flutter/services.dart';
89
import 'package:flutter_test/flutter_test.dart';
910
import 'package:mockito/mockito.dart';
@@ -552,6 +553,32 @@ void main() {
552553
],
553554
);
554555
});
556+
557+
test('backgroundColor is null by default', () {
558+
final CreationParams creationParams = CreationParams(
559+
webSettings: WebSettings(
560+
userAgent: const WebSetting<String?>.of('Dart Test'),
561+
),
562+
);
563+
final Map<String, dynamic> creationParamsMap =
564+
MethodChannelWebViewPlatform.creationParamsToMap(creationParams);
565+
566+
expect(creationParamsMap['backgroundColor'], null);
567+
});
568+
569+
test('backgroundColor is converted to an int', () {
570+
const Color whiteColor = Color(0xFFFFFFFF);
571+
final CreationParams creationParams = CreationParams(
572+
backgroundColor: whiteColor,
573+
webSettings: WebSettings(
574+
userAgent: const WebSetting<String?>.of('Dart Test'),
575+
),
576+
);
577+
final Map<String, dynamic> creationParamsMap =
578+
MethodChannelWebViewPlatform.creationParamsToMap(creationParams);
579+
580+
expect(creationParamsMap['backgroundColor'], whiteColor.value);
581+
});
555582
});
556583

557584
group('Tests on `plugins.flutter.io/cookie_manager` channel', () {

0 commit comments

Comments
 (0)