Search code examples
flutteradmobgoogle-mobile-ads

How show interstitial ads by number of clicks (google_mobile_ads)


I want to show an interstitial ads in 10 clicks. Is there a way to do this? Or how I can show only 1 time at the opening of the app? I'm just trying to find how to use it.

 ElevatedButton(
          child: Text("Ads"),
          onPressed: () {
            _interstitialAd.show();
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => PageTwo()));
          },
        ),

Solution

  • You need to keep a counter in your state somewhere and and call the method to show the ad only when it's a multiple of 10.

    //A field of your state class
    int counter = 0;
    
    //...somewhere in build
    ElevatedButton(
      child: Text("Ads"),
      onPressed: () {
        if(counter%10 == 0) {
          _interstitialAd.show();
        }
    
        counter++;
        Navigator.push(context,
          MaterialPageRoute(builder: (context) => PageTwo()));
      },
    ),