将变量附加到文本字段的文本

Attach a variable to the text of a Textfield

在 flutter 中,您可以将变量用作文本的 属性。例如,如果您声明:

String myVar= "Hello world";

当你使用它时:

Text(myVar)

然后你改变变量myVar的值,Text对象的文本也会改变。

那么,是否可以对文本字段的文本执行类似的操作?或者唯一的方法是使用 TextEditingController,然后每次更改此对象的文本 属性。

您可以在此处找到更多信息:https://flutter.io/tutorials/interactive/

这就是@Gunter 所说的:

    class SampleWidgetState extends State<SampleWidget> {
      String myVar = "Hello world";

      @override
      Widget build(BuildContext context) {
        return Center(
          child: Column(
            children: <Widget>[
              Text(myVar),
              RawMaterialButton(
                child: Text("press me"),
                onPressed: () {
                  setState(() {
                    myVar = "By World";
                  });
                },
              )
            ],
          ),
        );
      }
    }