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

Commit c3e9d1b

Browse files
[webview_flutter] Adds examples of accessing platform-specific features for each class (#7089)
* start * more docs and version bump * fix main
1 parent 00d5855 commit c3e9d1b

File tree

7 files changed

+232
-6
lines changed

7 files changed

+232
-6
lines changed

packages/webview_flutter/webview_flutter/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.0.4
2+
3+
* Adds examples of accessing platform-specific features for each class.
4+
15
## 4.0.3
26

37
* Updates example code for `use_build_context_synchronously` lint.

packages/webview_flutter/webview_flutter/README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
9999
```
100100

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

@@ -212,11 +212,17 @@ Below is a non-exhaustive list of changes to the API:
212212
* `WebView.userAgent` -> `WebViewController.setUserAgent`
213213
* `WebView.backgroundColor` -> `WebViewController.setBackgroundColor`
214214
* The following features have been moved to an Android implementation class. See section
215-
`Platform-Specific Features` for details on accessing Android platform specific features.
215+
`Platform-Specific Features` for details on accessing Android platform-specific features.
216216
* `WebView.debuggingEnabled` -> `static AndroidWebViewController.enableDebugging`
217217
* `WebView.initialMediaPlaybackPolicy` -> `AndroidWebViewController.setMediaPlaybackRequiresUserGesture`
218218
* The following features have been moved to an iOS implementation class. See section
219-
`Platform-Specific Features` for details on accessing iOS platform specific features.
219+
`Platform-Specific Features` for details on accessing iOS platform-specific features.
220220
* `WebView.gestureNavigationEnabled` -> `WebKitWebViewController.setAllowsBackForwardNavigationGestures`
221221
* `WebView.initialMediaPlaybackPolicy` -> `WebKitWebViewControllerCreationParams.mediaTypesRequiringUserAction`
222222
* `WebView.allowsInlineMediaPlayback` -> `WebKitWebViewControllerCreationParams.allowsInlineMediaPlayback`
223+
224+
<!-- Links -->
225+
[WebViewController]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewController-class.html
226+
[WebViewWidget]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewWidget-class.html
227+
[NavigationDelegate]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/NavigationDelegate-class.html
228+
[WebViewCookieManager]: https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewCookieManager-class.html

packages/webview_flutter/webview_flutter/lib/src/navigation_delegate.dart

+49
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ import 'webview_controller.dart';
1212
/// the progress of navigation requests.
1313
///
1414
/// See [WebViewController.setNavigationDelegate].
15+
///
16+
/// ## Platform-Specific Features
17+
/// This class contains an underlying implementation provided by the current
18+
/// platform. Once a platform implementation is imported, the examples below
19+
/// can be followed to use features provided by a platform's implementation.
20+
///
21+
/// {@macro webview_flutter.NavigationDelegate.fromPlatformCreationParams}
22+
///
23+
/// Below is an example of accessing the platform-specific implementation for
24+
/// iOS and Android:
25+
///
26+
/// ```dart
27+
/// final NavigationDelegate navigationDelegate = NavigationDelegate();
28+
///
29+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
30+
/// final WebKitNavigationDelegate webKitDelegate =
31+
/// navigationDelegate.platform as WebKitNavigationDelegate;
32+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
33+
/// final AndroidNavigationDelegate androidDelegate =
34+
/// navigationDelegate.platform as AndroidNavigationDelegate;
35+
/// }
36+
/// ```
1537
class NavigationDelegate {
1638
/// Constructs a [NavigationDelegate].
1739
NavigationDelegate({
@@ -32,6 +54,33 @@ class NavigationDelegate {
3254

3355
/// Constructs a [NavigationDelegate] from creation params for a specific
3456
/// platform.
57+
///
58+
/// {@template webview_flutter.NavigationDelegate.fromPlatformCreationParams}
59+
/// Below is an example of setting platform-specific creation parameters for
60+
/// iOS and Android:
61+
///
62+
/// ```dart
63+
/// PlatformNavigationDelegateCreationParams params =
64+
/// const PlatformNavigationDelegateCreationParams();
65+
///
66+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
67+
/// params = WebKitNavigationDelegateCreationParams
68+
/// .fromPlatformNavigationDelegateCreationParams(
69+
/// params,
70+
/// );
71+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
72+
/// params = AndroidNavigationDelegateCreationParams
73+
/// .fromPlatformNavigationDelegateCreationParams(
74+
/// params,
75+
/// );
76+
/// }
77+
///
78+
/// final NavigationDelegate navigationDelegate =
79+
/// NavigationDelegate.fromPlatformCreationParams(
80+
/// params,
81+
/// );
82+
/// ```
83+
/// {@endtemplate}
3584
NavigationDelegate.fromPlatformCreationParams(
3685
PlatformNavigationDelegateCreationParams params, {
3786
FutureOr<NavigationDecision> Function(NavigationRequest request)?

packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart

+56
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,75 @@ import 'package:flutter/material.dart';
1010
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
1111

1212
import 'navigation_delegate.dart';
13+
import 'webview_widget.dart';
1314

1415
/// Controls a WebView provided by the host platform.
1516
///
1617
/// Pass this to a [WebViewWidget] to display the WebView.
18+
///
19+
/// A [WebViewController] can only be used by a single [WebViewWidget] at a
20+
/// time.
21+
///
22+
/// ## Platform-Specific Features
23+
/// This class contains an underlying implementation provided by the current
24+
/// platform. Once a platform implementation is imported, the examples below
25+
/// can be followed to use features provided by a platform's implementation.
26+
///
27+
/// {@macro webview_flutter.WebViewController.fromPlatformCreationParams}
28+
///
29+
/// Below is an example of accessing the platform-specific implementation for
30+
/// iOS and Android:
31+
///
32+
/// ```dart
33+
/// final WebViewController webViewController = WebViewController();
34+
///
35+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
36+
/// final WebKitWebViewController webKitController =
37+
/// webViewController.platform as WebKitWebViewController;
38+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
39+
/// final AndroidWebViewController androidController =
40+
/// webViewController.platform as AndroidWebViewController;
41+
/// }
42+
/// ```
1743
class WebViewController {
1844
/// Constructs a [WebViewController].
45+
///
46+
/// See [WebViewController.fromPlatformCreationParams] for setting parameters
47+
/// for a specific platform.
1948
WebViewController()
2049
: this.fromPlatformCreationParams(
2150
const PlatformWebViewControllerCreationParams(),
2251
);
2352

2453
/// Constructs a [WebViewController] from creation params for a specific
2554
/// platform.
55+
///
56+
/// {@template webview_flutter.WebViewCookieManager.fromPlatformCreationParams}
57+
/// Below is an example of setting platform-specific creation parameters for
58+
/// iOS and Android:
59+
///
60+
/// ```dart
61+
/// PlatformWebViewControllerCreationParams params =
62+
/// const PlatformWebViewControllerCreationParams();
63+
///
64+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
65+
/// params = WebKitWebViewControllerCreationParams
66+
/// .fromPlatformWebViewControllerCreationParams(
67+
/// params,
68+
/// );
69+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
70+
/// params = AndroidWebViewControllerCreationParams
71+
/// .fromPlatformWebViewControllerCreationParams(
72+
/// params,
73+
/// );
74+
/// }
75+
///
76+
/// final WebViewController webViewController =
77+
/// WebViewController.fromPlatformCreationParams(
78+
/// params,
79+
/// );
80+
/// ```
81+
/// {@endtemplate}
2682
WebViewController.fromPlatformCreationParams(
2783
PlatformWebViewControllerCreationParams params,
2884
) : this.fromPlatform(PlatformWebViewController(params));

packages/webview_flutter/webview_flutter/lib/src/webview_cookie_manager.dart

+52
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,67 @@
55
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
66

77
/// Manages cookies pertaining to all WebViews.
8+
///
9+
/// ## Platform-Specific Features
10+
/// This class contains an underlying implementation provided by the current
11+
/// platform. Once a platform implementation is imported, the examples below
12+
/// can be followed to use features provided by a platform's implementation.
13+
///
14+
/// {@macro webview_flutter.WebViewCookieManager.fromPlatformCreationParams}
15+
///
16+
/// Below is an example of accessing the platform-specific implementation for
17+
/// iOS and Android:
18+
///
19+
/// ```dart
20+
/// final WebViewCookieManager cookieManager = WebViewCookieManager();
21+
///
22+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
23+
/// final WebKitWebViewCookieManager webKitManager =
24+
/// cookieManager.platform as WebKitWebViewCookieManager;
25+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
26+
/// final AndroidWebViewCookieManager androidManager =
27+
/// cookieManager.platform as AndroidWebViewCookieManager;
28+
/// }
29+
/// ```
830
class WebViewCookieManager {
931
/// Constructs a [WebViewCookieManager].
32+
///
33+
/// See [WebViewCookieManager.fromPlatformCreationParams] for setting
34+
/// parameters for a specific platform.
1035
WebViewCookieManager()
1136
: this.fromPlatformCreationParams(
1237
const PlatformWebViewCookieManagerCreationParams(),
1338
);
1439

1540
/// Constructs a [WebViewCookieManager] from creation params for a specific
1641
/// platform.
42+
///
43+
/// {@template webview_flutter.WebViewCookieManager.fromPlatformCreationParams}
44+
/// Below is an example of setting platform-specific creation parameters for
45+
/// iOS and Android:
46+
///
47+
/// ```dart
48+
/// PlatformWebViewCookieManagerCreationParams params =
49+
/// const PlatformWebViewCookieManagerCreationParams();
50+
///
51+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
52+
/// params = WebKitWebViewCookieManagerCreationParams
53+
/// .fromPlatformWebViewCookieManagerCreationParams(
54+
/// params,
55+
/// );
56+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
57+
/// params = AndroidWebViewCookieManagerCreationParams
58+
/// .fromPlatformWebViewCookieManagerCreationParams(
59+
/// params,
60+
/// );
61+
/// }
62+
///
63+
/// final WebViewCookieManager webViewCookieManager =
64+
/// WebViewCookieManager.fromPlatformCreationParams(
65+
/// params,
66+
/// );
67+
/// ```
68+
/// {@endtemplate}
1769
WebViewCookieManager.fromPlatformCreationParams(
1870
PlatformWebViewCookieManagerCreationParams params,
1971
) : this.fromPlatform(PlatformWebViewCookieManager(params));

packages/webview_flutter/webview_flutter/lib/src/webview_widget.dart

+61-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,35 @@ import 'package:webview_flutter_platform_interface/webview_flutter_platform_inte
1010
import 'webview_controller.dart';
1111

1212
/// Displays a native WebView as a Widget.
13+
///
14+
/// ## Platform-Specific Features
15+
/// This class contains an underlying implementation provided by the current
16+
/// platform. Once a platform implementation is imported, the examples below
17+
/// can be followed to use features provided by a platform's implementation.
18+
///
19+
/// {@macro webview_flutter.WebViewWidget.fromPlatformCreationParams}
20+
///
21+
/// Below is an example of accessing the platform-specific implementation for
22+
/// iOS and Android:
23+
///
24+
/// ```dart
25+
/// final WebViewController controller = WebViewController();
26+
///
27+
/// final WebViewWidget webViewWidget = WebViewWidget(controller: controller);
28+
///
29+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
30+
/// final WebKitWebViewWidget webKitWidget =
31+
/// webViewWidget.platform as WebKitWebViewWidget;
32+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
33+
/// final AndroidWebViewWidget androidWidget =
34+
/// webViewWidget.platform as AndroidWebViewWidget;
35+
/// }
36+
/// ```
1337
class WebViewWidget extends StatelessWidget {
1438
/// Constructs a [WebViewWidget].
39+
///
40+
/// See [WebViewWidget.fromPlatformCreationParams] for setting parameters for
41+
/// a specific platform.
1542
WebViewWidget({
1643
Key? key,
1744
required WebViewController controller,
@@ -27,8 +54,40 @@ class WebViewWidget extends StatelessWidget {
2754
),
2855
);
2956

30-
/// Constructs a [WebViewWidget] from creation params for a specific
31-
/// platform.
57+
/// Constructs a [WebViewWidget] from creation params for a specific platform.
58+
///
59+
/// {@template webview_flutter.WebViewWidget.fromPlatformCreationParams}
60+
/// Below is an example of setting platform-specific creation parameters for
61+
/// iOS and Android:
62+
///
63+
/// ```dart
64+
/// final WebViewController controller = WebViewController();
65+
///
66+
/// PlatformWebViewWidgetCreationParams params =
67+
/// PlatformWebViewWidgetCreationParams(
68+
/// controller: controller.platform,
69+
/// layoutDirection: TextDirection.ltr,
70+
/// gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
71+
/// );
72+
///
73+
/// if (WebViewPlatform.instance is WebKitWebViewPlatform) {
74+
/// params = WebKitWebViewWidgetCreationParams
75+
/// .fromPlatformWebViewWidgetCreationParams(
76+
/// params,
77+
/// );
78+
/// } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
79+
/// params = AndroidWebViewWidgetCreationParams
80+
/// .fromPlatformWebViewWidgetCreationParams(
81+
/// params,
82+
/// );
83+
/// }
84+
///
85+
/// final WebViewWidget webViewWidget =
86+
/// WebViewWidget.fromPlatformCreationParams(
87+
/// params: params,
88+
/// );
89+
/// ```
90+
/// {@endtemplate}
3291
WebViewWidget.fromPlatformCreationParams({
3392
Key? key,
3493
required PlatformWebViewWidgetCreationParams params,

packages/webview_flutter/webview_flutter/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter
22
description: A Flutter plugin that provides a WebView widget on Android and iOS.
33
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 4.0.3
5+
version: 4.0.4
66

77
environment:
88
sdk: ">=2.17.0 <3.0.0"

0 commit comments

Comments
 (0)