loadAndShowNative static method

void loadAndShowNative(
  1. BuildContext context,
  2. NativeHolder nativeHolder,
  3. NativeAdmobType nativeType
)

Implementation

static void loadAndShowNative(BuildContext context, NativeHolder nativeHolder,
    NativeAdmobType nativeType) async {
  if (!_isShowAds || await isNetworkConnected() == false) {
    isLoad = false;
    adsNativeController.value = Pair(null, "no internet");
    return;
  }

  final String adUnitId = _isDebug
      ? _idTestNativeAd
      : Platform.isAndroid
          ? nativeHolder.idAndroid
          : nativeHolder.idIOS;

  if (!context.mounted) {
    return;
  }
  adsNativeController.value.nativeAd?.dispose();
  isLoad = true;
  adsNativeController.value = Pair(null, "");

  NativeAd(
          adUnitId: adUnitId,
          listener: NativeAdListener(
            onAdLoaded: (ad) {
              adsNativeController.value = Pair(ad as NativeAd, "success");
            },
            onAdFailedToLoad: (ad, error) {
              isLoad = false;
              adsNativeController.value = Pair(ad as NativeAd, "error");
              ad.dispose();
            },
            // Called when a click is recorded for a NativeAd.
            onAdClicked: (ad) {},
            // Called when an impression occurs on the ad.
            onAdImpression: (ad) {},
            // Called when an ad removes an overlay that covers the screen.
            onAdClosed: (ad) {},
            // Called when an ad opens an overlay that covers the screen.
            onAdOpened: (ad) {},
            // For iOS only. Called before dismissing a full screen view
            onAdWillDismissScreen: (ad) {},
            // Called when an ad receives revenue value.
            onPaidEvent: (ad, valueMicros, precision, currencyCode) {},
          ),
          request: const AdRequest(),
          // Styling
          nativeTemplateStyle: NativeTemplateStyle(
              // Required: Choose a template.
              templateType: (nativeType == NativeAdmobType.NATIVE_MEDIUM)
                  ? TemplateType.medium
                  : TemplateType.small,
              // Optional: Customize the ad's style.
              mainBackgroundColor: Colors.transparent,
              cornerRadius: 10.0,
              callToActionTextStyle: NativeTemplateTextStyle(
                  textColor: Colors.cyan,
                  backgroundColor: Colors.red,
                  style: NativeTemplateFontStyle.monospace,
                  size: 16.0),
              primaryTextStyle: NativeTemplateTextStyle(
                  textColor: Colors.red,
                  backgroundColor: Colors.transparent,
                  style: NativeTemplateFontStyle.italic,
                  size: 16.0),
              secondaryTextStyle: NativeTemplateTextStyle(
                  textColor: Colors.green,
                  backgroundColor: Colors.black,
                  style: NativeTemplateFontStyle.bold,
                  size: 16.0),
              tertiaryTextStyle: NativeTemplateTextStyle(
                  textColor: Colors.brown,
                  backgroundColor: Colors.transparent,
                  style: NativeTemplateFontStyle.normal,
                  size: 16.0)))
      .load();
}