Skip to content

Commit

Permalink
[webview_flutter] Add a backgroundColor option to the webview platfor…
Browse files Browse the repository at this point in the history
…m interface (flutter#4567)

Part of flutter#3431
Part of flutter/flutter#29300
  • Loading branch information
e-adrien authored and amantoux committed Dec 11, 2021
1 parent 47948ba commit 87d8dbc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
@@ -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,
'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

0 comments on commit 87d8dbc

Please sign in to comment.