Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.6.4

* Adds support for `setOnContentOffsetChanged` method to the `AndroidWebViewController`.

## 3.6.3

* Updates gradle, AGP and fixes some lint errors.
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.annotation.VisibleForTesting;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.WebViewFlutterApi;
import java.util.Objects;

/**
* Flutter API implementation for `WebView`.
Expand Down Expand Up @@ -56,4 +57,16 @@ public void create(@NonNull WebView instance, @NonNull WebViewFlutterApi.Reply<V
void setApi(@NonNull WebViewFlutterApi api) {
this.api = api;
}

public void onScrollChanged(
@NonNull WebView instance,
@NonNull Long xArg,
@NonNull Long yArg,
@NonNull WebViewFlutterApi.Reply<Void> callback) {
api.onScrollPosChange(
Objects.requireNonNull(instanceManager.getIdentifierForStrongReference(instance)),
xArg,
yArg,
callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public WebChromeClient getWebChromeClient() {
return currentWebChromeClient;
}

@Override
protected void onScrollChanged(int l, int t, int oldL, int oldT) {
super.onScrollChanged(l, t, oldL, oldT);
api.onScrollChanged(
this, (long) l, (long) t, reply -> {});
}

/**
* Flutter API used to send messages back to Dart.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,25 @@ public void setImportantForAutofillForParentFlutterView() {

verify(mockFlutterView).setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_YES);
}

@Test
public void onScrollChanged() {
final InstanceManager instanceManager = InstanceManager.create(identifier -> {});

final WebViewFlutterApiImpl flutterApiImpl =
new WebViewFlutterApiImpl(mockBinaryMessenger, instanceManager);

final WebViewFlutterApi mockFlutterApi = mock(WebViewFlutterApi.class);
flutterApiImpl.setApi(mockFlutterApi);
flutterApiImpl.create(mockWebView, reply -> {});

flutterApiImpl.onScrollChanged(mockWebView, 0L, 1L, reply -> {});

final long instanceIdentifier =
Objects.requireNonNull(instanceManager.getIdentifierForStrongReference(mockWebView));
verify(mockFlutterApi)
.onScrollPosChange(eq(instanceIdentifier), eq(0L), eq(1L), any());

instanceManager.stopFinalizationListener();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ Future<void> main() async {
});

group('Programmatic Scroll', () {
testWidgets('setAndGetScrollPosition', (WidgetTester tester) async {
testWidgets('setAndGetAndListenScrollPosition',
(WidgetTester tester) async {
const String scrollTestPage = '''
<!DOCTYPE html>
<html>
Expand All @@ -683,6 +684,7 @@ Future<void> main() async {
base64Encode(const Utf8Encoder().convert(scrollTestPage));

final Completer<void> pageLoaded = Completer<void>();
Completer<ContentOffsetChange> offsetsCompleter = Completer<ContentOffsetChange>();
final PlatformWebViewController controller = PlatformWebViewController(
const PlatformWebViewControllerCreationParams(),
)
Expand All @@ -698,7 +700,12 @@ Future<void> main() async {
'data:text/html;charset=utf-8;base64,$scrollTestPageBase64',
),
),
);
)
..setOnContentOffsetChanged(
(ContentOffsetChange contentOffsetChange) {
print('comppp $contentOffsetChange');
offsetsCompleter.complete(contentOffsetChange);
});

await tester.pumpWidget(Builder(
builder: (BuildContext context) {
Expand All @@ -722,17 +729,21 @@ Future<void> main() async {
// time to settle.
expect(scrollPos.dx, isNot(X_SCROLL));
expect(scrollPos.dy, isNot(Y_SCROLL));

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

await expectLater(
offsetsCompleter.future.then((ContentOffsetChange contentOffsetChange) => <int>[contentOffsetChange.x, contentOffsetChange.y]), completion(<int>[X_SCROLL, Y_SCROLL]));

// Check scrollBy() (on top of scrollTo())
offsetsCompleter = Completer<ContentOffsetChange>();
await controller.scrollBy(X_SCROLL, Y_SCROLL);
scrollPos = await controller.getScrollPosition();
expect(scrollPos.dx, X_SCROLL * 2);
expect(scrollPos.dy, Y_SCROLL * 2);
await expectLater(
offsetsCompleter.future.then((ContentOffsetChange contentOffsetChange) => <int>[contentOffsetChange.x, contentOffsetChange.y]), completion(<int>[X_SCROLL * 2, Y_SCROLL * 2]));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ Page resource error:
)
..loadRequest(LoadRequestParams(
uri: Uri.parse('https://flutter.dev'),
));
))
..setOnContentOffsetChanged((ContentOffsetChange contentOffsetChange) {
debugPrint(
'Scroll offset change to x = ${contentOffsetChange.x}, y = ${contentOffsetChange.y}');
});
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ flutter:
- assets/sample_video.mp4
- assets/www/index.html
- assets/www/styles/style.css


# FOR TESTING ONLY. DO NOT MERGE.
dependency_overrides:
webview_flutter_platform_interface:
path: ../../../webview_flutter/webview_flutter_platform_interface
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'android_webview.dart' as android_webview;

/// Handles constructing objects and calling static methods for the Android
Expand All @@ -25,7 +27,9 @@ class AndroidWebViewProxy {
});

/// Constructs a [android_webview.WebView].
final android_webview.WebView Function() createAndroidWebView;
final android_webview.WebView Function(
{Function(ContentOffsetChange contentOffsetChange)?
onScrollChanged}) createAndroidWebView;

/// Constructs a [android_webview.WebChromeClient].
final android_webview.WebChromeClient Function({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show BinaryMessenger;

import 'package:flutter/widgets.dart' show WidgetsFlutterBinding;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'android_webview.g.dart';
import 'android_webview_api_impls.dart';
Expand Down Expand Up @@ -89,6 +90,7 @@ class WebView extends JavaObject {
/// any effect and should not be exposed publicly. More info here:
/// https://github.com/flutter/flutter/issues/108106
WebView({
this.onScrollChanged,
@visibleForTesting super.binaryMessenger,
@visibleForTesting super.instanceManager,
}) : super.detached() {
Expand All @@ -101,6 +103,7 @@ class WebView extends JavaObject {
/// create copies.
@protected
WebView.detached({
this.onScrollChanged,
super.binaryMessenger,
super.instanceManager,
}) : super.detached();
Expand All @@ -112,6 +115,9 @@ class WebView extends JavaObject {
/// The [WebSettings] object used to control the settings for this WebView.
late final WebSettings settings = WebSettings(this);

/// The [ScrollChangedCallback] object used to listen for scroll changed events.
final Function(ContentOffsetChange contentOffsetChange)? onScrollChanged;

/// Enables debugging of web contents (HTML / CSS / JavaScript) loaded into any WebViews of this application.
///
/// This flag can be enabled in order to facilitate debugging of web layouts
Expand Down Expand Up @@ -400,6 +406,7 @@ class WebView extends JavaObject {
@override
WebView copy() {
return WebView.detached(
onScrollChanged: onScrollChanged,
binaryMessenger: _api.binaryMessenger,
instanceManager: _api.instanceManager,
);
Expand Down
Loading