Search code examples
flutterdartflutter-layoutflutter-dependenciesflutter-web

How to prompt an action when a button is clicked every three times in flutter


I want to set up my ad to show after every three 3 clicks. but currently, I have written the code to show when the button is clicked the third time. how do I write my code so the ad pops up when it is clicked every 3 times. Also, I will like the ad to start to show the first time it is clicked and not the third time. What I mean is. the ad should show the first time the button is clicked. add should show the fourth time the button is clicked. then the seventh time. then every three times continuously So basically, whenever the ad shows, the button needs to be clicked three times before it shows again.

var clickCount = 0;

CategoryCardWithImage(
                            title: "A",
                            image: "assets/images/networks.png",
                            press: () {
                              clickCount = clickCount+1;
                              if (clickCount >2 && clickCount <4) {
                               return _showInterstitialAd();
                              }
                              Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context) {
                                  return BPage();
                                }),
                              );
                            },
                          ),

Updated version below. I tried inputting the code below but it didn't work. it just pops the ad anytime the button is clicked.

var clickCount = 0;

CategoryCardWithImage(
                            title: "D",
                            image: "assets/images/banks.png",
                            press: () {
                              bool shouldDisplayAd = clickCount == 1 || clickCount % 3 == 0;
                              if (shouldDisplayAd) return _showInterstitialAd();
                              Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context) {
                                  return BanksPage();
                                }),
                              );
                            },
                          ),


Solution

  • For making the ad show every third click, you could check if the counter is divisible by 3:

    press: () {
        clickCount++;
        bool shouldDisplayAd = clickCount == 1 || clickCount % 3 == 0;
        if (shouldDisplayAd) return _showInterstitialAd(); }