Skip to content

Commit

Permalink
[webview_flutter] Add listener for content offset (#3444)
Browse files Browse the repository at this point in the history
## Description
This PR is created from [the previous PR in flutter/plugin](flutter/plugins#2107)

## Related Issues
flutter/flutter#31027
  • Loading branch information
TheVinhLuong authored Feb 9, 2024
1 parent c269727 commit b58b33c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.7.0

* Adds support to track scroll position changes.
* Updates minimum supported SDK version to Flutter 3.16.6/Dart 3.2.3.

## 4.6.0

* Adds support for custom handling of JavaScript dialogs. See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,16 @@ Future<void> main() async {

final Completer<void> pageLoaded = Completer<void>();
final WebViewController controller = WebViewController();
ScrollPositionChange? recordedPosition;
unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted));
unawaited(controller.setNavigationDelegate(NavigationDelegate(
onPageFinished: (_) => pageLoaded.complete(),
)));
unawaited(controller.setOnScrollPositionChange(
(ScrollPositionChange contentOffsetChange) {
recordedPosition = contentOffsetChange;
}));

unawaited(controller.loadRequest(Uri.parse(
'data:text/html;charset=utf-8;base64,$scrollTestPageBase64',
)));
Expand All @@ -527,17 +533,23 @@ Future<void> main() async {
// time to settle.
expect(scrollPos.dx, isNot(X_SCROLL));
expect(scrollPos.dy, isNot(Y_SCROLL));
expect(recordedPosition?.x, isNot(X_SCROLL));
expect(recordedPosition?.y, isNot(Y_SCROLL));

await controller.scrollTo(X_SCROLL, Y_SCROLL);
scrollPos = await controller.getScrollPosition();
expect(scrollPos.dx, X_SCROLL);
expect(scrollPos.dy, Y_SCROLL);
expect(recordedPosition?.x, X_SCROLL);
expect(recordedPosition?.y, Y_SCROLL);

// Check scrollBy() (on top of scrollTo())
await controller.scrollBy(X_SCROLL, Y_SCROLL);
scrollPos = await controller.getScrollPosition();
expect(scrollPos.dx, X_SCROLL * 2);
expect(scrollPos.dy, Y_SCROLL * 2);
expect(recordedPosition?.x, X_SCROLL * 2);
expect(recordedPosition?.y, Y_SCROLL * 2);
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/webview_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
webview_flutter_android: ^3.14.0
webview_flutter_wkwebview: ^3.11.0
webview_flutter_android: ^3.15.0
webview_flutter_wkwebview: ^3.12.0

dev_dependencies:
build_runner: ^2.1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ class WebViewController {
Future<String?> getUserAgent() {
return platform.getUserAgent();
}

/// Sets a listener for scroll position changes.
Future<void> setOnScrollPositionChange(
void Function(ScrollPositionChange change)? onScrollPositionChange,
) {
return platform.setOnScrollPositionChange(onScrollPositionChange);
}
}

/// Permissions request when web content requests access to protected resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export 'package:webview_flutter_platform_interface/webview_flutter_platform_inte
PlatformWebViewPermissionRequest,
PlatformWebViewWidgetCreationParams,
ProgressCallback,
ScrollPositionChange,
UrlChange,
WebResourceError,
WebResourceErrorCallback,
Expand Down
6 changes: 3 additions & 3 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/packages/tree/main/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: 4.6.0
version: 4.7.0

environment:
sdk: ^3.2.3
Expand All @@ -19,9 +19,9 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_android: ^3.14.0
webview_flutter_android: ^3.15.0
webview_flutter_platform_interface: ^2.10.0
webview_flutter_wkwebview: ^3.11.0
webview_flutter_wkwebview: ^3.12.0

dev_dependencies:
build_runner: ^2.1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,24 @@ void main() {
);
await expectLater(webViewController.getUserAgent(), completion(userAgent));
});

test('setOnScrollPositionChange', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();

final WebViewController webViewController = WebViewController.fromPlatform(
mockPlatformWebViewController,
);

void onScrollPositionChange(ScrollPositionChange change) {}

await webViewController.setOnScrollPositionChange(onScrollPositionChange);

verify(
mockPlatformWebViewController
.setOnScrollPositionChange(onScrollPositionChange),
);
});
}

class TestPlatformWebViewPermissionRequest
Expand Down

0 comments on commit b58b33c

Please sign in to comment.