主要通過controller 實現,具體代碼如下
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)))), ),