首页 > 解决方案 > 如何在 Flutter 中更新 Text()

问题描述

如何使用 RaisedButton 更新 Text()?

我需要在没有 setState 的情况下运行它

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

谢谢你帮助我:)

标签: flutterflutter-text

解决方案


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

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

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

并像这样更新它;

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

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


推荐阅读