forked from pallopmax/flutter_audience_network
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.dart
264 lines (241 loc) · 7.14 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import 'package:easy_audience_network/easy_audience_network.dart';
import 'package:flutter/material.dart';
void main() => runApp(AdExampleApp());
class AdExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Audience Network Example',
theme: ThemeData(
primarySwatch: Colors.blue,
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.primary,
buttonColor: Colors.blue,
),
),
home: Scaffold(
appBar: AppBar(
title: Text(
'Audience Network Example',
),
),
body: AdsPage(),
),
);
}
}
class AdsPage extends StatefulWidget {
final String idfa;
const AdsPage({Key? key, this.idfa = ''}) : super(key: key);
@override
AdsPageState createState() => AdsPageState();
}
class AdsPageState extends State<AdsPage> {
bool _isInterstitialAdLoaded = false;
bool _isRewardedAdLoaded = false;
InterstitialAd? _interstitialAd;
RewardedAd? _rewardedAd;
/// All widget ads are stored in this variable. When a button is pressed, its
/// respective ad widget is set to this variable and the view is rebuilt using
/// setState().
Widget _currentAd = SizedBox(
width: 0.0,
height: 0.0,
);
@override
void initState() {
super.initState();
// testingId is useful when you want to test if your implementation works in production
// without getting real ads, I believe it does not work properly on iOS,
// if you want to get your testingId, don't set any testingId and don't set testMode
EasyAudienceNetwork.init(
// testingId: "b602d594afd2b0b327e07a06f36ca6a7e42546d0",
testMode: true,
).then((_) {
_loadInterstitialAd();
_loadRewardedVideoAd();
});
}
void _loadInterstitialAd() {
final interstitialAd = InterstitialAd(InterstitialAd.testPlacementId);
interstitialAd.listener = InterstitialAdListener(
onLoaded: () {
_isInterstitialAdLoaded = true;
print('interstitial ad loaded');
},
onError: (code, message) {
print('interstitial ad error\ncode = $code\nmessage = $message');
},
onDismissed: () {
// load next ad already
interstitialAd.destroy();
_isInterstitialAdLoaded = false;
_loadInterstitialAd();
},
);
interstitialAd.load();
_interstitialAd = interstitialAd;
}
void _loadRewardedVideoAd() {
final rewardedAd = RewardedAd(RewardedAd.testPlacementId);
rewardedAd.listener = RewardedAdListener(
onLoaded: () {
_isRewardedAdLoaded = true;
print('rewarded ad loaded');
},
onError: (code, message) {
print('rewarded ad error\ncode = $code\nmessage = $message');
},
onVideoClosed: () {
// load next ad already
rewardedAd.destroy();
_isRewardedAdLoaded = false;
_loadRewardedVideoAd();
},
);
rewardedAd.load();
_rewardedAd = rewardedAd;
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Flexible(
child: Align(
alignment: Alignment(0, -1.0),
child: Padding(
padding: EdgeInsets.all(16),
child: _getAllButtons(),
),
),
fit: FlexFit.tight,
flex: 2,
),
// Column(children: <Widget>[
// _nativeAd(),
// // _nativeBannerAd(),
// _nativeAd(),
// ],),
Flexible(
child: Align(
alignment: Alignment(0, 1.0),
child: _currentAd,
),
fit: FlexFit.tight,
flex: 3,
)
],
);
}
Widget _getAllButtons() {
return GridView.count(
shrinkWrap: true,
crossAxisCount: 2,
childAspectRatio: 3,
children: <Widget>[
_getRaisedButton(title: "Banner Ad", onPressed: _showBannerAd),
_getRaisedButton(title: "Native Ad", onPressed: _showNativeAd),
_getRaisedButton(
title: "Native Banner Ad", onPressed: _showNativeBannerAd),
_getRaisedButton(
title: "Intestitial Ad", onPressed: _showInterstitialAd),
_getRaisedButton(title: "Rewarded Ad", onPressed: _showRewardedAd),
],
);
}
Widget _getRaisedButton({required String title, void Function()? onPressed}) {
return Padding(
padding: EdgeInsets.all(8),
child: ElevatedButton(
onPressed: onPressed,
child: Text(
title,
textAlign: TextAlign.center,
),
),
);
}
_showInterstitialAd() {
final interstitialAd = _interstitialAd;
if (interstitialAd != null && _isInterstitialAdLoaded == true)
interstitialAd.show();
else
print("Interstial Ad not yet loaded!");
}
_showRewardedAd() {
final rewardedAd = _rewardedAd;
if (rewardedAd != null && _isRewardedAdLoaded) {
rewardedAd.show();
} else {
print("Rewarded Ad not yet loaded!");
}
}
_showBannerAd() {
setState(() {
_currentAd = BannerAd(
placementId: BannerAd.testPlacementId,
bannerSize: BannerSize.STANDARD,
listener: BannerAdListener(
onError: (code, message) =>
print('banner ad error\ncode: $code\nmessage:$message'),
onLoaded: () => print('banner ad loaded'),
),
);
});
}
_showNativeBannerAd() {
setState(() {
_currentAd = _nativeBannerAd();
});
}
Widget _nativeBannerAd() {
return NativeAd(
placementId: NativeAd.testPlacementId,
adType: NativeAdType.NATIVE_BANNER_AD,
bannerAdSize: NativeBannerAdSize.HEIGHT_100,
width: double.infinity,
backgroundColor: Colors.blue,
titleColor: Colors.white,
descriptionColor: Colors.white,
buttonColor: Colors.deepPurple,
buttonTitleColor: Colors.white,
buttonBorderColor: Colors.white,
listener: NativeAdListener(
onError: (code, message) =>
print('native banner ad error\ncode: $code\nmessage:$message'),
onLoaded: () => print('native banner ad loaded'),
onMediaDownloaded: () => 'native banner ad media downloaded',
),
);
}
_showNativeAd() {
setState(() {
_currentAd = _nativeAd();
});
}
Widget _nativeAd() {
return NativeAd(
placementId: NativeAd.testPlacementId,
adType: NativeAdType.NATIVE_AD_VERTICAL,
width: double.infinity,
height: 300,
backgroundColor: Colors.blue,
titleColor: Colors.white,
descriptionColor: Colors.white,
buttonColor: Colors.deepPurple,
buttonTitleColor: Colors.white,
buttonBorderColor: Colors.white,
listener: NativeAdListener(
onError: (code, message) =>
print('native ad error\ncode: $code\nmessage:$message'),
onLoaded: () => print('native ad loaded'),
onMediaDownloaded: () => 'native ad media downloaded',
),
keepExpandedWhileLoading: true,
expandAnimationDuraion: 1000,
);
}
}