Skip to content

[webview_flutter] Add listener for content offset #3444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dd1196d
Recreating PR from flutter/plugins
TheVinhLuong Mar 11, 2023
d91b421
Merge branch 'master' into webview-scroll-listener
TheVinhLuong Mar 11, 2023
2a9b827
[webview_android] Remove enableContentOffsetChangedListener
TheVinhLuong Apr 1, 2023
07d30ef
Merge branch 'master' into webview-scroll-listener
TheVinhLuong Apr 1, 2023
5a80183
Fixup based on code review
TheVinhLuong Apr 14, 2023
1ea39bc
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Apr 29, 2023
e6a5c55
Resolve conflict
TheVinhLuong Apr 29, 2023
024e3a7
Fixup based on code review
TheVinhLuong Apr 29, 2023
af3358b
Merge branch 'main' into webview-scroll-listener
TheVinhLuong May 30, 2023
c7b5231
Make dart low-level code for offset changes listener mirror the Andro…
TheVinhLuong May 30, 2023
44ca259
Add iOS implementation
TheVinhLuong Jul 3, 2023
726d1a8
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Aug 1, 2023
ac8eacc
Add iOS related unit test codes
TheVinhLuong Aug 1, 2023
59fa39d
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Aug 1, 2023
cfbdee9
Fix CI failing checks
TheVinhLuong Aug 1, 2023
f24f661
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Aug 6, 2023
562bd04
Apply changes based on review comments
TheVinhLuong Aug 27, 2023
1b5e9d3
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Aug 27, 2023
d256a35
Fix code based on code review
TheVinhLuong Sep 26, 2023
df20af9
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Sep 26, 2023
3c2a2ca
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Sep 30, 2023
0c6aac1
Merge branch 'main' of github.com:flutter/packages into webview-scrol…
bparrishMines Oct 3, 2023
ac9dcee
some improvements
bparrishMines Oct 5, 2023
5dfa5b2
switch to doubles
bparrishMines Oct 5, 2023
94eed4b
update webview_flutter
bparrishMines Oct 5, 2023
b0153f9
formatting
bparrishMines Oct 5, 2023
a37dc83
Merge branch 'main' of github.com:flutter/packages into webview-scrol…
bparrishMines Oct 5, 2023
4348c1b
pubspec ordering and doc
bparrishMines Oct 5, 2023
48aa520
Merge branch 'main' of github.com:flutter/packages into webview-scrol…
bparrishMines Oct 11, 2023
02a4758
PR feedback
bparrishMines Oct 11, 2023
1233235
Fix integration test
TheVinhLuong Oct 14, 2023
2c39f56
Merge branch 'main' of github.com:flutter/packages into webview-scrol…
bparrishMines Nov 7, 2023
79e6c2c
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Dec 12, 2023
0d8668b
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Feb 9, 2024
a2400d2
Updates minimum supported SDK version to Flutter 3.16.6/Dart 3.2.3. U…
TheVinhLuong Feb 9, 2024
7f8d4c6
Fixup based on code review
TheVinhLuong Feb 9, 2024
3ea68d8
Merge branch 'main' into webview-scroll-listener
TheVinhLuong Feb 9, 2024
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
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
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