Browse Source

added position listener in both x and y axis

theblackcat102 7 years ago
parent
commit
59c0dbac68

+ 7 - 13
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -54,19 +54,13 @@ class WebviewManager {
 
         observableWebView = (ObservableWebView) webView;
         observableWebView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback(){
-            public void onScroll(int l, int t, int oldl, int oldt){
-                if(t > oldt){
-                    // Call
-                    Map<String, Object> data = new HashMap<>();
-                    data.put("direction", "DOWN");
-                    FlutterWebviewPlugin.channel.invokeMethod("onScrollChanged", data);
-                    //Do stuff
-                }
-                else if(t < oldt){
-                    Map<String, Object> data = new HashMap<>();
-                    data.put("direction", "UP");
-                    FlutterWebviewPlugin.channel.invokeMethod("onScrollChanged", data);
-                }
+            public void onScroll(int x, int y, int oldx, int oldy){
+                Map<String, Object> yDirection = new HashMap<>();
+                yDirection.put("direction", (double)y);
+                FlutterWebviewPlugin.channel.invokeMethod("onScrollYChanged", yDirection);
+                Map<String, Object> xDirection = new HashMap<>();
+                xDirection.put("direction", (double)x);
+                FlutterWebviewPlugin.channel.invokeMethod("onScrollXChanged", xDirection);
             }
         });
 

+ 22 - 1
example/lib/main.dart

@@ -58,6 +58,8 @@ class _MyHomePageState extends State<MyHomePage> {
   // On urlChanged stream
   StreamSubscription<WebViewStateChanged> _onStateChanged;
 
+  StreamSubscription<double> _onScrollYChanged,_onScrollXChanged;
+
   TextEditingController _urlCtrl = new TextEditingController(text: selectedUrl);
 
   TextEditingController _codeCtrl =
@@ -95,6 +97,23 @@ class _MyHomePageState extends State<MyHomePage> {
       }
     });
 
+
+    _onScrollYChanged = flutterWebviewPlugin.onScrollYChanged.listen((double y) {
+      if (mounted) {
+        setState(() {
+          _history.add("Scroll in  Y Direction: $y");
+        });
+      }
+    });
+
+    _onScrollXChanged = flutterWebviewPlugin.onScrollXChanged.listen((double x) {
+      if (mounted) {
+        setState(() {
+          _history.add("Scroll in  X Direction: $x");
+        });
+      }
+    });
+
     _onStateChanged =
         flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
       if (mounted) {
@@ -111,6 +130,8 @@ class _MyHomePageState extends State<MyHomePage> {
     _onDestroy.cancel();
     _onUrlChanged.cancel();
     _onStateChanged.cancel();
+    _onScrollYChanged.cancel();
+    _onScrollXChanged.cancel();
 
     flutterWebviewPlugin.dispose();
 
@@ -193,7 +214,7 @@ class _MyHomePageState extends State<MyHomePage> {
             },
             child: new Text("Cookies"),
           ),
-          new Text(_history.join("\n"))
+          new Text(_history.sublist(_history.length-10, _history.length ).join("\n")),
         ],
       ),
     );

+ 5 - 8
ios/Classes/FlutterWebviewPlugin.m

@@ -111,14 +111,11 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 }
 
 - (void) scrollViewDidScroll:(UIScrollView *)scrollView {
-    CGPoint velocity = [[scrollView panGestureRecognizer]velocityInView:scrollView.superview];
-    if (velocity.y > 1) {
-        id data = @{@"direction": @"UP"};
-        [channel invokeMethod:@"onScrollChanged" arguments:data];
-    } else if (velocity.y < -1 ) {
-        id data = @{@"direction": @"DOWN"};
-        [channel invokeMethod:@"onScrollChanged" arguments:data];
-    }
+    id xDirection = @{@"xDirection": @(scrollView.contentOffset.x) };
+    [channel invokeMethod:@"onScrollXChanged" arguments:xDirection];
+
+    id yDirection = @{@"yDirection": @(scrollView.contentOffset.y) };
+    [channel invokeMethod:@"onScrollYChanged" arguments:yDirection];
 }
 
 - (void)navigate:(FlutterMethodCall*)call {

+ 15 - 5
lib/src/base.dart

@@ -20,7 +20,8 @@ class FlutterWebviewPlugin {
   final _onUrlChanged = new StreamController<String>.broadcast();
   final _onStateChanged = new StreamController<WebViewStateChanged>.broadcast();
   final _onError = new StreamController<String>.broadcast();
-  final _onScrollChanged = new StreamController<String>.broadcast();
+  final _onScrollXChanged = new StreamController<double>.broadcast();
+  final _onScrollYChanged = new StreamController<double>.broadcast();
 
   static FlutterWebviewPlugin _instance;
 
@@ -38,8 +39,11 @@ class FlutterWebviewPlugin {
       case "onUrlChanged":
         _onUrlChanged.add(call.arguments["url"]);
         break;
-      case "onScrollChanged":
-        _onScrollChanged.add(call.arguments["direction"]);
+      case "onScrollXChanged":
+        _onScrollXChanged.add(call.arguments["xDirection"]);
+        break;
+      case "onScrollYChanged":
+        _onScrollYChanged.add(call.arguments["yDirection"]);
         break;
       case "onState":
         _onStateChanged.add(
@@ -65,7 +69,12 @@ class FlutterWebviewPlugin {
   Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;
 
 
-  Stream<String> get onScrollChanged => _onScrollChanged.stream;
+  /// Listening web view y position scroll change
+  Stream<double> get onScrollYChanged => _onScrollYChanged.stream;
+
+  /// Listening web view x position scroll change
+  Stream<double> get onScrollXChanged => _onScrollXChanged.stream;
+
   /// Start the Webview with [url]
   /// - [withJavascript] enable Javascript or not for the Webview
   ///     iOS WebView: Not implemented yet
@@ -157,7 +166,8 @@ class FlutterWebviewPlugin {
     _onDestroy.close();
     _onUrlChanged.close();
     _onStateChanged.close();
-    _onScrollChanged.close();
+    _onScrollXChanged.close();
+    _onScrollYChanged.close();
     _onError.close();
     _instance = null;
   }