From 937d717a7026a6c39f44e4af7bf93ee031afb106 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Thu, 4 Nov 2021 16:04:09 +0100 Subject: [PATCH] [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 --- .../CHANGELOG.md | 4 ++ .../webview_method_channel.dart | 18 ++++++ .../webview_platform_controller.dart | 26 +++++++++ .../pubspec.yaml | 2 +- .../webview_method_channel_test.dart | 55 +++++++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) diff --git a/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md b/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md index 0579121be82b..4c7434a86b41 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.0 + +* Added `loadFile` and `loadHtml` interface methods. + ## 1.3.0 * Added `loadRequest` method to platform interface. diff --git a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart index a88479e7e024..8df9f4c62b33 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart +++ b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart @@ -79,6 +79,24 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController { ); } + @override + Future loadFile(String absoluteFilePath) async { + assert(absoluteFilePath != null); + return _channel.invokeMethod('loadFile', absoluteFilePath); + } + + @override + Future loadHtmlString( + String html, { + String? baseUrl, + }) async { + assert(html != null); + return _channel.invokeMethod('loadHtmlString', { + 'html': html, + 'baseUrl': baseUrl, + }); + } + @override Future loadUrl( String url, diff --git a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart index 806f9500dbad..cfc817450372 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart +++ b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart @@ -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 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 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 diff --git a/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml b/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml index 508af0ef0862..4a4746d8ab68 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml @@ -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" diff --git a/packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart b/packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart index b85b7b3df286..396013535aa9 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart +++ b/packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart @@ -57,6 +57,61 @@ void main() { log.clear(); }); + test('loadFile', () async { + await webViewPlatform.loadFile( + '/folder/asset.html', + ); + + expect( + log, + [ + isMethodCall( + 'loadFile', + arguments: '/folder/asset.html', + ), + ], + ); + }); + + test('loadHtmlString without base URL', () async { + await webViewPlatform.loadHtmlString( + 'Test HTML string', + ); + + expect( + log, + [ + isMethodCall( + 'loadHtmlString', + arguments: { + 'html': 'Test HTML string', + 'baseUrl': null, + }, + ), + ], + ); + }); + + test('loadHtmlString without base URL', () async { + await webViewPlatform.loadHtmlString( + 'Test HTML string', + baseUrl: 'https://flutter.dev', + ); + + expect( + log, + [ + isMethodCall( + 'loadHtmlString', + arguments: { + 'html': 'Test HTML string', + 'baseUrl': 'https://flutter.dev', + }, + ), + ], + ); + }); + test('loadUrl with headers', () async { await webViewPlatform.loadUrl( 'https://test.url',