# Flutter TextField 介紹
## 基本屬性介紹
* decoration: 外框樣式
* autofocus: 自動關注(自動跳出小鍵盤)
* textInputAction: 小鍵盤自帶按紐樣式,需要確定IOS及安卓是否相容

```dart
enum TextInputAction {
none, //沒有任何動作
unspecified, //讓操作系統決定哪個動作更合適
done, //完成動作,一般會顯示“完成”二字
go, //跳轉動作,一般用於輸入了一個超鏈接後執行該動作。鍵盤上會顯示“前往”二字
search, //搜索動作
send, //發送
next, //下個
previous, // 返回前一個
continueAction, //繼續動作,在Android上好像沒反應,不顯示鍵盤
route, //在Android上好像沒反應,不顯示鍵盤
emergencyCall, //撥打緊急電話,在Android上好像沒反應,不顯示鍵盤
newline, //在Android上好像沒反應,換行
}
```
* onSubmitted: 點擊自帶按鈕後觸發的事件
```dart
onSubmitted: (val){
print("點擊了鍵盤上的動作按鈕,當前輸入框的值爲:${val}");
},
```
* onChanged:
```dart
onChanged: (val) {
print("當前輸入框的值爲:${val}");
},
```
* TextInputFormatter: 用於限制輸入的內容,官方提供以下幾種限制方式
* FilteringTextInputFormatter.allow 白名單,也就是只允許輸入符合規則的字符
* FilteringTextInputFormatter.deny 黑名單,除了規定的字符其他的都可以輸入
```dart
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[a-zA-Z]')),
],
```
## 完整程式範例
```dart
late TextEditingController searchTextController;
@override
void initState() {
super.initState();
getPreviousSearches();
searchTextController = TextEditingController(text: '');
}
@override
void dispose() {
searchTextController.dispose();
super.dispose();
}
Expanded(
child: TextField(
decoration: const InputDecoration(
border: InputBorder.none, hintText: 'Search'),
autofocus: false,
textInputAction: TextInputAction.done,
onSubmitted: (value) {
if (!previousSearches.contains(value)) {
previousSearches.add(value);
savePreviousSearches();
}
},
controller: searchTextController,
)),
```
###### tags: `flutter`