我試圖在按下 2-3 秒后更改按鈕的文本。所以,如果我按下“SAVE GOALS”按鈕,我想將其文本更改為“SAVED”2 秒鐘,然后回傳“SAVE GOALS”。我知道如何更改為“已保存”,但我不知道如何更改回“已保存目標”。延遲,睡眠等,沒有任何效果。我在一個有狀態的小部件中。
OutlinedButton(
onPressed: () {
setState(() {
saveGoalsButtonText = "SAVED!";
Future.delayed(Duration(seconds: 3));
saveGoalsButtonText = "SAVE GOALS";
});
goals = _goalsController.text;
_saveGoals();
//Navigator.pushReplacementNamed(context, '/masterclasses');
} ,
style: OutlinedButton.styleFrom(
primary: const Color(0xffE4BDB6),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
side: const BorderSide(width: 3, color: Color(0xffE4BDB6)),
),
child: Text(
saveGoalsButtonText,
style: const TextStyle(
color: Color(0xff221F1E),
fontSize: 14,
fontWeight: FontWeight.w700,
)
),
),
uj5u.com熱心網友回復:
你可以這樣做:
onPressed: () {
setState(() {
saveGoalsButtonText = "SAVED!";
});
Future.delayed(const Duration(seconds: 3), () {
setState(() {
saveGoalsButtonText = "SAVE GOALS";
});
});
} ,
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425231.html
