Skip to content

Commit

Permalink
[webview_flutter] Add interface methods to load local files and HTML …
Browse files Browse the repository at this point in the history
…strings. (flutter#4446)

* Add methods to load HTML and Flutter assets.

Adds methods to the webview_flutter_platform_interface to support
loading content from Flutter asset defined in the pubspec.yaml of
directly from HTML string.

* Renamed loadHtml to loadHtmlString

* Support loading arbitrary files instead of only Flutter assets

* Update changelog description

* Fix formatting

* Fix formatting
  • Loading branch information
mvanbeusekom committed Nov 4, 2021
1 parent 2e102b8 commit e39a584
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.4.0

* Added `loadFile` and `loadHtml` interface methods.

## 1.3.0

* Added `loadRequest` method to platform interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
);
}

@override
Future<void> loadFile(String absoluteFilePath) async {
assert(absoluteFilePath != null);
return _channel.invokeMethod<void>('loadFile', absoluteFilePath);
}

@override
Future<void> loadHtmlString(
String html, {
String? baseUrl,
}) async {
assert(html != null);
return _channel.invokeMethod<void>('loadHtmlString', <String, dynamic>{
'html': html,
'baseUrl': baseUrl,
});
}

@override
Future<void> loadUrl(
String url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ abstract class WebViewPlatformController {
/// The `handler` parameter must not be null.
WebViewPlatformController(WebViewPlatformCallbacksHandler handler);

/// Loads the file located on the specified [absoluteFilePath].
///
/// The [absoluteFilePath] parameter should contain the absolute path to the
/// file as it is stored on the device. For example:
/// `/Users/username/Documents/www/index.html`.
///
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
Future<void> loadFile(
String absoluteFilePath,
) {
throw UnimplementedError(
"WebView loadFlutterAsset is not implemented on the current platform");
}

/// Loads the supplied HTML string.
///
/// The [baseUrl] parameter is used when resolving relative URLs within the
/// HTML string.
Future<void> loadHtmlString(
String html, {
String? baseUrl,
}) {
throw UnimplementedError(
"WebView loadHtmlString is not implemented on the current platform");
}

/// Loads the specified URL.
///
/// If `headers` is not null and the URL is an HTTP URL, the key value paris in `headers` will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.3.0
version: 1.4.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,61 @@ void main() {
log.clear();
});

test('loadFile', () async {
await webViewPlatform.loadFile(
'/folder/asset.html',
);

expect(
log,
<Matcher>[
isMethodCall(
'loadFile',
arguments: '/folder/asset.html',
),
],
);
});

test('loadHtmlString without base URL', () async {
await webViewPlatform.loadHtmlString(
'Test HTML string',
);

expect(
log,
<Matcher>[
isMethodCall(
'loadHtmlString',
arguments: <String, String?>{
'html': 'Test HTML string',
'baseUrl': null,
},
),
],
);
});

test('loadHtmlString without base URL', () async {
await webViewPlatform.loadHtmlString(
'Test HTML string',
baseUrl: 'https://flutter.dev',
);

expect(
log,
<Matcher>[
isMethodCall(
'loadHtmlString',
arguments: <String, String?>{
'html': 'Test HTML string',
'baseUrl': 'https://flutter.dev',
},
),
],
);
});

test('loadUrl with headers', () async {
await webViewPlatform.loadUrl(
'https://test.url',
Expand Down

0 comments on commit e39a584

Please sign in to comment.