如何在Flutter中更新Text()

问题描述 投票:-1回答:1

如何使用RaisedButton更新Text()?

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

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

感谢您的帮助:)

flutter flutter-text
1个回答
2
投票

首先,您必须在StatefulWidget中,因此您可以调用setState。如果您在不调用setState的情况下更改变量,则不会在UI中看到更改。

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

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

并像这样更新它;

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

这里是whole app,您可以自己运行。

© www.soinside.com 2019 - 2024. All rights reserved.