如何在颤动中显示一个字符串变量,当按下一个按钮时它会发生变化?

How to show a string variable in flutter which changes when a button is pressed?

我想显示一个字符串变量,它会在按下按钮时改变它的值。在主体内部有变量名pressedText。它是一个静态变量。当按下按钮时,我想显示它的更新值,但现在无法显示任何内容。我可以显示它的更新值吗?

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Second Route"),
      ),
      body: Column(
        children: <Widget>[
          Text(
            pressedText,
            style: TextStyle(
              fontSize: 30.0,
            ),
          ),
          Expanded(
            child: Center(
              child: Wrap(
                children: _buildButtonsWithNames(),
              ),
            ),
          )
        ],
      ),      
  }
}

您可以使用 setState 进行更改,并将您的 Wrap 小部件与 InkWll 绑定并在 onTap 函数中调用 setState 或者您可以将一个函数传递给您的 _buildButtonsWithNames .

Expanded(
            child: Center(
              child: InkWell(
                onTap:(){
                   setState(() {
                    pressedText = "change Text"
                  });
                 },
                child: Wrap(
                children: _buildButtonsWithNames(),
                ),
              )
            ),
          )