skip to Main Content

Please i’m new to flutter. i want users to be able to withdraw once in a day at any given time but i would want the withdrawal button to be disabled after their first click or first withdrawal then by tomorrow the button will be active. please how can i try this, i have tried using sharedpreferences but to no avail.

i tried this code below but am getting error "FormatException: Invalid date format" also i have tried changing the date format but to no avail. please any help will be so much appreciated.



ElevatedButton(
  onPressed: () async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
   // var currentTime =  DateTime.now();
   //  var _formatter = DateFormat('yyyy/MM/dd HH:mm').format(currentTime);
   // var resetTime =  DateTime.parse(prefs.getString("time").toString()); 
   // final resetTime = DateTime.parse(prefs.getString("time") ?? '');

    var currentTime =  DateTime.now();
    var resetTime =  DateTime.parse(prefs.getString("time").toString());
    if(resetTime != null && !compareDate(currentTime, resetTime))
                                    prefs.setBool("one_tap", true);
                                  
      if(prefs.getBool("one_tap") == null || prefs.getBool("one_tap")!){
          print("Once in a day");
          title = "Change Date";
          prefs.setBool("one_tap", false);
          prefs.setString("time", currentTime.toString());
         }
},
child: Text(title),),

compareDate(DateTime current, DateTime reset) {
    if (reset != null) {
      return current.year == reset.year &&
          current.month == reset.month &&
          current.day == reset.day;
    } else {
      return true;
    }
  }

3

Answers


  1. There are two options to do that :

    1. When withdrawn is done at that time you have to save that datetime is your backend database, and when user came back to same withdrawn button page that time hit the api to check the last withdrawal datetime and then you can match with your current datetime and if its more than a day then enable the button and vice versa.
    2. Save a bool variable in backend when withdrawal success and in first option we are calculating the remaining time in frontend so now do that in backend and same logic will run and you can enable/disable button using that bool variable
    Login or Signup to reply.
  2. For unabling user to click button in a period of time you can simply use DateTime as you wrote before.
    You should save the time in sharedprefrences or other databases when user click on the button and check the current time to the passed time when user click on the button again and for that you can get the passed time like the code bellow:

    // get the submit button time and then store it in sharedpref
      DateTime submitButtonTime  = DateTime.now();
    
      // get the current time when you want to check the passed time
      DateTime currentTime = DateTime.now().add(Duration(days: 1));
    
      // you can use "difference" method to get the passed time between two 
      //times
      num passedTime = currentTime.difference(submitButtonTime).inHours;
    
      // print the passed time in hours, there are other types like  
      //minutes or milliseconds if you want to get more precise to the passed 
      //time
      print(passedTime);
    
    Login or Signup to reply.
  3. Try that pseudo code, the following code is not guaranteed to be produceable, but gather the idea from it:

    just make a method to decide whether the button is enabled or not:

    bool makeItEnabled(){
    
    SharedPreferences prefs = SharedPreferences.getInstance();
    
    String? lastClickTime = prefs.getString('lastWithdraw');
    
    if(lastClickTime == null){
    
        // set that button enabled
        return true;
    }
    
    return (DateTime.now().diff(DateTime.parse(lastClickTime).hours > 24 );
    
    }
    

    and in your button:

    YouButton(
    
    enabled: makeItEnabled(),
    onPressed:()async{
    
        // make your withdraw
        // store that time in sharedpref
        SharedPreferences prefs = SharedPreferences.getInstance();
        await prefs.setString('lastWithdraw' , DateTime.now());
    
    }
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search