Skip to content

[webview_flutter_android] 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

Merged
merged 14 commits into from
May 16, 2025
Merged
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.6.0

* Adds support to set using wide view port. See `AndroidWebViewController.setUseWideViewPort`.
* Changes default of `WebSettings.setUseWideViewPort` to `false` to align with native WebViews.

## 4.5.0

* Adds support to set whether to draw the scrollbar. See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ const String kAlertTestPage = '''
</html>
''';

const String kViewportMetaPage = '''
<!DOCTYPE html>
<html>
<head>
<title>Viewport meta example</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 example</p>
<img id="shape" src="https://storage.googleapis.com/cms-storage-bucket/4fd5520fe28ebf839174.svg"/>
</div>
</body>
</html>
''';

class WebViewExample extends StatefulWidget {
const WebViewExample({super.key, this.cookieManager});

Expand Down Expand Up @@ -325,6 +345,7 @@ enum MenuOptions {
logExample,
basicAuthentication,
javaScriptAlert,
viewportMeta,
}

class SampleMenu extends StatelessWidget {
Expand Down Expand Up @@ -380,6 +401,8 @@ class SampleMenu extends StatelessWidget {
_promptForUrl(context);
case MenuOptions.javaScriptAlert:
_onJavaScriptAlertExample(context);
case MenuOptions.viewportMeta:
_onViewportMetaExample();
}
},
itemBuilder: (BuildContext context) => <PopupMenuItem<MenuOptions>>[
Expand Down Expand Up @@ -452,6 +475,10 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.javaScriptAlert,
child: Text('JavaScript Alert Example'),
),
const PopupMenuItem<MenuOptions>(
value: MenuOptions.viewportMeta,
child: Text('Viewport meta example'),
),
],
);
}
Expand Down Expand Up @@ -748,6 +775,10 @@ class SampleMenu extends StatelessWidget {
}) ??
'';
}

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

class NavigationControls extends StatelessWidget {
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.5.0
version: 4.6.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 @@ -1598,6 +1598,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