【问题标题】:Visibility Detector flutter能见度探测器抖动
【发布时间】:2022-12-18 00:26:10
【问题描述】:

我在 flutter 中使用可见性检测器来检查小部件的 100% 可见性。但即使小部件位于固定应用栏后面,可见性检测器也会认为它 100% 可见。仅当其位于导航栏下方时才将其视为可见的解决方案是什么?

【问题讨论】:

  • 你能提供更多的上下文吗?
  • 听起来像 XY 问题

标签: flutter widget visibilitychange


【解决方案1】:

请将代码与您的问题一起发布以便更好地理解

【讨论】:

    【解决方案2】:

    听起来您正在使用 Flutter 中的 VisibilityDetector 小部件来检查另一个小部件的可见性。默认情况下,VisibilityDetector 会将小部件视为在屏幕上 100% 可见,无论它是否被其他元素(例如应用栏)遮挡。

    要解决这个问题,您需要使用不同的方法来检查小部件的可见性。一种可能的解决方案是使用 MediaQuery 类来确定屏幕的高度和应用栏的位置,然后使用该信息来计算小部件的可见性。您还可以使用 Stack 和 Positioned 小部件相对于应用栏定位小部件,然后使用 VisibilityDetector 检查其可见性。

    下面是一个示例,说明如何使用 MediaQuery 类来计算小部件的可见性:

    // Determine the height of the screen
    final screenHeight = MediaQuery.of(context).size.height;
    
    // Determine the height of the app bar
    final appBarHeight = kToolbarHeight;
    
    // Calculate the height of the visible area of the screen
    final visibleAreaHeight = screenHeight - appBarHeight;
    
    // Use the VisibilityDetector to check the visibility of the widget
    VisibilityDetector(
      key: Key('my_widget'),
      onVisibilityChanged: (VisibilityInfo info) {
        // Calculate the percentage of the widget that is visible
        final visibility = info.visibleFraction * 100;
    
        // If the widget is more than 50% visible, consider it visible
        if (visibility > 50) {
          // Do something when the widget is visible
        }
      },
      child: Container(
        height: visibleAreaHeight,
        // Other widget properties
      ),
    )
    

    我希望这有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 2019-02-12
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多