I want to disable a button for the day it was clicked. For example; I have a homepage, user clicks a button and go to another page. Does do something on second page and return to homepage. After that user can't click this button again for today. How can i create this logic in flutter? I found 'timer_builder' package on pub.dev but i couldn't implement this logic.


Solution 1: m123

You can do this by wrapping the button with an IgnorePointer.

bool _shouldIgnore = false;

IgnorePointer(
   ignoring: _shouldIgnore,
   child: [YOURBUTTON],
),

And to switch the button on and off time-controlled, I would recommend it to you. Here I leave the button locked for exactly 24 hours after an action. You need this package for saving the data locally.

When the action is done on the second page:

_lockButton() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();

  var _date = DateTime.now();
  await prefs.setString('lastPressed', _date.toString);
}

On the firstpage:

//InitState is called when the page is built.
void initState() async{ 
  SharedPreferences prefs = await SharedPreferences.getInstance();

  //DateTime.parse because you can only save Strings locally.
  // .add Adds 12h to the date when the button was last pressed.
  var _date= DateTime.parse(prefs.getString('lastPressed')).add(Duration(hours: 12));

  if(_date.isBefore(DateTime.now()){
   _shoudlIgnore = false;
  }
  else {_shoudlIgnore = true;}
}

Attention you have to initialize the variable _shouldIgnore before the InitState.