【发布时间】:2018-12-11 05:27:54
【问题描述】:
下面是一个对话框,通过使用文本字段和按钮来捕获用户输入。当 textField 为空时,该按钮被禁用,但是当 textField 被填充值时,它继续被禁用。这是因为 _textController.text 状态没有被更新(在这个小部件中再次呈现)。
void _pushAdd() async {
await showDialog(
context: this.context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Add a custom word'),
content: _renderForm(),
actions: <Widget>[
FlatButton(
child: Text('ADD'),
onPressed: (_textController.text.isNotEmpty) ? () => _addNewPair() : null,
),
],
);
},
);
// Changed _pushAdd as async to handle onClose and clear _textController upon exit
_textController.clear();
目前 _textController 是在顶部的类中启动的(不是 init)。
var _textController = new TextEditingController();
带有 _textController 的 textField 位于此处:
Widget _renderForm() {
return Container(
height: 50.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
autofocus: true,
textCapitalization: TextCapitalization.words,
controller: _textController,
decoration: InputDecoration(
hintText: 'RedPotato',
),
),
]
)
);
}
我最初的计划是使用 onChanged: + 另一种存储文本的状态。但是,我怀疑这样做是否有效。 所以我问,实时处理 TextField 值以便其他 Widget 可以监听更改的标准方法是什么?
【问题讨论】: