Skip to content

Commit 03b33b1

Browse files
authored
[webview_flutter_platform_interface] Provide flexible API for loading local HTML files (#9697)
[https://github.com/flutter/flutter/issues/136479](https://github.com/flutter/flutter/issues/136479) These are the platform interface changes, after the main PR was approved. [https://github.com/flutter/packages/pull/8787](https://github.com/flutter/packages/pull/8787) What was changed: * Added `LoadFileParams` to specify parameters for loading local HTML pages. * `PlatformWebViewController` class extended with the new method `loadFileWithParams(LoadFileParams)`. * Unit tests for the new functionality of the `PlatformWebViewController` were added. ## Pre-Review Checklist
1 parent b986458 commit 03b33b1

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-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+
## 2.14.0
2+
3+
* Adds `PlatformWebViewController.loadFileWithParams(LoadFileParams)` to support loading local HTML files with platform-specific parameters.
4+
15
## 2.13.1
26

37
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ abstract class PlatformWebViewController extends PlatformInterface {
6161
'loadFile is not implemented on the current platform');
6262
}
6363

64+
/// Loads a local HTML file using the provided [params].
65+
///
66+
/// The [params.absoluteFilePath] should contain the absolute path to the file
67+
/// on the device.
68+
/// For example: `/Users/username/Documents/www/index.html`.
69+
///
70+
/// Platform-specific implementations may extend [LoadFileParams] to support
71+
/// additional parameters, such as iOS/macOS-specific read access options.
72+
Future<void> loadFileWithParams(
73+
LoadFileParams params,
74+
) {
75+
return loadFile(params.absoluteFilePath);
76+
}
77+
6478
/// Loads the Flutter asset specified in the pubspec.yaml file.
6579
///
6680
/// Throws an ArgumentError if [key] is not part of the specified assets
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
7+
/// Object specifying parameters for loading a local HTML file into a web view.
8+
///
9+
/// Platform-specific implementations can add additional fields by extending
10+
/// this class.
11+
///
12+
/// This example demonstrates how to extend [LoadFileParams] to provide
13+
/// additional platform-specific parameters.
14+
///
15+
/// When extending [LoadFileParams], additional parameters should always accept
16+
/// `null` or have a default value to prevent breaking changes.
17+
///
18+
/// ```dart
19+
/// class WebKitLoadFileParams extends LoadFileParams {
20+
/// const WebKitLoadFileParams({
21+
/// required super.absoluteFilePath,
22+
/// required this.readAccessPath,
23+
/// });
24+
///
25+
/// /// The directory to which the WebView is granted read access.
26+
/// final String readAccessPath;
27+
/// }
28+
/// ```
29+
@immutable
30+
base class LoadFileParams {
31+
/// Creates a new [LoadFileParams] object.
32+
const LoadFileParams({
33+
required this.absoluteFilePath,
34+
});
35+
36+
/// The path to the local HTML file to be loaded.
37+
final String absoluteFilePath;
38+
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export 'javascript_dialog_request.dart';
99
export 'javascript_log_level.dart';
1010
export 'javascript_message.dart';
1111
export 'javascript_mode.dart';
12+
export 'load_file_params.dart';
1213
export 'load_request_params.dart';
1314
export 'navigation_decision.dart';
1415
export 'navigation_request.dart';

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/packages/tree/main/packages/webview_flutt
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: 2.13.1
7+
version: 2.14.0
88

99
environment:
1010
sdk: ^3.6.0

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ void main() {
6969
);
7070
});
7171

72+
test('loadFileWithParams redirects to loadFile with correct path', () async {
73+
final LoadFileSpyPlatformWebViewController controller =
74+
LoadFileSpyPlatformWebViewController(
75+
const PlatformWebViewControllerCreationParams(),
76+
);
77+
78+
const String testPath = 'file:///test/index.html';
79+
const LoadFileParams params = LoadFileParams(
80+
absoluteFilePath: testPath,
81+
);
82+
83+
await controller.loadFileWithParams(params);
84+
85+
expect(controller.loadFilePath, equals(testPath));
86+
});
87+
7288
test(
7389
'Default implementation of loadFlutterAsset should throw unimplemented error',
7490
() {
@@ -528,6 +544,17 @@ class ExtendsPlatformWebViewController extends PlatformWebViewController {
528544
ExtendsPlatformWebViewController(super.params) : super.implementation();
529545
}
530546

547+
class LoadFileSpyPlatformWebViewController extends PlatformWebViewController {
548+
LoadFileSpyPlatformWebViewController(super.params) : super.implementation();
549+
550+
String? loadFilePath;
551+
552+
@override
553+
Future<void> loadFile(String absoluteFilePath) async {
554+
loadFilePath = absoluteFilePath;
555+
}
556+
}
557+
531558
// ignore: must_be_immutable
532559
class MockLoadRequestParamsDelegate extends Mock
533560
with

0 commit comments

Comments
 (0)