"top"
额外的参数,但似乎没有生效。
我的代码:
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();
}
}
是“可折叠”:“底部”启用可折叠横幅的正确方法? 是的,它得到了支持
"collapsible": "bottom"