主要通过controller 实现,具体代码如下
1TextField( //输入键盘类型 2 keyboardType: TextInputType.text, 3 autofocus: true, 4 decoration: InputDecoration( 5 border: OutlineInputBorder( 6 borderRadius: BorderRadius.circular(30), 7 borderSide: BorderSide.none), 8 ), 9 onChanged: (value) { 10 this._keyword = value; 11 }, 12 controller: TextEditingController.fromValue(TextEditingValue( 13 text: '${this._keyword == null ? "" : this._keyword}', //判断keyword是否为空 14 // 保持光标在最后 15 16 selection: TextSelection.fromPosition(TextPosition( 17 affinity: TextAffinity.downstream, 18 offset: '${this._keyword}'.length)))), 19 ),