gpt4 book ai didi

dart - 从 Flutter TextField 读取值

转载 作者:IT王子 更新时间:2023-10-29 07:21:14 26 4
gpt4 key购买 nike

我有一个带有 TextField 的小部件,我正在从 StreamBuilder 初始化它,尝试使用 bloc 模式。一个 Contact 模型正在通过流进入。这用于最初填充 TextField。我的问题是关于在用户更新 TextField 然后按下“保存”按钮后读取值。如何从 TextField 中读取值。我已经包含了一个简单的例子来说明我正在尝试做的事情。

  void getTextValues() {
//???
}

@override
Widget build(BuildContext context) {
return StreamBuilder<Contact>(
stream: bloc.getContact,
builder: (context, contact) {
return Column(
children: <Widget>[
TextField(
controller: TextEditingController(text: contact.data.name),
),
new RaisedButton(
padding: const EdgeInsets.all(8.0),
textColor: Colors.white,
color: Colors.blue,
onPressed: getTextValues,
child: new Text("Save"),
),
],
);
},
);
}

我想我可以声明一个 TextEditingController 并将其分配给 Controller 属性,但我看不到从 StreamBuilder 中给它一个初始值的方法。我错过了什么吗?

  TextEditingController nameController = TextEditingController();

....

controller: nameController,

最佳答案

I think I could declare a TextEditingController and assign that to the controller property but I don't see a way to give that an initial value from the StreamBuilder.

有办法。将您的构建器代码更改为:

builder: (context, contact) {
nameController.value = TextEditingValue(text: contact.data.name);
return Column(
children: <Widget>[
TextField(
controller: nameController,
),
new RaisedButton(
padding: const EdgeInsets.all(8.0),
textColor: Colors.white,
color: Colors.blue,
onPressed: getTextValues,
child: new Text("Save"),
),
],
);
},

关于dart - 从 Flutter TextField 读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55544254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com