admin管理员组文章数量:1290959
I'm trying to implement a collapsible banner ad in my Flutter app using Google AdMob. However, the ad does not collapse as expected, and it behaves like a regular banner ad. I have followed the documentation and set the "collapsible": "bottom"
or "top"
extra parameter in my ad request, but it doesn't seem to take effect.
My Code:
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:insta/ads/consent_manager.dart';
class BannerAdWidget extends StatefulWidget {
final String adUnitId;
const BannerAdWidget({
super.key,
required this.adUnitId,
});
@override
_BannerAdWidgetState createState() => _BannerAdWidgetState();
}
class _BannerAdWidgetState extends State<BannerAdWidget> {
BannerAd? _bannerAd;
bool _isAdLoaded = false;
final ConsentManager _consentManager = ConsentManager.instance;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_gatherConsentAndLoadAd();
}
void _gatherConsentAndLoadAd() {
_consentManager.gatherConsent((FormError? error) async {
if (error == null) {
bool canRequestAds = await _consentManager.canRequestAds();
if (canRequestAds) {
_loadAd();
} else {
debugPrint('User consent is not sufficient to request ads.');
}
} else {
debugPrint('Error gathering consent: ${error.message}');
}
});
}
Future<void> _loadAd() async {
await _bannerAd?.dispose();
setState(() {
_bannerAd = null;
_isAdLoaded = false;
});
final AnchoredAdaptiveBannerAdSize? adSize = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
MediaQuery.of(context).size.width.truncate(),
);
if (adSize == null) {
debugPrint('Unable to get height of anchored banner.');
return;
}
const adRequest = AdRequest(extras: {
"collapsible": "bottom",
});
_bannerAd = BannerAd(
adUnitId: widget.adUnitId,
size: adSize,
request: adRequest,
listener: BannerAdListener(
onAdLoaded: (Ad ad) {
debugPrint('$ad loaded: ${ad.responseInfo}');
setState(() {
_bannerAd = ad as BannerAd;
_isAdLoaded = true;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
debugPrint('Banner ad failed to load: $error');
ad.dispose();
},
),
);
_bannerAd!.load();
}
Widget _buildAdWidget() {
return OrientationBuilder(
builder: (context, orientation) {
if (_bannerAd != null && _isAdLoaded) {
return Container(
alignment: Alignment.bottomCenter,
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
child: AdWidget(ad: _bannerAd!),
);
}
return const SizedBox.shrink();
},
);
}
@override
Widget build(BuildContext context) {
return _buildAdWidget();
}
@override
void dispose() {
_bannerAd?.dispose();
super.dispose();
}
}
What I Expected:
- The banner ad should appear at the bottom and collapse when the user interacts with the UI.
What Actually Happened:
- The banner ad loads successfully, but it does not collapse as expected.
Questions:
- Does Google AdMob currently support collapsible banners in Flutter?
- Is
"collapsible": "bottom"
the correct way to enable a collapsible banner? - Are there any known workarounds or alternative approaches to achieve this behavior?
Any insights or suggestions would be greatly appreciated! Thanks in advance.
本文标签: Google AdMob Collapsible Banner Not Working in FlutterStack Overflow
版权声明:本文标题:Google AdMob Collapsible Banner Not Working in Flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741520652a2383163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论