我成功地在页脚上显示智能横幅然后处理它.我的问题是在应用的所有页面上显示.
我的问题是:如何只在一个页面上显示?
@override
void initState() {
super.initState();
myBanner
..load().then((loaded) {
if (loaded && this.mounted) {
myBanner..show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
akn*_*nay 15
我从这里的Github 链接找到了工作解决方案。这个想法很简单。
showBannerAd内调用initState()。showBannerAd推送页面后调用如下代码片段。从推送页面返回后,这将再次显示广告。Navigator.push(context, MaterialPageRoute(builder: (context) => EditPage())).then((value) {
Ads.showBannerAd();
});
Run Code Online (Sandbox Code Playgroud)
hideBannderAd内部initState()。然后你就完成了。我的广告助手类
import 'package:firebase_admob/firebase_admob.dart';
const String testDevice = 'YOUR_DEVICE_ID';
class Ads {
static BannerAd _bannerAd;
static void initialize() {
FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
}
static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
testDevices: testDevice != null ? <String>[testDevice] : null,
keywords: <String>['foo', 'bar'],
contentUrl: 'http://foo.com/bar.html',
childDirected: true,
nonPersonalizedAds: true,
);
static BannerAd _createBannerAd() {
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
size: AdSize.banner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd event $event");
},
);
}
static void showBannerAd() {
if (_bannerAd == null) _bannerAd = _createBannerAd();
_bannerAd
..load()
..show(anchorOffset: 0.0, anchorType: AnchorType.bottom);
}
static void hideBannerAd() async {
await _bannerAd.dispose();
_bannerAd = null;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有多个充当单独屏幕的小部件,则只需包含该部分
@override void initState() {
super.initState();
myBanner ..load().then((loaded) {
if (loaded && this.mounted) {
myBanner..show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
您可能需要处置它:
@override
void dispose() {
myBanner?.dispose();
super.dispose();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |