skip to Main Content

How to set a limit on button clicks 2 times a day?

I have a button

            IconButton(
              icon: Icon(Icons.arrow_forward_ios), onPressed: () async {

              _CreatePost();
              _showLoaderDialog(context);
            },
            ),

and I want the button to be inactive for 24 hours when there are 2 clicks. How can I do this I would appreciate any help.

I tried to find information on many sites but there is very little information, I tried a lot of things but it does not help.

2

Answers


  1. If i had this problem i would solve saving the datetime of last two clicks on shared preferences or any database that you prefer, and on build method i would check if the older click has more than one day. if not i would disable the button and make a future.delayed function to able this buttom on right time. After the two clicks on 24h you need disable the button and make a future.delayed function to able the button after 24h. Just make sure to cancel the future function when the user change screen. A example of how cancel future.delayed function is here.

    import 'dart:async';
    
    class MyScreen extends StatefulWidget {
      @override
      _MyScreenState createState() => _MyScreenState();
    }
    
    class _MyScreenState extends State<MyScreen> {
      final _cancelToken = CancelToken();
    
      @override
      void dispose() {
        // Cancel the scheduled task when the screen is disposed
        _cancelToken.cancel();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RaisedButton(
              child: Text('Execute Function X'),
              onPressed: () {
                // Schedule the task to run after 5 seconds
                Future.delayed(Duration(seconds: 5), _executeFunctionX, cancelToken: _cancelToken);
              },
            ),
          ),
        );
      }
    
      void _executeFunctionX() {
        // This function will only execute if the cancel token is not cancelled
        if (!_cancelToken.isCancelled) {
          // Code to execute
        }
      }
    }
    
    Login or Signup to reply.
  2. You can save the last button press time to shared_preferences and you just have to calculate the time difference if it is more than 24 hours when you activate the button.

    class MyScreen extends StatefulWidget {
      @override
      _MyScreenState createState() => _MyScreenState();
    }
    
    class _MyScreenState extends State<MyScreen> {
    
      int btnCount = 0;
      String timeString;
    
      final SharedPreferences prefs = await SharedPreferences.getInstance();
      
      @override
      void initState() {
        super.initState();
    
        btnCount = prefs.getInt('counter');
        time = prefs.getString('action');
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RaisedButton(
              child: Text('Execute Function X'),
              onPressed: () {
                int remaining = differenceTwoTime(target: DateTime.parse(timeString));
    
                if(remaining > 24){
                   log('button is activate');
    
                   if(btnCount == 2){
                     await prefs.setInt('counter', 2);
                     prefs.setString('action');
                   }else{
                     btnCount++;
                   }
                }else{
                   log('button is not activate');
                }
              },
            ),
          ),
        );
      }
    
      int differenceTwoTime({DateTime? from, required DateTime target}) {
         return target.difference(from ?? DateTime.now()).inHours;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search