Search code examples
flutterdartadmob

flutter how to show first an interstitial ad through button and then go to new screen?


i want to load an interstitial ad with a button in flutter. I want to show first the interstitial and then, when ad finish, go automaticaly to another screen, or load another thing.

I dont know how to make something like "interstitial.wait" to show first the ad, and when it will close run another thing.

this is my code:

onPressed: () {
   showInterstitialAd(); //this first
   launch(urlCourse.text); //when ad closed, run this
}

Solution

  • Answering my own question. We can make use of onAdDismissedFullScreenContent

    this is an example:

     void showInterstitialAd() {
        if (interstitialAd == null) {
          print('trying to show before loading');
          return;
        }
    
        interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
            onAdShowedFullScreenContent: (ad) => print('ad showed $ad'),
    
            //when ad went closes
            onAdDismissedFullScreenContent: (ad) {
              //run something
              launch(urlCourse.text); 
              ad.dispose();
              createInterstitialAd();
            },
            onAdFailedToShowFullScreenContent: (ad, error) {
              ad.dispose();
              print('failed to show the ad $ad');
    
              createInterstitialAd();
            });
    
        interstitialAd!.show();
        interstitialAd = null;
      }