loadAndShowInter static method

void loadAndShowInter(
  1. InterHolder interHolder, {
  2. required dynamic onAdClosed(),
  3. required dynamic onAdFail(),
  4. required bool enableLoadingDialog,
})

Implementation

static void loadAndShowInter(InterHolder interHolder,
    {required Function() onAdClosed,
    required Function() onAdFail,
    required bool enableLoadingDialog}) async {
  if (!_isShowAds || await isNetworkConnected() == false) {
    onAdFail();
    return;
  }

  if (enableLoadingDialog) {
    EasyLoading.show(status: 'Loading ads...');
  }

  final String adUnitId = _isDebug
      ? _idTestInterstitialAd
      : Platform.isAndroid
          ? interHolder.idAndroid
          : interHolder.idIOS;

  InterstitialAd.load(
      adUnitId: adUnitId,
      request: const AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        // Called when an ad is successfully received.
        onAdLoaded: (InterstitialAd ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
              // Called when the ad showed the full screen content.
              onAdShowedFullScreenContent: (ad) {
                log("message onAdShowedFullScreenContent $ad");
                Future.delayed(const Duration(milliseconds: 800), () {
                  EasyLoading.dismiss();
                });
              },
              // Called when an impression occurs on the ad.
              onAdImpression: (ad) {},
              // Called when the ad failed to show full screen content.
              onAdFailedToShowFullScreenContent: (ad, err) {
                onAdFail();
                EasyLoading.dismiss();
                ad.dispose();
              },
              // Called when the ad dismissed full screen content.
              onAdDismissedFullScreenContent: (ad) {
                onAdClosed();
                ad.dispose();
              },
              // Called when a click is recorded for an ad.
              onAdClicked: (ad) {});

          // Keep a reference to the ad so you can show it later.
          _interstitialAd = ad;
          _interstitialAd?.show();
          _interstitialAd = null;
        },
        // Called when an ad request failed.
        onAdFailedToLoad: (LoadAdError error) {
          onAdFail();
          log('InterstitialAd failed to load: $error');
          _interstitialAd = null;
        },
      ));
}