我有一个函数,当用户选择错误答案时返回 false。
tappedbutton(int index) async {
final userAnswer = await userAnswer();
if (userAnswer) {
// Executing some code
}else{
ErrorSnackbar(); // This snackbar takes 2 second to close.
}
Run Code Online (Sandbox Code Playgroud)
我的目标是在用户选择错误答案后延迟调用该函数两秒钟(用户可以再次单击按钮,不会触发任何操作)并阻止立即单击。我怎样才能实现它?
Mar*_*ski 11
您必须在外部范围中添加一个辅助变量,该变量将指示用户是否处于答案冷却时间。
最短的解决方案将是:
var answerCooldownInProgress = false;
tappedbutton(int index) async {
// Ignore user taps when cooldown is ongoing
if (answerCooldownInProgress) {
return;
}
final userAnswer = await userAnswer();
if (userAnswer) {
// ...
} else {
ErrorSnackbar();
answerCooldownInProgress = true;
await Future.delayed(const Duration(seconds: 2));
answerCooldownInProgress = false;
}
}
Run Code Online (Sandbox Code Playgroud)
Future.delayed(const Duration(seconds: 2)).then((val) {
// Your logic here
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23587 次 |
| 最近记录: |