Skip to content

Commit

Permalink
[webview_flutter] Adds examples of accessing platform-specific featur…
Browse files Browse the repository at this point in the history
…es for each class (#7089)

* start

* more docs and version bump

* fix main
  • Loading branch information
bparrishMines committed Feb 19, 2023
1 parent 39a1ee4 commit 1c89a0e
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.0.4

* Adds examples of accessing platform-specific features for each class.

## 4.0.3

* Updates example code for `use_build_context_synchronously` lint.
Expand Down
12 changes: 9 additions & 3 deletions packages/webview_flutter/webview_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
```

Now, additional features can be accessed through the platform implementations. Classes
`WebViewController`, `WebViewWidget`, `NavigationDelegate`, and `WebViewCookieManager` pass their
[WebViewController], [WebViewWidget], [NavigationDelegate], and [WebViewCookieManager] pass their
functionality to a class provided by the current platform. Below are a couple of ways to access
additional functionality provided by the platform and is followed by an example.

Expand Down Expand Up @@ -212,11 +212,17 @@ Below is a non-exhaustive list of changes to the API:
* `WebView.userAgent` -> `WebViewController.setUserAgent`
* `WebView.backgroundColor` -> `WebViewController.setBackgroundColor`
* The following features have been moved to an Android implementation class. See section
`Platform-Specific Features` for details on accessing Android platform specific features.
`Platform-Specific Features` for details on accessing Android platform-specific features.
* `WebView.debuggingEnabled` -> `static AndroidWebViewController.enableDebugging`
* `WebView.initialMediaPlaybackPolicy` -> `AndroidWebViewController.setMediaPlaybackRequiresUserGesture`
* The following features have been moved to an iOS implementation class. See section
`Platform-Specific Features` for details on accessing iOS platform specific features.
`Platform-Specific Features` for details on accessing iOS platform-specific features.
* `WebView.gestureNavigationEnabled` -> `WebKitWebViewController.setAllowsBackForwardNavigationGestures`
* `WebView.initialMediaPlaybackPolicy` -> `WebKitWebViewControllerCreationParams.mediaTypesRequiringUserAction`
* `WebView.allowsInlineMediaPlayback` -> `WebKitWebViewControllerCreationParams.allowsInlineMediaPlayback`

<!-- Links -->
[WebViewController]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewController-class.html
[WebViewWidget]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewWidget-class.html
[NavigationDelegate]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/NavigationDelegate-class.html
[WebViewCookieManager]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewCookieManager-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ import 'webview_controller.dart';
/// the progress of navigation requests.
///
/// See [WebViewController.setNavigationDelegate].
///
/// ## Platform-Specific Features
/// This class contains an underlying implementation provided by the current
/// platform. Once a platform implementation is imported, the examples below
/// can be followed to use features provided by a platform's implementation.
///
/// {@macro webview_flutter.NavigationDelegate.fromPlatformCreationParams}
///
/// Below is an example of accessing the platform-specific implementation for
/// iOS and Android:
///
/// ```dart
/// final NavigationDelegate navigationDelegate = NavigationDelegate();
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// final WebKitNavigationDelegate webKitDelegate =
/// navigationDelegate.platform as WebKitNavigationDelegate;
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// final AndroidNavigationDelegate androidDelegate =
/// navigationDelegate.platform as AndroidNavigationDelegate;
/// }
/// ```
class NavigationDelegate {
/// Constructs a [NavigationDelegate].
NavigationDelegate({
Expand All @@ -32,6 +54,33 @@ class NavigationDelegate {

/// Constructs a [NavigationDelegate] from creation params for a specific
/// platform.
///
/// {@template webview_flutter.NavigationDelegate.fromPlatformCreationParams}
/// Below is an example of setting platform-specific creation parameters for
/// iOS and Android:
///
/// ```dart
/// PlatformNavigationDelegateCreationParams params =
/// const PlatformNavigationDelegateCreationParams();
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// params = WebKitNavigationDelegateCreationParams
/// .fromPlatformNavigationDelegateCreationParams(
/// params,
/// );
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// params = AndroidNavigationDelegateCreationParams
/// .fromPlatformNavigationDelegateCreationParams(
/// params,
/// );
/// }
///
/// final NavigationDelegate navigationDelegate =
/// NavigationDelegate.fromPlatformCreationParams(
/// params,
/// );
/// ```
/// {@endtemplate}
NavigationDelegate.fromPlatformCreationParams(
PlatformNavigationDelegateCreationParams params, {
FutureOr<NavigationDecision> Function(NavigationRequest request)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,75 @@ import 'package:flutter/material.dart';
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'navigation_delegate.dart';
import 'webview_widget.dart';

/// Controls a WebView provided by the host platform.
///
/// Pass this to a [WebViewWidget] to display the WebView.
///
/// A [WebViewController] can only be used by a single [WebViewWidget] at a
/// time.
///
/// ## Platform-Specific Features
/// This class contains an underlying implementation provided by the current
/// platform. Once a platform implementation is imported, the examples below
/// can be followed to use features provided by a platform's implementation.
///
/// {@macro webview_flutter.WebViewController.fromPlatformCreationParams}
///
/// Below is an example of accessing the platform-specific implementation for
/// iOS and Android:
///
/// ```dart
/// final WebViewController webViewController = WebViewController();
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// final WebKitWebViewController webKitController =
/// webViewController.platform as WebKitWebViewController;
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// final AndroidWebViewController androidController =
/// webViewController.platform as AndroidWebViewController;
/// }
/// ```
class WebViewController {
/// Constructs a [WebViewController].
///
/// See [WebViewController.fromPlatformCreationParams] for setting parameters
/// for a specific platform.
WebViewController()
: this.fromPlatformCreationParams(
const PlatformWebViewControllerCreationParams(),
);

/// Constructs a [WebViewController] from creation params for a specific
/// platform.
///
/// {@template webview_flutter.WebViewCookieManager.fromPlatformCreationParams}
/// Below is an example of setting platform-specific creation parameters for
/// iOS and Android:
///
/// ```dart
/// PlatformWebViewControllerCreationParams params =
/// const PlatformWebViewControllerCreationParams();
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// params = WebKitWebViewControllerCreationParams
/// .fromPlatformWebViewControllerCreationParams(
/// params,
/// );
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// params = AndroidWebViewControllerCreationParams
/// .fromPlatformWebViewControllerCreationParams(
/// params,
/// );
/// }
///
/// final WebViewController webViewController =
/// WebViewController.fromPlatformCreationParams(
/// params,
/// );
/// ```
/// {@endtemplate}
WebViewController.fromPlatformCreationParams(
PlatformWebViewControllerCreationParams params,
) : this.fromPlatform(PlatformWebViewController(params));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,67 @@
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

/// Manages cookies pertaining to all WebViews.
///
/// ## Platform-Specific Features
/// This class contains an underlying implementation provided by the current
/// platform. Once a platform implementation is imported, the examples below
/// can be followed to use features provided by a platform's implementation.
///
/// {@macro webview_flutter.WebViewCookieManager.fromPlatformCreationParams}
///
/// Below is an example of accessing the platform-specific implementation for
/// iOS and Android:
///
/// ```dart
/// final WebViewCookieManager cookieManager = WebViewCookieManager();
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// final WebKitWebViewCookieManager webKitManager =
/// cookieManager.platform as WebKitWebViewCookieManager;
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// final AndroidWebViewCookieManager androidManager =
/// cookieManager.platform as AndroidWebViewCookieManager;
/// }
/// ```
class WebViewCookieManager {
/// Constructs a [WebViewCookieManager].
///
/// See [WebViewCookieManager.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
WebViewCookieManager()
: this.fromPlatformCreationParams(
const PlatformWebViewCookieManagerCreationParams(),
);

/// Constructs a [WebViewCookieManager] from creation params for a specific
/// platform.
///
/// {@template webview_flutter.WebViewCookieManager.fromPlatformCreationParams}
/// Below is an example of setting platform-specific creation parameters for
/// iOS and Android:
///
/// ```dart
/// PlatformWebViewCookieManagerCreationParams params =
/// const PlatformWebViewCookieManagerCreationParams();
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// params = WebKitWebViewCookieManagerCreationParams
/// .fromPlatformWebViewCookieManagerCreationParams(
/// params,
/// );
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// params = AndroidWebViewCookieManagerCreationParams
/// .fromPlatformWebViewCookieManagerCreationParams(
/// params,
/// );
/// }
///
/// final WebViewCookieManager webViewCookieManager =
/// WebViewCookieManager.fromPlatformCreationParams(
/// params,
/// );
/// ```
/// {@endtemplate}
WebViewCookieManager.fromPlatformCreationParams(
PlatformWebViewCookieManagerCreationParams params,
) : this.fromPlatform(PlatformWebViewCookieManager(params));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,35 @@ import 'package:webview_flutter_platform_interface/webview_flutter_platform_inte
import 'webview_controller.dart';

/// Displays a native WebView as a Widget.
///
/// ## Platform-Specific Features
/// This class contains an underlying implementation provided by the current
/// platform. Once a platform implementation is imported, the examples below
/// can be followed to use features provided by a platform's implementation.
///
/// {@macro webview_flutter.WebViewWidget.fromPlatformCreationParams}
///
/// Below is an example of accessing the platform-specific implementation for
/// iOS and Android:
///
/// ```dart
/// final WebViewController controller = WebViewController();
///
/// final WebViewWidget webViewWidget = WebViewWidget(controller: controller);
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// final WebKitWebViewWidget webKitWidget =
/// webViewWidget.platform as WebKitWebViewWidget;
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// final AndroidWebViewWidget androidWidget =
/// webViewWidget.platform as AndroidWebViewWidget;
/// }
/// ```
class WebViewWidget extends StatelessWidget {
/// Constructs a [WebViewWidget].
///
/// See [WebViewWidget.fromPlatformCreationParams] for setting parameters for
/// a specific platform.
WebViewWidget({
Key? key,
required WebViewController controller,
Expand All @@ -27,8 +54,40 @@ class WebViewWidget extends StatelessWidget {
),
);

/// Constructs a [WebViewWidget] from creation params for a specific
/// platform.
/// Constructs a [WebViewWidget] from creation params for a specific platform.
///
/// {@template webview_flutter.WebViewWidget.fromPlatformCreationParams}
/// Below is an example of setting platform-specific creation parameters for
/// iOS and Android:
///
/// ```dart
/// final WebViewController controller = WebViewController();
///
/// PlatformWebViewWidgetCreationParams params =
/// PlatformWebViewWidgetCreationParams(
/// controller: controller.platform,
/// layoutDirection: TextDirection.ltr,
/// gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
/// );
///
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
/// params = WebKitWebViewWidgetCreationParams
/// .fromPlatformWebViewWidgetCreationParams(
/// params,
/// );
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
/// params = AndroidWebViewWidgetCreationParams
/// .fromPlatformWebViewWidgetCreationParams(
/// params,
/// );
/// }
///
/// final WebViewWidget webViewWidget =
/// WebViewWidget.fromPlatformCreationParams(
/// params: params,
/// );
/// ```
/// {@endtemplate}
WebViewWidget.fromPlatformCreationParams({
Key? key,
required PlatformWebViewWidgetCreationParams params,
Expand Down
2 changes: 1 addition & 1 deletion packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 4.0.3
version: 4.0.4

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down

0 comments on commit 1c89a0e

Please sign in to comment.