[英]How to get banner size of smart banner?
我正在嘗試通過firebase_admob將 admob 集成到我的 flutter 應用程序中。
默認情況下,它似乎只是在當前視圖的頂部疊加/堆疊橫幅......而不考慮實際的小部件樹及其約束。
我真的不知道為什么有人會設計一個這樣的圖書館......但是好吧。 也許是出於反欺詐的原因?!
為了避免用橫幅覆蓋實際內容,我想在我的內容中添加一個填充(填充高度 = 橫幅高度)。
由於我使用的是“智能橫幅”,庫在運行時動態地嘗試為給定的屏幕尺寸找到最佳橫幅尺寸。
如何找出它想出的橫幅尺寸?
根據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;
}
如您所見,我始終使用設備最長的一側,而不是計算與實際設備高度相關的橫幅高度(換句話說,根據設備方向)。
這樣做是因為當用戶旋轉設備時,官方 flutter_admob 插件不會重新加載適當的橫幅。 考慮這種情況: - 縱向設備,橫幅加載 50 - 用戶旋轉設備:根據文檔,高度應為 32,但橫幅不會重新加載,將其保留在高度 50 - 發生新的橫幅加載,根據到您的廣告系列設置,提供高度為 32 的橫幅 - 用戶再次旋轉設備等
正如我在評論中所說,一個有效的解決方案是追蹤最后的橫幅尺寸,並對橫幅加載事件做出反應,以便為用戶提供無縫體驗。
您可以使用我的方法getMargin ,它會計算您需要顯示橫幅而不與內容重疊的填充值,無論手機的高度如何,它都會自動設置正確的填充值。
SmartBanner 有三個高度: 32|50|90我的方法計算手機的高度並設置正確的值 + 5。
double getMargin(double height){
double margin;
if(height <= 400){
margin = 37;
} else if(height >= 400 && height < 720){
margin = 55;
} else if(height >= 720){
margin = 95;
}
return margin;
}
例子:
double screenHeight = MediaQuery.of(context).size.height;
Container(
height: EdgeInsets.fromLTRB(bottom: getMargin(height))
)
另外,您可以像我一樣將它添加到一個單獨的類中,並使方法靜態以從那里調用。
class Ads {
static double getMargin(double height){
double margin;
if(height <= 400){
margin = 37;
} else if(height >= 400 && height < 720){
margin = 55;
} else if(height >= 720){
margin = 95;
}
return margin;
}
例子:
double screenHeight = MediaQuery.of(context).size.height;
Container(
height: EdgeInsets.fromLTRB(bottom: Ads.getMargin(height))
)
從@mattia-galati 的回答中發布的規格來看,也有固定的高度選項可用。 你為什么不使用其中之一?
AdSize.banner 、 AdSize.largeBanner等都有固定的高度。
僅使用AdSize.smartBanner 的任何特定原因?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.