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

Commit e39a584

Browse files
authored
[webview_flutter] Add interface methods to load local files and HTML strings. (#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
1 parent 2e102b8 commit e39a584

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.4.0
2+
3+
* Added `loadFile` and `loadHtml` interface methods.
4+
15
## 1.3.0
26

37
* Added `loadRequest` method to platform interface.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
7979
);
8080
}
8181

82+
@override
83+
Future<void> loadFile(String absoluteFilePath) async {
84+
assert(absoluteFilePath != null);
85+
return _channel.invokeMethod<void>('loadFile', absoluteFilePath);
86+
}
87+
88+
@override
89+
Future<void> loadHtmlString(
90+
String html, {
91+
String? baseUrl,
92+
}) async {
93+
assert(html != null);
94+
return _channel.invokeMethod<void>('loadHtmlString', <String, dynamic>{
95+
'html': html,
96+
'baseUrl': baseUrl,
97+
});
98+
}
99+
82100
@override
83101
Future<void> loadUrl(
84102
String url,

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ abstract class WebViewPlatformController {
2323
/// The `handler` parameter must not be null.
2424
WebViewPlatformController(WebViewPlatformCallbacksHandler handler);
2525

26+
/// Loads the file located on the specified [absoluteFilePath].
27+
///
28+
/// The [absoluteFilePath] parameter should contain the absolute path to the
29+
/// file as it is stored on the device. For example:
30+
/// `/Users/username/Documents/www/index.html`.
31+
///
32+
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
33+
Future<void> loadFile(
34+
String absoluteFilePath,
35+
) {
36+
throw UnimplementedError(
37+
"WebView loadFlutterAsset is not implemented on the current platform");
38+
}
39+
40+
/// Loads the supplied HTML string.
41+
///
42+
/// The [baseUrl] parameter is used when resolving relative URLs within the
43+
/// HTML string.
44+
Future<void> loadHtmlString(
45+
String html, {
46+
String? baseUrl,
47+
}) {
48+
throw UnimplementedError(
49+
"WebView loadHtmlString is not implemented on the current platform");
50+
}
51+
2652
/// Loads the specified URL.
2753
///
2854
/// If `headers` is not null and the URL is an HTTP URL, the key value paris in `headers` will

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 1.3.0
7+
version: 1.4.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,61 @@ void main() {
5757
log.clear();
5858
});
5959

60+
test('loadFile', () async {
61+
await webViewPlatform.loadFile(
62+
'/folder/asset.html',
63+
);
64+
65+
expect(
66+
log,
67+
<Matcher>[
68+
isMethodCall(
69+
'loadFile',
70+
arguments: '/folder/asset.html',
71+
),
72+
],
73+
);
74+
});
75+
76+
test('loadHtmlString without base URL', () async {
77+
await webViewPlatform.loadHtmlString(
78+
'Test HTML string',
79+
);
80+
81+
expect(
82+
log,
83+
<Matcher>[
84+
isMethodCall(
85+
'loadHtmlString',
86+
arguments: <String, String?>{
87+
'html': 'Test HTML string',
88+
'baseUrl': null,
89+
},
90+
),
91+
],
92+
);
93+
});
94+
95+
test('loadHtmlString without base URL', () async {
96+
await webViewPlatform.loadHtmlString(
97+
'Test HTML string',
98+
baseUrl: 'https://flutter.dev',
99+
);
100+
101+
expect(
102+
log,
103+
<Matcher>[
104+
isMethodCall(
105+
'loadHtmlString',
106+
arguments: <String, String?>{
107+
'html': 'Test HTML string',
108+
'baseUrl': 'https://flutter.dev',
109+
},
110+
),
111+
],
112+
);
113+
});
114+
60115
test('loadUrl with headers', () async {
61116
await webViewPlatform.loadUrl(
62117
'https://test.url',

0 commit comments

Comments
 (0)