Skip to content

feat: Expose setUseWideViewPort on Android (#106999) #9151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 34 additions & 1 deletion packages/webview_flutter/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ const String kTransparentBackgroundPage = '''
</html>
''';

const String kViewportMetaPage = '''
<!DOCTYPE html>
<html>
<head>
<title>Viewport meta test</title>
</head>
<meta name="viewport" content="width=1000, initial-scale=1" />
<style type="text/css">
body { background: transparent; margin: 0; padding: 0; }
#shape { background: red; width: 50vw; height: 50vw; }
</style>
<body>
<div>
<p>Viewport meta test</p>
<img id="shape" src="https://storage.googleapis.com/cms-storage-bucket/4fd5520fe28ebf839174.svg"/>
</div>
</body>
</html>
''';

const String kLogExamplePage = '''
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -199,7 +219,8 @@ Page resource error:
if (controller.platform is AndroidWebViewController) {
AndroidWebViewController.enableDebugging(true);
(controller.platform as AndroidWebViewController)
.setMediaPlaybackRequiresUserGesture(false);
..setMediaPlaybackRequiresUserGesture(false)
..setUseWideViewPort(false);
}
// #enddocregion platform_features

Expand Down Expand Up @@ -307,6 +328,7 @@ enum MenuOptions {
loadFlutterAsset,
loadHtmlString,
transparentBackground,
viewportMeta,
setCookie,
logExample,
basicAuthentication,
Expand Down Expand Up @@ -351,6 +373,8 @@ class SampleMenu extends StatelessWidget {
_onLoadHtmlStringExample();
case MenuOptions.transparentBackground:
_onTransparentBackground();
case MenuOptions.viewportMeta:
_onViewportMetaExample();
case MenuOptions.setCookie:
_onSetCookie();
case MenuOptions.logExample:
Expand Down Expand Up @@ -409,6 +433,11 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.transparentBackground,
child: Text('Transparent background example'),
),
const PopupMenuItem<MenuOptions>(
key: ValueKey<String>('ShowViewportMetaExample'),
value: MenuOptions.viewportMeta,
child: Text('Viewport meta example'),
),
const PopupMenuItem<MenuOptions>(
value: MenuOptions.setCookie,
child: Text('Set cookie'),
Expand Down Expand Up @@ -540,6 +569,10 @@ class SampleMenu extends StatelessWidget {
return webViewController.loadHtmlString(kTransparentBackgroundPage);
}

Future<void> _onViewportMetaExample() {
return webViewController.loadHtmlString(kViewportMetaPage);
}

Widget _getCookieList(String cookies) {
if (cookies == '""') {
return Container();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.5.0

* Adds support to set using wide view port. See `AndroidWebViewController.setUseWideViewPort`.

## 4.4.2

* Updates pigeon generated code to fix `ImplicitSamInstance` and `SyntheticAccessor` Kotlin lint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AndroidWebViewController extends PlatformWebViewController {
_webView.settings.setJavaScriptCanOpenWindowsAutomatically(true);
_webView.settings.setSupportMultipleWindows(true);
_webView.settings.setLoadWithOverviewMode(true);
_webView.settings.setUseWideViewPort(true);
_webView.settings.setUseWideViewPort(false);
_webView.settings.setDisplayZoomControls(false);
_webView.settings.setBuiltInZoomControls(true);

Expand Down Expand Up @@ -599,6 +599,13 @@ class AndroidWebViewController extends PlatformWebViewController {
Future<void> setTextZoom(int textZoom) =>
_webView.settings.setTextZoom(textZoom);

/// Sets whether the WebView should enable support for the "viewport" HTML
/// meta tag or should use a wide viewport.
///
/// The default is false.
Future<void> setUseWideViewPort(bool use) =>
_webView.settings.setUseWideViewPort(use);

/// Enables or disables content URL access.
///
/// The default is true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_android
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 4.4.2
version: 4.5.0

environment:
sdk: ^3.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void main() {
.called(1);
verify(mockWebSettings.setLoadWithOverviewMode(true)).called(1);
verify(mockWebSettings.setSupportMultipleWindows(true)).called(1);
verify(mockWebSettings.setUseWideViewPort(true)).called(1);
verify(mockWebSettings.setUseWideViewPort(false)).called(1);
});

test('loadFile without file prefix', () async {
Expand Down Expand Up @@ -1576,6 +1576,22 @@ void main() {
verify(mockSettings.setMediaPlaybackRequiresUserGesture(true)).called(1);
});

test('setUseWideViewPort', () async {
final MockWebView mockWebView = MockWebView();
final MockWebSettings mockSettings = MockWebSettings();
final AndroidWebViewController controller = createControllerWithMocks(
mockWebView: mockWebView,
mockSettings: mockSettings,
);

clearInteractions(mockWebView);

await controller.setUseWideViewPort(true);

verify(mockWebView.settings).called(1);
verify(mockSettings.setUseWideViewPort(true)).called(1);
});

test('setAllowContentAccess', () async {
final MockWebView mockWebView = MockWebView();
final MockWebSettings mockSettings = MockWebSettings();
Expand Down