Im successfully showing the smart banner on footer and then dispose it. My problem is that is showing on all pages of the app.
My question is: How to show only on one page?
@override
void initState() {
super.initState();
myBanner
..load().then((loaded) {
if (loaded && this.mounted) {
myBanner..show();
}
});
}
Ads won't show if you haven't integrated the Google Mobile Ads SDK correctly. Is your ad implementation code working properly? Test your implementation code to check that ads can show. You can also use ad inspector to test your app's ad serving.
I found working solution from Github link here. The idea is quite simple.
showBannerAd
inside initState()
at the page that you want to show Ads.showBannerAd
like the following snippet after you pushed a page. This will show Ads again after you return back from the pushed page.Navigator.push(context, MaterialPageRoute(builder: (context) => EditPage())).then((value) {
Ads.showBannerAd();
});
hideBannderAd
inside initState()
at the pushed page that you don't want to show Ads. Then you are done. My Ads helper class
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With