getNextAd method

Widget? getNextAd(
  1. List priorityList, {
  2. CustomNativeAdType type = CustomNativeAdType.medium,
})

Implementation

Widget? getNextAd(
  List priorityList, {
  CustomNativeAdType type = CustomNativeAdType.medium,
}) {
  bool useNativeFactory = adModel?.iscustomeads ?? false;
  final factoryId =
      type == CustomNativeAdType.small
          ? 'customNativeAdSmall'
          : 'customNativeAdMedium';
  final targetTemplate =
      type == CustomNativeAdType.small
          ? TemplateType.small
          : TemplateType.medium;

  for (var network in priorityList) {
    switch (network) {
      case 'admob':
        final index = _admobAds.indexWhere(
          (ad) =>
              ad.factoryId == factoryId ||
              ad.nativeTemplateStyle?.templateType == targetTemplate,
        );
        if (index != -1) {
          final ad = _admobAds.removeAt(index);
          _preloadAdMob(type, useNativeFactory: useNativeFactory);
          return Center(
            child: Container(
              constraints:
                  useNativeFactory
                      ? constraintsForType(type, ad: ad)
                      : defaultConstraintsForType(
                        ad.nativeTemplateStyle?.templateType,
                      ),
              child: AdWidget(ad: ad, key: ObjectKey(ad)),
            ),
          );
        }
        break;
      case 'admanager':
        final index = _adManagerAds.indexWhere(
          (ad) =>
              ad.factoryId == factoryId ||
              ad.nativeTemplateStyle?.templateType == targetTemplate,
        );
        if (index != -1) {
          final ad = _adManagerAds.removeAt(index);
          _preloadAdManager(type, useNativeFactory: useNativeFactory);
          return Center(
            child: Container(
              constraints:
                  useNativeFactory
                      ? constraintsForType(type, ad: ad)
                      : defaultConstraintsForType(
                        ad.nativeTemplateStyle?.templateType,
                      ),
              child: AdWidget(ad: ad, key: ObjectKey(ad)),
            ),
          );
        }
        break;
      case 'qureka':
        final index = _qurekaAds.indexWhere(
          (ad) => ad is CustomNativeAdView && ad.adType == type,
        );
        if (index != -1) {
          final qk = _qurekaAds.removeAt(index);
          _preloadQureka(type);
          return qk;
        } else if (adModel != null) {
          return CustomNativeAdView(adModel: adModel!, adType: type);
        }
        break;
    }
  }
  return null; // nothing available
}