如何获得智能横幅的横幅尺寸?

for*_*emo 11 android admob dart firebase flutter

我正在尝试通过firebase_admob将admob集成到我的flutter应用程序中.

默认情况下,它似乎只是将横幅叠加/堆叠在当前视图的顶部...而不考虑实际的窗口小部件树及其约束.

我真的不知道为什么有人会设计这样的库......但是好的.也许是出于反欺诈的原因?!

为了避免用横幅覆盖实际内容,我想在我的内容中添加填充(填充高度= banner-height).

由于我使用的是"智能横幅",因此库会动态尝试在运行时为给定的屏幕尺寸找到最佳横幅尺寸.

如何找出它想出的横幅尺寸?

Mat*_*ati 8

根据Google Specs,这是我用来获取智能横幅高度的代码。

  /// The initial size of the banner is calculated on the height of the
  /// viewport. Due to ADMob banner refresh policies, in order to have
  /// a consistent behaviour, we should keep track of the current AD size
  /// and maintain it when the user rotates the screen, and update that
  /// value at every banner successful.
  /// For now, we will avoid this complexity and set the banner height to
  /// the maximum height that a banner could get on this device, forcing
  /// the use of the longest side as the base.
  /// see https://developers.google.com/admob/android/banner#smart_banners
  double _getSmartBannerHeight(BuildContext context) {
    MediaQueryData mediaScreen = MediaQuery.of(context);
    double dpHeight = mediaScreen.orientation == Orientation.portrait
        ? mediaScreen.size.height
        : mediaScreen.size.width;
    log.fine("Device height: $dpHeight");
    if (dpHeight <= 400.0) {
      return 32.0;
    }
    if (dpHeight > 720.0) {
      return 90.0;
    }
    return 50.0;
  }
Run Code Online (Sandbox Code Playgroud)

如您所见,我始终使用设备最长的一侧,而不是计算与实际设备高度相关的横幅高度(换句话说,根据设备方向)。

这样做是因为当用户旋转设备时,官方 flutter_admob 插件不会重新加载适当的横幅。考虑这种情况: - 纵向设备,横幅加载 50 - 用户旋转设备:根据文档,高度应为 32,但横幅不会重新加载,将其保留在高度 50 - 发生新的横幅加载,根据到您的广告系列设置,提供高度为 32 的横幅 - 用户再次旋转设备等

正如我在评论中所说,一个有效的解决方案是追踪最后的横幅尺寸,并对横幅加载事件做出反应,以便为用户提供无缝体验。