Skip to content

Commit 666b35f

Browse files
committed
added a CallBack support for scrolling in Y direction
1 parent 7e6cdaa commit 666b35f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.content.Context;
99
import android.os.Build;
1010
import android.os.Handler;
11+
import android.util.Log;
1112
import android.view.View;
1213
import android.webkit.WebStorage;
1314
import android.webkit.WebView;
@@ -34,6 +35,21 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
3435
platformThreadHandler = new Handler(context.getMainLooper());
3536
// Allow local storage.
3637
webView.getSettings().setDomStorageEnabled(true);
38+
// this is a test added to get call back to Y scroll position API 23;
39+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
40+
41+
42+
webView.setOnScrollChangeListener(new View.OnScrollChangeListener(){
43+
@Override
44+
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
45+
methodChannel.invokeMethod("callback",scrollY);
46+
//Log.d("Hi android","---------------ScrollY = -----------------------------"+scrollY);
47+
48+
}
49+
});
50+
}
51+
52+
3753

3854
methodChannel = new MethodChannel(messenger, "plugins.flutter.io/webview_" + id);
3955
methodChannel.setMethodCallHandler(this);

packages/webview_flutter/example/lib/main.dart

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class WebViewExample extends StatelessWidget {
4343
// to allow calling Scaffold.of(context) so we can show a snackbar.
4444
body: Builder(builder: (BuildContext context) {
4545
return WebView(
46+
onScrollYCallback: (y){
47+
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@----------Y = $y ---------------------");
48+
},
4649
initialUrl: 'https://flutter.dev',
4750
javascriptMode: JavascriptMode.unrestricted,
4851
onWebViewCreated: (WebViewController webViewController) {

packages/webview_flutter/lib/webview_flutter.dart

+9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ typedef NavigationDecision NavigationDelegate(NavigationRequest navigation);
6969
/// Signature for when a [WebView] has finished loading a page.
7070
typedef void PageFinishedCallback(String url);
7171

72+
/// Signature for when a [webView] Scrolled in Y direction.
73+
typedef Future<dynamic> ScrollYCallback (MethodCall call);
74+
7275
final RegExp _validChannelNames = RegExp('^[a-zA-Z_][a-zA-Z0-9]*\$');
7376

7477
/// A named channel for receiving messaged from JavaScript code running inside a web view.
@@ -117,9 +120,13 @@ class WebView extends StatefulWidget {
117120
this.navigationDelegate,
118121
this.gestureRecognizers,
119122
this.onPageFinished,
123+
this.onScrollYCallback
124+
120125
}) : assert(javascriptMode != null),
121126
super(key: key);
122127

128+
/// Web view scrolling in Y direction callBack.
129+
final ScrollYCallback onScrollYCallback;
123130
/// If not null invoked once the web view is created.
124131
final WebViewCreatedCallback onWebViewCreated;
125132

@@ -270,6 +277,7 @@ class _WebViewState extends State<WebView> {
270277
_controller.complete(controller);
271278
if (widget.onWebViewCreated != null) {
272279
widget.onWebViewCreated(controller);
280+
273281
}
274282
}
275283

@@ -367,6 +375,7 @@ class WebViewController {
367375
_settings = _WebSettings.fromWidget(_widget);
368376
_updateJavascriptChannelsFromSet(_widget.javascriptChannels);
369377
_channel.setMethodCallHandler(_onMethodCall);
378+
_channel.setMethodCallHandler(_widget.onScrollYCallback);
370379
}
371380

372381
final MethodChannel _channel;

0 commit comments

Comments
 (0)