-
Notifications
You must be signed in to change notification settings - Fork 4
/
banner_ads.dart
65 lines (57 loc) · 1.82 KB
/
banner_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
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'ads.dart';
class AdBanner extends StatefulWidget {
const AdBanner({Key? key,
this.width,
this.height}) : super(key: key);
final int? width;
final int? height;
@override
State<AdBanner> createState() => _AdBannerState();
}
class _AdBannerState extends State<AdBanner> {
late BannerAd bannerAd;
bool isReady=false;
@override
initState(){
super.initState();
createBannerAd();
}
createBannerAd(){
bannerAd= BannerAd(
size: (widget.width!=null&&widget.height!=null)?
AdSize(width:widget.width??0 ,height: widget.height??0):AdSize.mediumRectangle,
request: const AdRequest(),
adUnitId: AdsId.bannerAdUnitId,
listener: BannerAdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) => setState(() {
isReady=true;
}),
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
// Dispose the ad here to free resources.
ad.dispose();
debugPrint('Ad failed to load: $error');
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) => debugPrint('Ad opened.'),
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) => debugPrint('Ad closed.'),
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) => debugPrint('Ad impression.'),
),
);
bannerAd.load();
}
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
child: AdWidget(ad: bannerAd),
width: bannerAd.size.width.toDouble(),
height: bannerAd.size.height.toDouble(),
);
}
}