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

[webview_flutter_web] Adds auto registration of the WebViewPlatform implementation #6886

Merged
merged 3 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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();
...
```
Original file line number Diff line number Diff line change
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();
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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>());
});
});
}