/// A simple app that loads a banner ad.
class BannerChico extends StatefulWidget {
const BannerChico({super.key});
@override
BannerChicoState createState() => BannerChicoState();
}
class BannerChicoState extends State<BannerChico> {
late final PuntosApp puntosApp;
late final GdprBool gdprBool;
int puntos = 0;
//bool gprdBool = false;
BannerAd? _bannerAd;
final String _adUnitId = Platform.isAndroid
? 'ca-app-pub-xxxxxx'
: 'ca-app-pub-xxxxxx';
@override
void initState() {
super.initState();
puntosApp = PuntosApp();
// gdprBool = GdprBool();
puntos = puntosApp.puntos;
// gprdBool = gdprBool.gprdBool;
if (puntos < 10) {
_loadAd();
}
}
@override
Widget build(BuildContext context) {
final esEuropeo = GdprBool().getGdprBool();
return Visibility(
visible: puntos < 10 && Suscrito().getunAnio() == false,
child: Stack(
children: [
if ((_bannerAd != null ? false : true) && esEuropeo)
const Align(
alignment: Alignment.bottomCenter,
child: MiBannerGdpr(),
),
if (_bannerAd != null)
Align(
alignment: Alignment.bottomCenter,
child: FittedBox(
fit: BoxFit.cover,
child: Container(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height * 0.3,
maxWidth: MediaQuery.of(context).size.width * 0.9,
),
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
child: AdWidget(ad: _bannerAd!),
),
),
),
],
));
}
void _loadAd() async {
await BannerAd(
adUnitId: _adUnitId,
request: const AdRequest(),
size: AdSize.banner,
listener: BannerAdListener(
// Called when an ad is successfully received.
onAdLoaded: (ad) {
if (mounted) {
setState(() {
// Actualiza el estado del widget
_bannerAd = ad as BannerAd;
});
}
},
// Called when an ad request failed.
onAdFailedToLoad: (ad, err) {
ad.dispose();
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) {},
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) {},
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) {},
),
).load();
}
@override
void dispose() {
_bannerAd?.dispose();
super.dispose();
}
}
Comentarios
Publicar un comentario