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

[webview_flutter] Add a backgroundColor option to the webview platform interface #4567

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,3 +1,7 @@
## 1.7.0

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

## 1.6.1

* Revert deprecation of `clearCookies` in WebViewPlatform for later deprecation.
Expand Down
Expand Up @@ -266,6 +266,7 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
'userAgent': creationParams.userAgent,
'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index,
'usesHybridComposition': usesHybridComposition,
'backgroundColor': creationParams.backgroundColor?.value,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a corresponding test here

Copy link
Contributor Author

@e-adrien e-adrien Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put the test in the group('Tests on plugin.flutter.io/webview_<channel_id> channel', () {}) but maybe it would have been better to have them in a dedicated group for changes in creationParamsToMap()

'cookies': creationParams.cookies
.map((WebViewCookie cookie) => cookie.toJson())
.toList()
Expand Down
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
import 'package:webview_flutter_platform_interface/src/types/types.dart';

import 'auto_media_playback_policy.dart';
Expand All @@ -22,6 +23,7 @@ class CreationParams {
this.userAgent,
this.autoMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.backgroundColor,
this.cookies = const <WebViewCookie>[],
}) : assert(autoMediaPlaybackPolicy != null);

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

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

/// The initial set of cookies to set before the webview does its first load.
final List<WebViewCookie> cookies;

@override
String toString() {
return 'CreationParams(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent, cookies: $cookies)';
return 'CreationParams(initialUrl: $initialUrl, settings: $webSettings, javascriptChannelNames: $javascriptChannelNames, UserAgent: $userAgent, backgroundColor: $backgroundColor, cookies: $cookies)';
}
}
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.6.1
version: 1.7.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Expand Up @@ -4,6 +4,7 @@

import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
Expand Down Expand Up @@ -552,6 +553,32 @@ void main() {
],
);
});

test('backgroundColor is null by default', () {
final CreationParams creationParams = CreationParams(
webSettings: WebSettings(
userAgent: const WebSetting<String?>.of('Dart Test'),
),
);
final Map<String, dynamic> creationParamsMap =
MethodChannelWebViewPlatform.creationParamsToMap(creationParams);

expect(creationParamsMap['backgroundColor'], null);
});

test('backgroundColor is converted to an int', () {
const Color whiteColor = Color(0xFFFFFFFF);
final CreationParams creationParams = CreationParams(
backgroundColor: whiteColor,
webSettings: WebSettings(
userAgent: const WebSetting<String?>.of('Dart Test'),
),
);
final Map<String, dynamic> creationParamsMap =
MethodChannelWebViewPlatform.creationParamsToMap(creationParams);

expect(creationParamsMap['backgroundColor'], whiteColor.value);
});
});

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