【问题标题】:Change text of a button when is pressed in flutter for a few seconds在颤动中按下几秒钟时更改按钮的文本
【发布时间】:2022-02-15 21:10:15
【问题描述】:

我试图在按下 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,
                              )
                          ),
                        ),

【问题讨论】:

    标签: flutter button


    【解决方案1】:

    你可以这样做:

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

    结果:

    【讨论】:

    • 谢谢!我的错误是我没有将第二个 setState 放在 Future.delayed 中,我认为它像睡眠一样工作,我是新手。它有效!
    猜你喜欢
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多