webview_all 1.0.1
webview_all: ^1.0.1 copied to clipboard
A Flutter plugin that provides a WebView widget with full-platform support.
WebView All #
A WebView component that supports all Flutter platforms.
| Platform | Support | Implementation |
|---|---|---|
| Android | SDK 24+ | WebView |
| iOS | 13.0+ | WKWebView |
| Windows | Win10 1809+ | WebView2 |
| macOS | 10.15+ | WKWebView |
| Linux | webkit2gtk-4.1 | WebKitGTK |
| Web | Any | js-interop |
Usage #
You can now display a WebView in the following way:
- Instantiate a
WebViewController:
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update the loading progress bar
},
onPageStarted: (String url) {},
onPageFinished: (String url) {},
onHttpError: (HttpResponseError error) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.bilibili.com')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse('https://flutter.dev'));
- Pass
controllertoWebViewWidget:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Simple Example')),
body: WebViewWidget(controller: controller),
);
}
For more details, please refer to the Dart documentation for WebViewController and WebViewWidget.
Platform Features #
Many classes provide subclasses, or expose the underlying platform implementation, so that you can access platform-specific capabilities.
If you want to access platform features, first add the corresponding platform implementation package to your app or package:
- Android: webview_flutter_android
- iOS/macOS: webview_flutter_wkwebview
Then, import the corresponding platform implementation package in your app or package:
// Import Android platform features.
import 'package:webview_flutter_android/webview_flutter_android.dart';
// Import iOS/macOS platform features.
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
After that, you can access additional capabilities through the platform implementation.
[WebViewController], [WebViewWidget], [NavigationDelegate], and [WebViewCookieManager]
all delegate their functionality to the implementation provided by the current platform. Below are two common ways to access them, with examples following.
- Pass the creation params class provided by the platform implementation to the
fromPlatformCreationParamsconstructor, such asWebViewController.fromPlatformCreationParams,WebViewWidget.fromPlatformCreationParams, and so on. - Call methods provided by the platform implementation through the
platformfield on the class, such asWebViewController.platform,WebViewWidget.platform, and so on.
The following example shows how to set additional parameters for iOS/macOS and Android on WebViewController.
late final PlatformWebViewControllerCreationParams params;
if (WebViewPlatform.instance is WebKitWebViewPlatform) {
params = WebKitWebViewControllerCreationParams(
allowsInlineMediaPlayback: true,
mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
);
} else {
params = const PlatformWebViewControllerCreationParams();
}
final controller = WebViewController.fromPlatformCreationParams(params);
if (controller.platform is AndroidWebViewController) {
AndroidWebViewController.enableDebugging(true);
(controller.platform as AndroidWebViewController)
.setMediaPlaybackRequiresUserGesture(false);
}
For more information about Android platform features, see: https://pub.dev/documentation/webview_flutter_android/latest/webview_flutter_android/webview_flutter_android-library.html
For more information about iOS/macOS platform features, see: https://pub.dev/documentation/webview_flutter_wkwebview/latest/webview_flutter_wkwebview/webview_flutter_wkwebview-library.html
Enable Material Components for Android #
If you want users to use Material Components when interacting with input controls in WebView, please follow the steps in Enable Material Components.
Set Custom Request Headers for POST Requests #
Currently, when making POST requests through WebViewController.loadRequest on Android, setting custom request headers is not yet supported. If you need this capability, one workaround is to make the request manually and then load the response content through loadHtmlString.
Known Limitations #
- Some APIs are missing on the macOS platform.
- Only a small subset of APIs is implemented on the Web platform.