TextField作为Flutter的输入框,需要设置控制器,有时候设置默认值,我们需要对控制器做处理。
代码如下:
TextField(
//输入键盘类型
keyboardType: TextInputType.text,
autofocus: true,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: BorderSide.none),
),
onChanged: (value) {
this._keyword = value;
},
controller: TextEditingController.fromValue(TextEditingValue(
text: '${this._keyword == null ? "" : this._keyword}', //判断keyword是否为空
// 保持光标在最后
selection: TextSelection.fromPosition(TextPosition(
affinity: TextAffinity.downstream,
offset: '${this._keyword}'.length)))),
),