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

[webview_flutter] Implements the loadFlutterAsset in the app facing package. #4593

Merged
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/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.8.0

* Adds support for the `loadFlutterAsset` method.

## 2.7.0

* Adds `setCookie` to CookieManager.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<!-- 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. -->
<html lang="en">
<head>
<title>Load file or HTML string example</title>
<link rel="stylesheet" href="styles/style.css" />
</head>
<body>

<h1>Local demo page</h1>
<p>
This is an example page used to demonstrate how to load a local file or HTML
string using the <a href="https://pub.dev/packages/webview_flutter">Flutter
webview</a> plugin.
</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: blue;
}
13 changes: 13 additions & 0 deletions packages/webview_flutter/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ enum MenuOptions {
navigationDelegate,
doPostRequest,
loadLocalFile,
loadFlutterAsset,
loadHtmlString,
transparentBackground,
setCookie,
Expand Down Expand Up @@ -227,6 +228,9 @@ class SampleMenu extends StatelessWidget {
case MenuOptions.loadLocalFile:
_onLoadLocalFileExample(controller.data!, context);
break;
case MenuOptions.loadFlutterAsset:
_onLoadFlutterAssetExample(controller.data!, context);
break;
case MenuOptions.loadHtmlString:
_onLoadHtmlStringExample(controller.data!, context);
break;
Expand Down Expand Up @@ -280,6 +284,10 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.loadLocalFile,
child: Text('Load local file'),
),
const PopupMenuItem<MenuOptions>(
value: MenuOptions.loadFlutterAsset,
child: Text('Load Flutter Asset'),
),
const PopupMenuItem<MenuOptions>(
key: ValueKey<String>('ShowTransparentBackgroundExample'),
value: MenuOptions.transparentBackground,
Expand Down Expand Up @@ -392,6 +400,11 @@ class SampleMenu extends StatelessWidget {
await controller.loadFile(pathToIndex);
}

Future<void> _onLoadFlutterAssetExample(
WebViewController controller, BuildContext context) async {
await controller.loadFlutterAsset('assets/www/index.html');
}

Future<void> _onLoadHtmlStringExample(
WebViewController controller, BuildContext context) async {
await controller.loadHtmlString(kLocalExamplePage);
Expand Down
2 changes: 2 additions & 0 deletions packages/webview_flutter/webview_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ flutter:
assets:
- assets/sample_audio.ogg
- assets/sample_video.mp4
- assets/www/index.html
- assets/www/styles/style.css
9 changes: 9 additions & 0 deletions packages/webview_flutter/webview_flutter/lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ class WebViewController {
return _webViewPlatformController.loadFile(absoluteFilePath);
}

/// Loads the Flutter asset specified in the pubspec.yaml file.
///
/// Throws an ArgumentError if [key] is not part of the specified assets
/// in the pubspec.yaml file.
Future<void> loadFlutterAsset(String key) {
assert(key.isNotEmpty);
return _webViewPlatformController.loadFlutterAsset(key);
}

/// Loads the supplied HTML string.
///
/// The [baseUrl] parameter is used when resolving relative URLs within the
Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
repository: https://github.com/flutter/plugins/tree/master/packages/webview_flutter/webview_flutter
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 2.7.0
version: 2.8.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -21,7 +21,7 @@ dependencies:
sdk: flutter
webview_flutter_android: ^2.8.0
webview_flutter_platform_interface: ^1.8.0
webview_flutter_wkwebview: ^2.6.0
webview_flutter_wkwebview: ^2.7.0

dev_dependencies:
build_runner: ^2.1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,40 @@ void main() {
expect(() => controller!.loadFile(''), throwsAssertionError);
});

testWidgets('Load Flutter asset', (WidgetTester tester) async {
WebViewController? controller;
await tester.pumpWidget(
WebView(
onWebViewCreated: (WebViewController webViewController) {
controller = webViewController;
},
),
);

expect(controller, isNotNull);

await controller!.loadFlutterAsset('assets/index.html');

verify(mockWebViewPlatformController.loadFlutterAsset(
'assets/index.html',
));
});

testWidgets('Load Flutter asset with empty key', (WidgetTester tester) async {
WebViewController? controller;
await tester.pumpWidget(
WebView(
onWebViewCreated: (WebViewController webViewController) {
controller = webViewController;
},
),
);

expect(controller, isNotNull);

expect(() => controller!.loadFlutterAsset(''), throwsAssertionError);
});

testWidgets('Load HTML string without base URL', (WidgetTester tester) async {
WebViewController? controller;
await tester.pumpWidget(
Expand Down