loadAndShowRewardedInterstitial static method

void loadAndShowRewardedInterstitial(
  1. RewardHolder rewardHolder, {
  2. required dynamic onEarned(),
  3. required dynamic onAdClosed(),
  4. required dynamic onAdFail(),
  5. required bool enableLoadingDialog,
})

Implementation

static void loadAndShowRewardedInterstitial(RewardHolder rewardHolder,
    {required Function() onEarned,
    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
      ? _idTestRewardedInterAd
      : Platform.isAndroid
          ? rewardHolder.idAndroid
          : rewardHolder.idIOS;

  RewardedInterstitialAd.load(
      adUnitId: adUnitId,
      request: const AdRequest(),
      rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
        // Called when an ad is successfully received.
        onAdLoaded: (ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
              // Called when the ad showed the full screen content.
              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();
                EasyLoading.dismiss();
                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.
          _rewardInterstitialdAd = ad;
          _rewardInterstitialdAd?.show(onUserEarnedReward:
              (AdWithoutView view, RewardItem rewardItem) {
            onEarned();
          });
        },
        // Called when an ad request failed.
        onAdFailedToLoad: (LoadAdError error) {
          onAdFail();
          EasyLoading.dismiss();
        },
      ));
}