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
There are two options to do that :
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:
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:
and in your button: