-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrewarded_ads.dart
66 lines (59 loc) · 1.92 KB
/
rewarded_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
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'ads.dart';
class AdReWarded{
AdReWarded._();
RewardedAd? _rewardedAd;
int _numRewardedLoadAttempts = 0;
int maxFailedLoadAttempts = 3;
static AdReWarded instance=AdReWarded._();
void createRewardedAd() {
RewardedAd.load(
adUnitId:AdsId.rewardedAdUnitId,
request: const AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (RewardedAd ad) {
print('$ad loaded.');
_rewardedAd = ad;
_numRewardedLoadAttempts = 0;
_showRewardedAd();
},
onAdFailedToLoad: (LoadAdError error) {
print('RewardedAd failed to load: $error');
_rewardedAd = null;
_numRewardedLoadAttempts += 1;
if (_numRewardedLoadAttempts < maxFailedLoadAttempts) {
createRewardedAd();
}
},
));
}
void _showRewardedAd() {
if (_rewardedAd == null) {
print('Warning: attempt to show rewarded before loaded.');
return;
}
_rewardedAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (RewardedAd ad) =>
print('ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (RewardedAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
createRewardedAd();
},
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
createRewardedAd();
},
);
_rewardedAd!.setImmersiveMode(true);
_rewardedAd!.show(
onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
print('$ad with reward $RewardItem(${reward.amount}, ${reward.type})');
});
_rewardedAd = null;
}
void dispose() {
_rewardedAd?.dispose();
}
}