-
Notifications
You must be signed in to change notification settings - Fork 4
/
interstitial_ads.dart
74 lines (69 loc) · 2.31 KB
/
interstitial_ads.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'ads.dart';
class AdInterstitial{
AdInterstitial._();
static AdInterstitial instance=AdInterstitial._();
InterstitialAd? _interstitialAd;
int maxFailedLoadAttempts = 3;
int _numInterstitialLoadAttempts = 0;
// void loadIntersitialAd(){
// InterstitialAd.load(
// adUnitId: AdsId.interstitialAdUnitId,
// request: const AdRequest(),
// adLoadCallback: InterstitialAdLoadCallback(
// onAdLoaded: (InterstitialAd ad) {
// _interstitialAd = ad;
// _showInterstitialAd();
// },
// onAdFailedToLoad: (LoadAdError error) {
// print('InterstitialAd failed to load: $error');
// },
// ));
// }
void createInterstitialAd() {
InterstitialAd.load(
adUnitId: AdsId.interstitialAdUnitId,
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
_interstitialAd = ad;
_numInterstitialLoadAttempts = 0;
_interstitialAd!.setImmersiveMode(true);
_showInterstitialAd();
},
onAdFailedToLoad: (LoadAdError error) {
print('InterstitialAd failed to load: $error.');
_numInterstitialLoadAttempts += 1;
_interstitialAd = null;
if (_numInterstitialLoadAttempts < maxFailedLoadAttempts) {
createInterstitialAd();
}
},
));
}
void _showInterstitialAd() {
if (_interstitialAd == null) {
print('Warning: attempt to show interstitial before loaded.');
return;
}
_interstitialAd?.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (InterstitialAd ad) =>
print('ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (InterstitialAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
createInterstitialAd();
},
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
createInterstitialAd();
},
);
_interstitialAd?.show();
_interstitialAd = null;
}
void dispose() {
_interstitialAd?.dispose();
}
}