Flutter 如何隐藏横幅广告时,导航到下一页的颤振?
我正在使用插件在颤振应用程序中设置广告。我试过横幅广告,效果很好,但当我导航到另一个页面时,它仍然保持在原来的位置。我希望当导航到另一个页面时,广告应该隐藏 代码片段如下所示Flutter 如何隐藏横幅广告时,导航到下一页的颤振?,flutter,admob,ads,Flutter,Admob,Ads,我正在使用插件在颤振应用程序中设置广告。我试过横幅广告,效果很好,但当我导航到另一个页面时,它仍然保持在原来的位置。我希望当导航到另一个页面时,广告应该隐藏 代码片段如下所示 BannerAd myBanner = BannerAd( // Replace the testAdUnitId with an ad unit id from the AdMob dash. // https://developers.google.com/admob/android/test-ads //
BannerAd myBanner = BannerAd(
// Replace the testAdUnitId with an ad unit id from the AdMob dash.
// https://developers.google.com/admob/android/test-ads
// https://developers.google.com/admob/ios/test-ads
adUnitId: BannerAd.testAdUnitId,
size: AdSize.smartBanner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd event is $event");
},
);
myBanner
// typically this happens well before the ad is shown
..load()
..show(
// Positions the banner ad 60 pixels from the bottom of the screen
anchorOffset: 60.0,
// Banner Position
anchorType: AnchorType.bottom,
);
销毁页面时将调用dispose()
。这样你就可以在那里没收横幅广告了
@override
void dispose() {
myBanner.dispose();
super.dispose();
}
您可以使用
RouteObserver
:
class AdmobObserver extends RouteObserver<PageRoute<dynamic>> {
BannerAd _myBanner = BannerAd(
adUnitId: BannerAd.testAdUnitId,
size: AdSize.smartBanner,
listener: (MobileAdEvent event) {
print("BannerAd event is $event");
},
);
@override
void didPush(Route route, Route previousRoute) {
super.didPush(route, previousRoute);
if (route.settings.name == '/') {
// show the banner when navigating to home screen
_showBannerAd();
} else {
// hide the banner when navigating to another screen
_myBanner.dispose();
}
}
@override
void didPop(Route route, Route previousRoute) {
super.didPop(route, previousRoute);
if (previousRoute.settings.name == '/') {
// show the banner again when returning back to the home screen
_showBannerAd();
}
}
void _showBannerAd() {
_myBanner
..load()
..show(
anchorOffset: 60.0,
// Banner Position
anchorType: AnchorType.bottom,
);
}
}
如果您想在显示新屏幕时隐藏,并在用户返回上一个屏幕时重新显示,则需要在开始新页面之前处理,并使用异步和等待等待导航器弹出新页面,让我们显示一些代码
BannerAd bannerAd;
@override
void initState() {
super.initState();
initAds();
}
void openNewPage() async{
//hide banner before start new page
bannerAd?.dispose();
await Navigator.push(context, MaterialPageRoute(builder: (_) => MySecondScreen()));
//now user is return to this page so reshow banner
initAds();
}
@override
void dispose() {
super.dispose();
bannerAd?.dispose();
interstitialAd?.dispose();
}
void initAds() async {
bannerAd = BannerAd(
adUnitId: kReleaseMode ? Constant.BANNER_AD_ID : BannerAd.testAdUnitId,
size: AdSize.smartBanner,
listener: (MobileAdEvent event) {
print("BannerAd event is $event");
},
);
bannerAd
// typically this happens well before the ad is shown
..load()
..show(
anchorType: AnchorType.bottom,
);
}
所以在这个方法中,我们隐藏了/reshow横幅
void openNewPage() async{
//hide banner before start new page
bannerAd?.dispose();
await Navigator.push(context, MaterialPageRoute(builder: (_) => MySecondScreen()));
//now user is return to this page so reshow banner
initAds();
}
调用Navigator.pop()代码>
调用Navigator.push()之前。这将解决您的问题请按照以下步骤操作。
考虑两页/屏幕作为主页和第二页。< /P>
第一步。在主页中添加此代码以转到第二页
myBanner?.dispose();
myBanner = null;
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondPage()));
Timer(Duration(seconds: 3), () {
if(secondPageBanner == null){
initState();
}
});
这将删除BannerAd对象,并且横幅将消失
第二步。在第二页中添加此代码
myBanner?.dispose();
myBanner = null;
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondPage()));
Timer(Duration(seconds: 3), () {
if(secondPageBanner == null){
initState();
}
});
将BannerAd初始化/声明代码放入initState()函数中。
这将在第二页显示横幅
第三步。返回主页时在第二页中添加此代码
secondPageBanner?.dispose();
if(secondPageBanner != null){
secondPageBanner = null;
}
setState(() {
Navigator.pop(context);
});
这将使secondPageBanner隐藏
第四步。在主页中添加此代码
Timer(Duration(seconds: 3), () {
if(myBanner == null){
initState();
}
});
还是像这样
void function(){
if(myBanner == null){
initState();
}
}
在按钮点击功能中,该按钮通常由用户点击
这将在主页中再次显示myBanner广告。用于检测要显示bannerAd是否位于顶部的路线,现在隐藏横幅的唯一方法是调用myBanner.dispose()。感谢您的回答。只工作一次(一次导航),之后每次我都会出现这个错误,它会锁定我的导航器,你知道吗<代码>未处理的异常:“包:flatter/src/widgets/navigator.dart”:失败的断言:第2416行位置18:“!导航器。_debugLocked':不正确。
具有确切的_debugLocked问题@Naografix,你找到解决方案了吗?这给了我新的错误,比如“package:flatter/src/widgets/navigator.dart”:失败的断言:第4506行位置12:“_debugLocked':不正确。请不要忘记取消计时器。