-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
main.dart
403 lines (378 loc) · 14.5 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import 'dart:io';
import 'package:admob_flutter/admob_flutter.dart';
// import 'package:admob_flutter_example/extensions.dart';
import 'package:admob_flutter_example/new_page.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Initialize without device test ids
Admob.initialize();
// Add a list of test ids.
// Admob.initialize(testDeviceIds: ['YOUR DEVICE ID']);
runApp(MyMaterialApp());
}
class MyMaterialApp extends StatefulWidget {
@override
_MyMaterialAppState createState() => _MyMaterialAppState();
}
class _MyMaterialAppState extends State<MyMaterialApp> {
GlobalKey<ScaffoldState> scaffoldState = GlobalKey();
AdmobBannerSize? bannerSize;
late AdmobInterstitial interstitialAd;
late AdmobReward rewardAd;
@override
void initState() {
super.initState();
// You should execute `Admob.requestTrackingAuthorization()` here before showing any ad.
bannerSize = AdmobBannerSize.BANNER;
interstitialAd = AdmobInterstitial(
adUnitId: getInterstitialAdUnitId()!,
listener: (AdmobAdEvent event, Map<String, dynamic>? args) {
if (event == AdmobAdEvent.closed) interstitialAd.load();
handleEvent(event, args, 'Interstitial');
},
);
rewardAd = AdmobReward(
adUnitId: getRewardBasedVideoAdUnitId()!,
listener: (AdmobAdEvent event, Map<String, dynamic>? args) {
if (event == AdmobAdEvent.closed) rewardAd.load();
handleEvent(event, args, 'Reward');
},
);
interstitialAd.load();
rewardAd.load();
}
void handleEvent(
AdmobAdEvent event, Map<String, dynamic>? args, String adType) {
switch (event) {
case AdmobAdEvent.loaded:
showSnackBar('New Admob $adType Ad loaded!');
break;
case AdmobAdEvent.opened:
showSnackBar('Admob $adType Ad opened!');
break;
case AdmobAdEvent.closed:
showSnackBar('Admob $adType Ad closed!');
break;
case AdmobAdEvent.failedToLoad:
showSnackBar('Admob $adType failed to load. :(');
break;
case AdmobAdEvent.rewarded:
showDialog(
context: scaffoldState.currentContext!,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
return true;
},
child: AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Reward callback fired. Thanks Andrew!'),
Text('Type: ${args!['type']}'),
Text('Amount: ${args['amount']}'),
],
),
),
);
},
);
break;
default:
}
}
void showSnackBar(String content) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(content),
duration: Duration(milliseconds: 1500),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blueGrey,
),
home: Builder(
builder: (BuildContext context) => Scaffold(
key: scaffoldState,
appBar: AppBar(
title: const Text('AdmobFlutter'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
fullscreenDialog: true,
builder: (BuildContext context) {
return NewPage(title: 'Full Screen Dialog');
},
),
);
},
child: Text(
'FullscreenDialog',
style: TextStyle(
color: Colors.white,
),
),
)
],
), // .withBottomAdmobBanner(context),
bottomNavigationBar: Builder(
builder: (BuildContext context) {
return Container(
color: Colors.blueGrey,
child: SafeArea(
child: SizedBox(
height: 60,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () async {
final isLoaded = await interstitialAd.isLoaded;
if (isLoaded ?? false) {
interstitialAd.show();
} else {
showSnackBar(
'Interstitial ad is still loading...');
}
},
child: Text(
'Show Interstitial',
style: TextStyle(color: Colors.white),
),
),
),
Expanded(
child: TextButton(
onPressed: () async {
if (await rewardAd.isLoaded) {
rewardAd.show();
} else {
showSnackBar('Reward ad is still loading...');
}
},
child: Text(
'Show Reward',
style: TextStyle(color: Colors.white),
),
),
),
Expanded(
child: PopupMenuButton(
initialValue: bannerSize,
offset: Offset(0, 20),
onSelected: (AdmobBannerSize newSize) {
setState(() {
bannerSize = newSize;
});
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<AdmobBannerSize>>[
PopupMenuItem(
value: AdmobBannerSize.BANNER,
child: Text('BANNER'),
),
PopupMenuItem(
value: AdmobBannerSize.LARGE_BANNER,
child: Text('LARGE_BANNER'),
),
PopupMenuItem(
value: AdmobBannerSize.MEDIUM_RECTANGLE,
child: Text('MEDIUM_RECTANGLE'),
),
PopupMenuItem(
value: AdmobBannerSize.FULL_BANNER,
child: Text('FULL_BANNER'),
),
PopupMenuItem(
value: AdmobBannerSize.LEADERBOARD,
child: Text('LEADERBOARD'),
),
PopupMenuItem(
value: AdmobBannerSize.SMART_BANNER(context),
child: Text('SMART_BANNER'),
),
PopupMenuItem(
value: AdmobBannerSize.ADAPTIVE_BANNER(
width: MediaQuery.of(context)
.size
.width
.toInt() -
40, // considering EdgeInsets.all(20.0)
),
child: Text('ADAPTIVE_BANNER'),
),
],
child: Center(
child: Text(
'Banner size',
style: TextStyle(
fontWeight: FontWeight.w500,
color: Colors.white),
),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return NewPage(
title: 'Push Page',
);
}),
);
},
child: Text(
'Push Page',
style: TextStyle(color: Colors.white),
),
),
),
],
),
),
),
);
},
),
body: Column(
children: [
Expanded(
child: Scrollbar(
child: ListView.builder(
padding: EdgeInsets.all(20.0),
itemCount: 1000,
itemBuilder: (BuildContext context, int index) {
if (index != 0 && index % 6 == 0) {
return Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 20.0),
child: AdmobBanner(
adUnitId: getBannerAdUnitId()!,
adSize: bannerSize!,
listener: (AdmobAdEvent event,
Map<String, dynamic>? args) {
handleEvent(event, args, 'Banner');
},
onBannerCreated:
(AdmobBannerController controller) {
// Dispose is called automatically for you when Flutter removes the banner from the widget tree.
// Normally you don't need to worry about disposing this yourself, it's handled.
// If you need direct access to dispose, this is your guy!
// controller.dispose();
},
),
),
Container(
height: 100.0,
margin: EdgeInsets.only(bottom: 20.0),
color: Colors.cyan,
),
],
);
}
return Container(
height: 100.0,
margin: EdgeInsets.only(bottom: 20.0),
color: Colors.cyan,
);
},
),
),
),
// Another option is to fix a banner ad to the top or bottom of your content.
// Notice that banners are not scrolling, which is a violation of admob policy.
//
// See: https://github.com/kmcgill88/admob_flutter/issues/194
// "banner ads should not move as a user scrolls, as users may try to
// click on the menu but end up clicking on the ad accidentally instead.
// This specific implementation is against policy and we reserve the right
// to disable ad serving to your app."
// Builder(
// builder: (BuildContext context) {
// final size = MediaQuery.of(context).size;
// final height = max(size.height * .05, 50.0);
// return Container(
// width: size.width,
// height: height,
// child: AdmobBanner(
// adUnitId: getBannerAdUnitId(),
// adSize: AdmobBannerSize.ADAPTIVE_BANNER(
// width: size.width.toInt(),
// ),
// listener: (AdmobAdEvent event, Map<String, dynamic> args) {
// handleEvent(event, args, 'Banner');
// },
// ),
// );
// },
// ),
],
),
),
),
);
// .withBottomAdmobBanner(context);
}
@override
void dispose() {
interstitialAd.dispose();
rewardAd.dispose();
super.dispose();
}
}
/*
Test Id's from:
https://developers.google.com/admob/ios/banner
https://developers.google.com/admob/android/banner
App Id - See README where these Id's go
Android: ca-app-pub-3940256099942544~3347511713
iOS: ca-app-pub-3940256099942544~1458002511
Banner
Android: ca-app-pub-3940256099942544/6300978111
iOS: ca-app-pub-3940256099942544/2934735716
Interstitial
Android: ca-app-pub-3940256099942544/1033173712
iOS: ca-app-pub-3940256099942544/4411468910
Reward Video
Android: ca-app-pub-3940256099942544/5224354917
iOS: ca-app-pub-3940256099942544/1712485313
*/
String? getBannerAdUnitId() {
if (Platform.isIOS) {
return 'ca-app-pub-3940256099942544/2934735716';
} else if (Platform.isAndroid) {
return 'ca-app-pub-3940256099942544/6300978111';
}
return null;
}
String? getInterstitialAdUnitId() {
if (Platform.isIOS) {
return 'ca-app-pub-3940256099942544/4411468910';
} else if (Platform.isAndroid) {
return 'ca-app-pub-3940256099942544/1033173712';
}
return null;
}
String? getRewardBasedVideoAdUnitId() {
if (Platform.isIOS) {
return 'ca-app-pub-3940256099942544/1712485313';
} else if (Platform.isAndroid) {
return 'ca-app-pub-3940256099942544/5224354917';
}
return null;
}