showBannerAds function

dynamic showBannerAds({
  1. int pos = 0,
  2. int retry = 0,
  3. AdSize size = AdSize.banner,
  4. EdgeInsetsGeometry? margin,
  5. Decoration? decoration,
  6. dynamic onAdLoadedCallback(
    1. Widget
    )?,
  7. Function? onAdFailedToLoadCallback,
})

Implementation

showBannerAds({int pos = 0,
  int retry = 0,
  AdSize size = AdSize.banner,
  EdgeInsetsGeometry? margin,
  Decoration? decoration,
  Function(Widget)? onAdLoadedCallback,
  Function? onAdFailedToLoadCallback}) async {
  if (_settings == null ||
      (_settings?.adSetting?.appVersionCode == appVersionCode) ||
      (_settings?.bannerAdsSequence?.isEmpty ?? true) ||
      (_settings?.isAllAds != true)) {
    if (onAdFailedToLoadCallback != null) {
      onAdFailedToLoadCallback();
    }
    return;
  }

  if ((_settings?.bannerAdsSequence?.length ?? 0) <= pos) {
    if ((_settings?.adSetting?.adsRetry ?? 0) > 0 &&
        (_settings?.adSetting?.adsRetry ?? 0) > retry) {
      pos = 0;
      retry++;
    } else {
      resetTimerDelayed();
      if (onAdFailedToLoadCallback != null) {
        onAdFailedToLoadCallback();
      }
      return;
    }
  }

  incrementBannerSequence().then((value) {
    currentBannerSequence(pos).then((curSeqBanner) {
      pos = curSeqBanner % (_settings?.bannerAdsSequence?.length ?? 0);
      var adsJson = getCurSeqData(_settings?.bannerAdsSequence, pos);
      var seqName = adsJson.containsKey("seq_name") ? adsJson["seq_name"] : "";

      dynamic adsModel = adsJson.containsKey("ads") ? adsJson["ads"] : null;

      if (adsModel != null) {
        switch (seqName) {
          case GOOGLEAD:
          case ADMANAGERAD:
          case APPLOVINAD:
          case FACEBOOKAD:
            if (adsModel is AdsModel) {
              AdsModel ads = adsModel;
              if ((ads.isBannerAd ?? false) &&
                  (ads.bannerAdId ?? "").isNotEmpty) {
                if (seqName == GOOGLEAD) {
                  BannerAd(
                      size: size,
                      adUnitId: ads.bannerAdId ?? "",
                      listener: BannerAdListener(
                        onAdLoaded: (Ad ad) {
                          if (onAdLoadedCallback != null &&
                              ad is AdWithView) {
                            onAdLoadedCallback(Container(
                              width: size.width.toDouble(),
                              height: size.height.toDouble(),
                              margin: margin,
                              decoration: decoration,
                              child: AdWidget(ad: ad),
                            ));
                          }
                        },
                        onAdFailedToLoad: (Ad ad, LoadAdError error) {
                          ad.dispose();
                          showBannerAds(
                              retry: retry,
                              pos: pos + 1,
                              size: size,
                              onAdLoadedCallback: onAdLoadedCallback,
                              onAdFailedToLoadCallback:
                              onAdFailedToLoadCallback);
                        },
                        onAdOpened: (Ad ad) {},
                        onAdClosed: (Ad ad) {},
                      ),
                      request: _request)
                      .load();
                }

                if (seqName == ADMANAGERAD) {
                  AdManagerBannerAd(
                    adUnitId: ads.bannerAdId ?? "",
                    sizes: <AdSize>[size],
                    request: _ad_manager_request,
                    listener: AdManagerBannerAdListener(
                      onAdLoaded: (Ad ad) async {
                        if (onAdLoadedCallback != null && ad is AdWithView) {
                          onAdLoadedCallback(Container(
                            width: size.width.toDouble(),
                            height: size.height.toDouble(),
                            margin: margin,
                            decoration: decoration,
                            child: AdWidget(ad: ad),
                          ));
                        }
                      },
                      onAdFailedToLoad: (Ad ad, LoadAdError error) {
                        ad.dispose();
                        // _adManagerBannerAd = null;
                        showBannerAds(
                            retry: retry,
                            pos: pos + 1,
                            size: size,
                            onAdLoadedCallback: onAdLoadedCallback,
                            onAdFailedToLoadCallback: onAdFailedToLoadCallback);
                      },
                    ),
                  ).load();
                }

                if (seqName == APPLOVINAD) {
                  AppLovinBannerAd(
                    adUnitId: size.height < 250
                        ? (ads.bannerAdId ?? "")
                        : ((ads.mrecBannerAdId ?? "").isNotEmpty
                        ? (ads.mrecBannerAdId ?? "")
                        : (ads.bannerAdId ?? "")),
                    size: size,
                    listener: BannerAdListener(
                      onAdLoaded: (Ad ad) async {
                        if (onAdLoadedCallback != null && ad is AdWithView) {
                          onAdLoadedCallback(Container(
                            width: size.width.toDouble(),
                            height: size.height.toDouble(),
                            margin: margin,
                            decoration: decoration,
                            child: AdWidget(ad: ad),
                          ));
                        }
                      },
                      onAdFailedToLoad: (Ad ad, LoadAdError error) {
                        ad.dispose();
                        showBannerAds(
                            retry: retry,
                            pos: pos + 1,
                            size: size,
                            onAdLoadedCallback: onAdLoadedCallback,
                            onAdFailedToLoadCallback: onAdFailedToLoadCallback);
                      },
                    ),
                  ).load();
                }

                if (seqName == FACEBOOKAD) {
                  FacebookBannerAd(
                    adUnitId: ads.bannerAdId ?? "",
                    size: size,
                    listener: BannerAdListener(
                      onAdLoaded: (Ad ad) async {
                        if (onAdLoadedCallback != null && ad is AdWithView) {
                          onAdLoadedCallback(Container(
                            width: size.width.toDouble(),
                            height: size.height.toDouble(),
                            margin: margin,
                            decoration: decoration,
                            child: AdWidget(ad: ad),
                          ));
                        }
                      },
                      onAdFailedToLoad: (Ad ad, LoadAdError error) {
                        ad.dispose();
                        showBannerAds(
                            retry: retry,
                            pos: pos + 1,
                            size: size,
                            onAdLoadedCallback: onAdLoadedCallback,
                            onAdFailedToLoadCallback: onAdFailedToLoadCallback);
                      },
                    ),
                  ).load();
                }

                return;
              }
            }
            showBannerAds(
                retry: retry,
                pos: pos + 1,
                size: size,
                onAdLoadedCallback: onAdLoadedCallback,
                onAdFailedToLoadCallback: onAdFailedToLoadCallback);
            break;
          case CUSTOMAD:
            CustomAds ads = CustomAds.fromJson(adsJson["json"]);
            if ((ads.isQurakaBanner ?? false) &&
                (ads.qurakaBannerImage?.isNotEmpty ?? false)) {
              final adUnitId = (ads.qurakaBannerImage?..shuffle())?.first ?? "";

              CustomBannerAd(
                adUnitId: adUnitId,
                size: size,
                listener: BannerAdListener(onAdLoaded: (Ad ad) async {
                  if (onAdLoadedCallback != null && ad is AdWithView) {
                    onAdLoadedCallback(Container(
                      width: size.width.toDouble(),
                      height: size.height.toDouble(),
                      margin: margin,
                      decoration: decoration,
                      child: AdWidget(ad: ad),
                    ));
                  }
                }, onAdFailedToLoad: (Ad ad, LoadAdError error) {
                  ad.dispose();
                  showBannerAds(
                      retry: retry,
                      pos: pos + 1,
                      size: size,
                      onAdLoadedCallback: onAdLoadedCallback,
                      onAdFailedToLoadCallback: onAdFailedToLoadCallback);
                }, onAdClicked: (Ad ad) {
                  launchURL(ads.qurekaLink ?? "");
                }),
              ).load();
            } else {
              showBannerAds(
                  retry: retry,
                  pos: pos + 1,
                  size: size,
                  onAdLoadedCallback: onAdLoadedCallback,
                  onAdFailedToLoadCallback: onAdFailedToLoadCallback);
            }
            break;
          default:
            showBannerAds(
                retry: retry,
                pos: pos + 1,
                size: size,
                onAdLoadedCallback: onAdLoadedCallback,
                onAdFailedToLoadCallback: onAdFailedToLoadCallback);
            break;
        }
      }
    });
  });
}