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

[webview_flutter] Add a backgroundColor option to the iOS webview #4570

Merged
merged 5 commits into from
Dec 4, 2021
Merged

[webview_flutter] Add a backgroundColor option to the iOS webview #4570

merged 5 commits into from
Dec 4, 2021

Conversation

e-adrien
Copy link
Contributor

@e-adrien e-adrien commented Dec 3, 2021

This PR add an option to set the background color of the iOS webview.

Original PR :

#3431

It fixes this issue :

flutter/flutter#29300

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@google-cla google-cla bot added the cla: yes label Dec 3, 2021
@github-actions github-actions bot added p: webview_flutter Edits files for a webview_flutter plugin platform-ios labels Dec 3, 2021
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

Looks great other than minor nits.

/cc @jmagman or @mvanbeusekom for secondary review.

WebViewController controller, BuildContext context) async {
final String contentBase64 =
base64Encode(const Utf8Encoder().convert(kTransparentBackgroundPage));
await controller.loadUrl('data:text/html;base64,$contentBase64');
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you can replace these two lines with the new controller.loadHtmlString.

UIColor* backgroundColor = [UIColor colorWithRed:(backgroundColorInt >> 16 & 0xff) / 255.f
green:(backgroundColorInt >> 8 & 0xff) / 255.f
blue:(backgroundColorInt & 0xff) / 255.f
alpha:(backgroundColorInt >> 24 & 0xff) / 255.f];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: CGFloat is essentially always 64-bit these days, so these don't need to be floats: / 255.0

alpha:(backgroundColorInt >> 24 & 0xff) / 255.f];
_webView.opaque = NO;
_webView.backgroundColor = UIColor.clearColor;
_webView.scrollView.backgroundColor = backgroundColor;
Copy link
Contributor

Choose a reason for hiding this comment

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

Non-opaque views have worse performance, so it's almost certainly worth special-casing this:

int alpha = backgroundColorInt >> 24 & 0xff;
UIColor* backgroundColor = [... alpha:(alpha / 255.0)];
if (alpha == 255) {
  _webView.backgroundColor = backgroundColor;
} else {
  [these three lines]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to make the change, but if I don't set _webView.opaque = NO; for an opaque color too the WebView's background is white on my emulator.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's unfortunate; hopefully that means the drawing for WKWebView is doing something non-obvious then which will avoid the performance penalty.

Copy link
Contributor

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

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

LGTM apart from comments Stuart already made and two small additional nits.

@@ -227,6 +228,13 @@ 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: convert "webview" to breadcrum:

Suggested change
/// The background color of the webview.
/// The background color of the [WebView].

Comment on lines 233 to 235
/// When null the platform's webview default background color is used.
///
/// By default `backgroundColor` is null.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could be combined into one paragraph:

Suggested change
/// When null the platform's webview default background color is used.
///
/// By default `backgroundColor` is null.
/// When `null` the platform's webview default background color is used. By
/// default [backgroundColor] is `null`.

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

Looks good once nits and opaque color assignment special casing added.

@@ -70,6 +91,7 @@ class _WebViewExampleState extends State<_WebViewExample> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
Copy link
Member

Choose a reason for hiding this comment

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

What about hard coding the color so if it changes in the framework it doesn't break this test in the plugin repo only on the master tests?

Suggested change
backgroundColor: Colors.green,
backgroundColor: Color(0xFF4CAF50),

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM!

@stuartmorgan-g stuartmorgan-g merged commit c32b27b into flutter:master Dec 4, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 4, 2021
amantoux pushed a commit to amantoux/plugins that referenced this pull request Dec 11, 2021
…utter#4570)

This PR add an option to set the background color of the iOS webview.

Part of flutter#3431
Part of flutter/flutter#29300
KyleFin pushed a commit to KyleFin/plugins that referenced this pull request Dec 21, 2021
…utter#4570)

This PR add an option to set the background color of the iOS webview.

Part of flutter#3431
Part of flutter/flutter#29300
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: webview_flutter Edits files for a webview_flutter plugin platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants