如何在 Flutter 中更新 Text()



如何使用 RaisedButton 更新 Text((?

我需要在没有设置状态的情况下运行它

RaisedButton(
  child: Text('Press Me To update Text !'),
  onPressed: (){
    changetext = 'it has been changed';
  },
)

感谢您帮助我:)

首先,你必须在StatefulWidget内,这样你才能调用setState。如果在不调用 setState 的情况下更改变量,则不会在 UI 中看到更改。

您必须将文本存储在变量中

String _text = 'Press Me To update Text !';

并像这样更新它;

RaisedButton(
   onPressed: () {
      setState(() {
        _text = 'The text is updated';
      });
   },
   child: Text(_text),
)

这是整个应用程序,您可以自己运行它。

最新更新