Skip to content
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

how to know the webview scrollled to end? #89

Closed
MiYogurt opened this issue May 2, 2019 · 3 comments
Closed

how to know the webview scrollled to end? #89

MiYogurt opened this issue May 2, 2019 · 3 comments

Comments

@MiYogurt
Copy link

MiYogurt commented May 2, 2019

use javascript event look like not work? i don't know reason, so confuse。

onWebViewCreated: (ctl){
   ctl.injectScriptCode("""
     document.body.onscroll = function(e){console.log(document.body.onscroll));
    """);
}

maybe need some event from webview。 i search some solution from google, i am not test the code, i haven't actually developed an Android project. Lack of experience

    float contentHeight = getContentHeight() * scale;
    float scrollHeight = getHeight() + getScrollY();
    if (Math.abs(contentHeight - scrollHeight) < 1) {
      getChannel().invokeMethod("onScrollBottom", null);
    } else if (getScrollY() == 0) {
      getChannel().invokeMethod("onScrollTop", null);
    }
@MiYogurt MiYogurt changed the title how to know the webview scrolll to end? how to know the webview scrollled to end? May 2, 2019
@MiYogurt
Copy link
Author

MiYogurt commented May 2, 2019

for solve webview in listView not scrollable problem, use delay is bad idea。so bad。When the drag is too large,will skip webview scrollable。

class WebListBox2 extends StatefulWidget {
  @override
  _WebListBoxState2 createState() => _WebListBoxState2();
}

class _WebListBoxState2 extends State<WebListBox2> {
  ScrollPhysics physics;
  bool isUp = false;
  ScrollController srollController;
  double screenHeight;
  GlobalKey webContainer = GlobalKey();
  bool webViewPositionChanged = true; // 浏览器的位置发生了变动
  int prevY = 0; // 上一次的Y

  @override
  void initState() {
    super.initState();
    physics = AlwaysScrollableScrollPhysics();
    srollController = ScrollController();
  }

  double get prevContainerHeight { // 第一个容器的高度
    var renderBox = webContainer.currentContext.findRenderObject();
    return renderBox.semanticBounds.height;
  }

  bool get isDown => !(isUp);

  @override
  void dispose() {
    super.dispose();
    srollController.dispose();
  }

  // 修正滚动的位置
  void toWebViewTop(){
    srollController.animateTo(prevContainerHeight, duration: Duration(milliseconds: 100), curve: Curves.linear);
  }

  @override
  Widget build(BuildContext context) {
    screenHeight = MediaQuery.of(context).size.height;
    return Listener(
      onPointerMove: (e){
        isUp = e.delta.dy < 0;
      },
      onPointerUp: (e){
        if(
        physics is AlwaysScrollableScrollPhysics &&
            srollController.position.pixels >= prevContainerHeight  &&  // 滚动超过的第一个容器
            srollController.position.pixels - prevContainerHeight <= screenHeight // 在第二个容器 webview 之内
        ){
          toWebViewTop();
          setState(() {
            physics = NeverScrollableScrollPhysics();
          });
        } else {
          if(isUp){
            Future.delayed(Duration(milliseconds: 100)).then((_){
              if(
              physics is NeverScrollableScrollPhysics && // 处于不可滚动状态
                  !webViewPositionChanged // 没有 Y 发生改变
              ){
                setState(() {
                  physics = AlwaysScrollableScrollPhysics();
                });
              }
              webViewPositionChanged = false;
            });
          }
        }

      },
      child: ListView(
        controller: srollController,
        physics: physics,
        children: <Widget>[
          SizedBox(
              key: webContainer,
              height: 996
          ),
          Container(
              height: screenHeight,
              child: InAppWebView(
                onScrollChanged: (controller, int x, int y){
                  if(y <= 10){
                    setState(() {
                      physics = AlwaysScrollableScrollPhysics();
                    });
                    return;
                  }
                  if(y != prevY) webViewPositionChanged = true;
                  prevY = y;
                },
                initialUrl: "https://flutter.io/",
              )
          ),
          SizedBox(height: screenHeight),
        ],
      ),
    );
  }
}

@MiYogurt
Copy link
Author

MiYogurt commented May 3, 2019

i test the code ,not work, have another way to know scrolled to end?

@MiYogurt MiYogurt closed this as completed May 5, 2019
This was referenced Jul 6, 2020
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant