This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter] Add listener for scroll position change in Android #2107
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b65687d
Implement listener for scroll position change
TheVinhLuong de090aa
Merge branch 'main' into webview-scroll
TheVinhLuong f0755b5
Refactor scroll listener for Android side based on feedback
TheVinhLuong 42f156e
Merge branch 'main' into webview-scroll
TheVinhLuong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| .atom/ | ||
| .idea/ | ||
| .vscode/ | ||
| **/.fvm/ | ||
|
|
||
| .packages | ||
| .pub/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/webview_flutter/webview_flutter_android/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...android/src/main/java/io/flutter/plugins/webviewflutter/ContentOffsetChangedListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // 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. | ||
|
|
||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| public interface ContentOffsetChangedListener { | ||
| void onContentOffsetChange(int left, int top, int oldLeft, int oldTop); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...r_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewExtendedApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // 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. | ||
|
|
||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import androidx.annotation.Nullable; | ||
| /** Define extending APIs for {@link android.webkit.WebView} */ | ||
| public interface WebViewExtendedApi { | ||
| void setContentOffsetChangedListener( | ||
| @Nullable ContentOffsetChangedListener contentOffsetChangedListener); | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
...ndroid/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterApiImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // 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. | ||
|
|
||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import android.webkit.WebView; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.WebViewFlutterApi; | ||
|
|
||
| /** | ||
| * Flutter Api implementation for {@link ContentOffsetChangedListener}. | ||
| * | ||
| * <p>Passes arguments of callbacks methods from a {@link ContentOffsetChangedListener} to Dart. | ||
| */ | ||
| public class WebViewFlutterApiImpl extends WebViewFlutterApi { | ||
| private final InstanceManager instanceManager; | ||
|
|
||
| /** | ||
| * Creates a Flutter api that sends messages to Dart. | ||
| * | ||
| * @param binaryMessenger handles sending messages to Dart | ||
| * @param instanceManager maintains instances stored to communicate with Dart objects | ||
| */ | ||
| public WebViewFlutterApiImpl(BinaryMessenger binaryMessenger, InstanceManager instanceManager) { | ||
| super(binaryMessenger); | ||
| this.instanceManager = instanceManager; | ||
| } | ||
|
|
||
| /** Passes arguments from {@link ContentOffsetChangedListener#onContentOffsetChange} to Dart. */ | ||
| public void onScrollPosChange( | ||
| WebView webView, long x, long y, long oldX, long oldY, Reply<Void> callback) { | ||
| final Long webViewIdentifier = instanceManager.getIdentifierForStrongReference(webView); | ||
| if (webViewIdentifier == null) { | ||
| throw new IllegalStateException("Could not find identifier for WebView."); | ||
| } | ||
| onScrollPosChange(webViewIdentifier, x, y, oldX, oldY, callback); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a new method to
WebView, you should add a nonScrollChangedcallback to the DartWebView. The same wayWebViewClientadds a callback: https://github.com/flutter/plugins/blob/main/packages/webview_flutter/webview_flutter_android/lib/src/android_webview.dart#L759I would also make the callback include the
WebViewfor convenience:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean
WebViewExtendedApiis not necessarily needed? I did read how theWebViewClientand attempted to do the same but couldn't find another way to set a scroll listener to aWebViewwithout defining theWebViewExtendedApi.The difference between setting a
ScrollListenerand aWebViewClientis that thewebkit.WebViewexpose the methodsetWebViewClientfor public use regardless of the Android required API level, this is unlikesetOnScrollChangeListenersince this is the method required min API level 23. The workaround here is to override the methodonScrollChangedof theWebViewand get the offset changed event from there.Do you have another approach for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea you can start by adding the
onScrollChangedparameter toWebView:Then will need to establish a Flutter API for WebView in the
pigeons/android_webview.dartfile:Then you can run the generator and implement the Flutter API that is generated. The implementation goes in
lib/src/android_webview_api_impl.dart.Then in Java you'll need to override the
onScrollChangedmethod in theWebViewPlatformViewclass found inWebViewHostApiImpl.javaclass. You can then instantiate aWebViewFlutterApiand call theWebViewFlutterApi.onScrollChangedmethod in this overridden method.I may have missed a few things in this explanation, so definitely feel free to ask me for more clarification. The plugin is still suffering from tech debt, so adding new features still requires more work than they will in the future. I can also contribute code to this PR if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback. I did as you said but even though it helped to make things simple I still end up needing to retain
WebViewExtendedApi.