-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadhelper.dart
165 lines (140 loc) · 4.25 KB
/
adhelper.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
class AdHelper{
//Test IDs
//Banner ca-app-pub-3940256099942544/6300978111
//Interstitial ca-app-pub-3940256099942544/1033173712
//Rewarded ca-app-pub-3940256099942544/5224354917
//Replace with your IDs
static String? get bannerAdUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/6300978111";
} else if (Platform.isIOS) {
return "ca-app-pub-3940256099942544/6300978111";
} else {
}
}
static String? get interstitialAdUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/1033173712";
} else if (Platform.isIOS) {
return "ca-app-pub-3940256099942544/1033173712";
} else {
}
}
static String? get rewardedAdUnitId {
if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/5224354917";
} else if (Platform.isIOS) {
return "ca-app-pub-3940256099942544/5224354917";
} else {
}
}
static InterstitialAd? interstitialAd;
static void createInterstitialAd() {
InterstitialAd.load(
adUnitId: AdHelper.interstitialAdUnitId!,
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) {
interstitialAd = ad;
showInterstitialAd();
},
onAdFailedToLoad: (LoadAdError error) => interstitialAd = null,
),
);
}
static void showInterstitialAd() {
if (interstitialAd != null) {
interstitialAd!.fullScreenContentCallback =
FullScreenContentCallback(onAdDismissedFullScreenContent: (ad) {
ad.dispose();
}, onAdFailedToShowFullScreenContent: (ad, error) {
ad.dispose();
});
interstitialAd!.show();
}
}
static RewardedAd? rewardedAd;
static final AdRequest request = AdRequest(nonPersonalizedAds: true,);
static void createRewardedAd() {
RewardedAd.load(
adUnitId: AdHelper.rewardedAdUnitId!,
request: request,
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (RewardedAd ad) {
print('$ad loaded.');
rewardedAd = ad;
showRewardedAd();
},
onAdFailedToLoad: (LoadAdError error) {
print('RewardedAd failed to load: $error');
rewardedAd = null;
},
));
}
static 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();
},
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
}
);
rewardedAd!.setImmersiveMode(true);
rewardedAd!.show(
onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
print("Rewarded");
});
rewardedAd = null;
}
}
class AdmobBanner extends StatefulWidget {
const AdmobBanner({super.key});
@override
State<AdmobBanner> createState() => _AdmobBannerState();
}
class _AdmobBannerState extends State<AdmobBanner> {
late BannerAd bannerAd;
bool isAdLoaded = false;
@override
void initState() {
super.initState();
bannerAd = BannerAd(
size: AdSize.banner,
adUnitId: AdHelper.bannerAdUnitId!,
listener: BannerAdListener(
onAdLoaded: (ad) {
setState(() {
isAdLoaded = true;
});
},
onAdFailedToLoad: (ad,error){
ad.dispose();
}
),
request: const AdRequest()
);
bannerAd.load();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: double.parse(bannerAd.size.height.toString()),
width: double.parse(bannerAd.size.width.toString()),
child: isAdLoaded
? AdWidget(ad: bannerAd)
: Stack()
);
}
}