I want to disable a button for 900ms(using flutter). How can I do that?
if (isButtonDisabled == false) {
incrementCounter();
AudioPlayer().play(
AssetSource(
Assets.correct_answer_audio),
volume: 1.0);
_onTyping();
await Future.delayed(
const Duration(milliseconds: 900));
Navigator.of(context).pop();
);
}
2
Answers
to disable an
ElevatedButton
, you need to setnull
asonPressed
when it is supposed to be disabled. Then enable it through either aFuture.delayed
orTimer
by setting your enabled variable to true and callingsetState
. You can check this code snippet in Dartpad.For
Disabled Button
=>onPressed : null
For
Active Button
=>onPressed : (){}
Use
Timer
after certain interval set theisDisabledButton
tofalse
which would be by defaulttrue
Example:
For first 2 seconds:
For After 2 seconds: