Created
February 21, 2022 01:41
-
-
Save jaysonss/caa9aeffe7a01d4f656ca9d67d38422a to your computer and use it in GitHub Desktop.
This file contains 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
class WebViewScrollDemoState extends State<WebViewDemo> { | |
double _bodyHeight = 500; | |
WebViewController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox( | |
height: _bodyHeight, | |
child: Scrollable( | |
viewportBuilder: (_, offset) { | |
return WebViewport( | |
offset: offset, | |
clipBehavior: Clip.hardEdge, | |
onScroll: _onScroll, | |
child: WebView( | |
key: _webviewKey, | |
initialUrl: "https://juejin.cn", | |
onWebViewCreated: (controller) { | |
_controller = controller; | |
}, | |
javascriptMode: JavascriptMode.unrestricted, | |
debuggingEnabled: true, | |
), | |
contentHeight: _contentHeight, | |
); | |
}, | |
), | |
); | |
} | |
void _onScroll(Offset offset) { | |
_controller?.evaluateJavascript( | |
"window.scrollTo(0,${_webviewOffsetY.abs().ceil()})"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment