简体   繁体   English

我想检测 WebView_Flutter 中的滚动并隐藏填充

[英]I want to detect scrolling in WebView_Flutter and hide the padding

I am using Webview_Flutter.我正在使用 Webview_Flutter。 The header of the site overlaps the position of the statusbar and I would like to add padding to avoid this.该网站的 header 与状态栏的 position 重叠,我想添加填充以避免这种情况。

This is the process of inserting padding to avoid the statusbar if the webview is opened or if there is a scroll position at the top.如果打开 webview 或者顶部有滚动条 position,这是插入填充以避免状态栏的过程。

    body: Padding(
          padding: (controller?.getScrollY() == null || controller?.getScrollY() == 0)
              ? EdgeInsets.only(top: height)
              : EdgeInsets.only(top: 0),
          child: Expanded(
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 0.0),
                      child: WebView(
                        javascriptMode: JavascriptMode.unrestricted,
                        initialUrl: Uri.parse(widget.link).toString(),
                        onWebResourceError: (error) {
                          // print(error.domain);
                        },
                        onWebViewCreated: (controller) {
                          this.controller = controller;
                        },
                        onProgress: (progress) {
                          setState(() {
                            this.progress = progress / 100;
                            progressPercent = progress;
                          });
                        },
                      ),
              

To detect WebView scroll event, you can use the flutter_inappwebview plugin (I'm the author) and implement the InAppWebView.onScrollChanged event.要检测 WebView 滚动事件,可以使用flutter_inappwebview插件(我是作者)并实现InAppWebView.onScrollChanged事件。

However, probably you don't need to add top padding for your WebView. You can set the AppBar.toolbarHeight to 0 , so the app bar will have the right height to cover the status bar.但是,您可能不需要为 WebView 添加顶部填充。您可以将AppBar.toolbarHeight设置为0 ,这样应用栏的高度就会覆盖状态栏。

Here is a full code example with both cases using the current latest version 6 available of the plugin ( 6.0.0-beta.16 ):这是一个完整的代码示例,两种情况都使用插件的当前最新版本 6 ( 6.0.0-beta.16 ):

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb &&
      kDebugMode &&
      defaultTargetPlatform == TargetPlatform.android) {
    await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
  }
  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;

  int scrollY = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 0,
        ),
        body: Padding(
          padding: EdgeInsets.only(top: scrollY <= 0 ? 25 : 0),
          child: Column(
            children: [
              Expanded(
                child: InAppWebView(
                  key: webViewKey,
                  initialUrlRequest:
                      URLRequest(url: WebUri("https://github.com/flutter")),
                  onWebViewCreated: (controller) {
                    webViewController = controller;
                  },
                  onScrollChanged: (controller, x, y) {
                    setState(() {
                      scrollY = y;
                    });
                  },
                ),
              )
            ],
          ),
        ));
  }
}

模拟器屏幕录制 - iPhone 14 Pro Max - 2022-11-25 at 01 21 15

I tried to find the listener of the webView scroll, I couldn't find it, you're right.我试图找到 webView 卷轴的监听器,我找不到它,你是对的。 There is a solution ^^, it's simple, we could wrap WebView in ListView then we could use scrollListener(1) or notificationListener(2) and don't forget to use setState to update Padding values有一个解决方案^^,很简单,我们可以在 ListView 中包装 WebView 然后我们可以使用 scrollListener(1) 或 notificationListener(2) 并且不要忘记使用 setState 来更新 Padding 值

class _HomeScreenState extends State<HomeScreen> {
    WebViewController? _webViewController;
    ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return
      Scaffold(
        body:Scaffold(
          backgroundColor: Colors.green,
          appBar: AppBar(
            title: const Text('Flutter WebView example'),
            // This drop down menu demonstrates that Flutter widgets can be shown over the web view.
            actions: <Widget>[

            ],
          ),
//NotificationListener(2)
          body: NotificationListener<ScrollNotification>(
            onNotification: (scrollNotification) {
              if (scrollNotification is ScrollStartNotification) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              print("ScrollStartNotification / pixel => ${scrollNotification.metrics.pixels}");
            });

          } else if (scrollNotification is ScrollEndNotification) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              setState(() {
                print("ScrollEndNotification / pixel =>${scrollNotification.metrics.pixels}");
              });
            });
          }

              return true;
            },
            child: ListView(
              physics: ClampingScrollPhysics(),
              controller: _scrollController,
              children: <Widget>[
                ConstrainedBox(
                    constraints: BoxConstraints(maxHeight: 10000),
                    child: WebView(
                      initialUrl: 'https://flutter.dev',
                      javascriptMode: JavascriptMode.unrestricted,
                      onWebViewCreated: (WebViewController webViewController) {},
                      onProgress: (int progress) {
                        print('WebView is loading (progress : $progress%)');
                      },
                      javascriptChannels: <JavascriptChannel>{
                      },
                      onPageStarted: (String url) {},
                      onPageFinished: (String url) {},
                      gestureNavigationEnabled: true,
                      backgroundColor: const Color(0x00000000),
                    ),
                ),
              ],
            ),
          )));


  }

    @override
  void initState() {
     super.initState();
//scrollListener(1)
     _scrollController.addListener(() {
       print("scrollListener / pixel =>${_scrollController.position.pixels}");
     });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM