在颤动中按下几秒钟时更改按钮的文本

Change text of a button when is pressed in flutter for a few seconds

我正在尝试在按下 2-3 秒后更改按钮的文本。所以,如果我按下“SAVE GOALS”按钮,我想将其文本更改为“SAVED”2 秒,然后再返回“SAVE GOALS”。我知道如何更改为“SAVED”,但我不知道如何更改回“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,
                              )
                          ),
                        ),

你可以这样做:

onPressed: () {
              setState(()  {
                saveGoalsButtonText = "SAVED!";
              });
              Future.delayed(const Duration(seconds: 3), () {
                setState(() {
                  saveGoalsButtonText = "SAVE GOALS";
                });

              });

            } ,

结果: