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

Commit 4b7a63f

Browse files
committed
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.
1 parent c85fa03 commit 4b7a63f

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-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.3.0
2+
3+
* Added `loadFlutterAsset` and `loadHtml` interface methods.
4+
15
## 1.2.0
26

37
* Added `runJavascript` and `runJavascriptReturningResult` interface methods to supersede `evaluateJavascript`.

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> loadFlutterAsset(String assetName) async {
84+
assert(assetName != null);
85+
return _channel.invokeMethod<void>('loadFlutterAsset', assetName);
86+
}
87+
88+
@override
89+
Future<void> loadHtml(
90+
String html, {
91+
String? baseUrl,
92+
}) async {
93+
assert(html != null);
94+
return _channel.invokeMethod<void>('loadHtml', <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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ abstract class WebViewPlatformController {
2323
/// The `handler` parameter must not be null.
2424
WebViewPlatformController(WebViewPlatformCallbacksHandler handler);
2525

26+
/// Loads the Flutter asset specified in the pubspec.yaml file.
27+
///
28+
/// Throws an ArgumentError if [assetName] is not part of the specified assets
29+
/// in the pubspec.yaml file.
30+
Future<void> loadFlutterAsset(
31+
String assetName,
32+
) {
33+
throw UnimplementedError(
34+
"WebView loadFlutterAsset is not implemented on the current platform");
35+
}
36+
37+
/// Loads the supplied HTML.
38+
///
39+
/// The [baseUrl] parameter is used when resolving relative URLs within the
40+
/// HTML string.
41+
Future<void> loadHtml(
42+
String html, {
43+
String? baseUrl,
44+
}) {
45+
throw UnimplementedError(
46+
"WebView loadHtml is not implemented on the current platform");
47+
}
48+
2649
/// Loads the specified URL.
2750
///
2851
/// 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.2.0
7+
version: 1.3.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
@@ -55,6 +55,61 @@ void main() {
5555
log.clear();
5656
});
5757

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

0 commit comments

Comments
 (0)