【发布时间】:2021-04-30 08:31:58
【问题描述】:
每当 TextFields 文本发生更改时,我想在 Text 中显示一些内容:
class _MyPageState extends State<MyPage> {
String name;
@override
Widget build(BuildContext context) {
TextEditingController c = new TextEditingController(text: name);
c.addListener(() {
setState(() { name = c.text;});
});
return Scaffold(
body: Center(
child: Column(children: [
Text('Hello, ' + name + '!'),
TextField(controller: c)
])));
}
}
Text 会按预期更新,但问题是每次输入字符时 TextField 的光标都会移动到位置 0。
【问题讨论】: