Skip to content

Commit

Permalink
[webview_flutter_web] Adds auto registration of the WebViewPlatform
Browse files Browse the repository at this point in the history
… implementation (flutter#6886)

* Adss auto reg

* test for registration

* update readme
  • Loading branch information
bparrishMines committed Dec 28, 2022
1 parent 417b370 commit 2d66f30
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 56 deletions.
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter_web/CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.2.1

* Adds auto registration of the `WebViewPlatform` implementation.

## 0.2.0

* **BREAKING CHANGE** Updates platform implementation to `2.0.0` release of
Expand Down
55 changes: 1 addition & 54 deletions packages/webview_flutter/webview_flutter_web/README.md
Expand Up @@ -18,59 +18,6 @@ yet, so it currently requires extra setup to use:
* [Add this package](https://pub.dev/packages/webview_flutter_web/install)
as an explicit dependency of your project, in addition to depending on
`webview_flutter`.
* Register `WebWebViewPlatform` as the `WebViewPlatform.instance` before creating a
`WebView`. See below for examples.

Once those steps below are complete, the APIs from `webview_flutter` listed
Once the step above is complete, the APIs from `webview_flutter` listed
above can be used as normal on web.

### Registering the implementation

Before creating a `WebView` (for instance, at the start of `main`), you will
need to register the web implementation.

#### Web-only project example

```dart
...
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_web/webview_flutter_web.dart';
main() {
WebViewPlatform.instance = WebWebViewPlatform();
...
```

#### Multi-platform project example

If your project supports platforms other than web, you will need to use a
conditional import to avoid directly including `webview_flutter_web.dart` on
non-web platforms. For example:

`register_web_webview.dart`:
```dart
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_web/webview_flutter_web.dart';
void registerWebViewWebImplementation() {
WebViewPlatform.instance = WebWebViewPlatform();
}
```

`register_web_webview_stub.dart`:
```dart
void registerWebViewWebImplementation() {
// No-op.
}
```

`main.dart`:
```dart
...
import 'register_web_webview_stub.dart'
if (dart.library.html) 'register_web.dart';
main() {
registerWebViewWebImplementation();
...
```
Expand Up @@ -24,5 +24,7 @@ class WebWebViewPlatform extends WebViewPlatform {
}

/// Gets called when the plugin is registered.
static void registerWith(Registrar registrar) {}
static void registerWith(Registrar registrar) {
WebViewPlatform.instance = WebWebViewPlatform();
}
}
2 changes: 1 addition & 1 deletion packages/webview_flutter/webview_flutter_web/pubspec.yaml
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_web
description: A Flutter plugin that provides a WebView widget on web.
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 0.2.0
version: 0.2.1

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down
@@ -0,0 +1,17 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
import 'package:webview_flutter_web/webview_flutter_web.dart';

void main() {
group('WebWebViewPlatform', () {
test('registerWith', () {
WebWebViewPlatform.registerWith(Registrar());
expect(WebViewPlatform.instance, isA<WebWebViewPlatform>());
});
});
}

0 comments on commit 2d66f30

Please sign in to comment.