TextField是一個經常使用的控件,同時它也是一個組合控件,由多個控件組合而成。
這是來自Material官方網站的的圖片
TextField是由7個控件組成,其中有些控件默認不顯示,咱們能夠對各個控件單獨設置想要的樣式來知足不一樣的UI展現需求。
下面咱們就來列舉幾種常見的樣式:node
1. 簡單的TextFieldgit
TextField( decoration: InputDecoration( labelText: "最基本的的TextField", ), )
TextField
接收一個InputDecoration
做爲參數,InputDecoration
初始化的參數labelText
能夠幫助咱們定義placeholder。labelText
模式會灰色的,選中以後會變爲藍色,而且TextField
底部會有一條藍色線條。github
2. 限制字符的長度app
TextField( maxLength: 10, decoration: InputDecoration( labelText: "最多10個字符", ), )
maxLength
能夠設置最長字符個數,若是超過這個限制再次輸入不會有顯示,而且在TextField
在有右下角有當前字符個數的標記,此處是10/10
。ide
3. 限制行數網站
TextField( maxLines: 2, decoration: InputDecoration( labelText: "兩行文字,超出的文字上翻", ), )
maxLines
參數能夠設置行數,好比這裏設置的是2,默認只會顯示兩行,超過兩行的部分只能經過上下滾動來顯示。spa
默認行數是1,超過的部分會往左縮進。
4. labelText設置顏色code
TextField( decoration: InputDecoration( labelText: "labelText 有顏色", labelStyle: TextStyle(color: Colors.red), ), )
InputDecoration
能夠設置labelStyle
參數,接收一個TextStyle
對象,TextStyle
這個咱們比較熟悉,在以前講解Text
的文章中已經作了不少詳解了。設置顏色以後,當點擊TextField
以後,文字會變小,顏色也是設置好的顏色。對象
5. 左側Iconblog
TextField( decoration: InputDecoration( icon: Icon(Icons.account_box), labelText: "左側有一個Icon", ), )
icon
參數能夠傳入一個Icon
對象用來顯示在TextField
的左側,咱們能夠傳入各式各樣的Icon
,知足咱們更豐富的展現需求。
6. 右側Icon suffix和suffixIcon
TextField( decoration: InputDecoration( labelText: "右側的兩個Icon suffix 和 suffixIcon", suffix: Icon(Icons.account_box), suffixIcon: Icon(Icons.add), ), )
suffixIcon
默認是顯示在右側的,TextField
被點擊以後會顯示爲被選中狀態,suffix
默認不顯示,只有當選中TextField
的時候纔會顯示出來。
7. 輔助提示
TextField( decoration: InputDecoration( labelText: "下方帶有輔助提示的TextField", helperText: "我是輔助提示", helperStyle: TextStyle(color: Colors.red), ), )
helperText
能夠幫助咱們在TextField
下面顯示一行提示文字,一樣咱們也能夠使用helperStyle
來設置這段提示文字的樣式。
8. 點擊後的提示 hintText
TextField( decoration: InputDecoration( labelText: "點擊後會有提示", hintText: "我是點擊後的提示", hintStyle: TextStyle(color: Colors.red), ), )
hintText
參數能夠幫助咱們設置一個點擊後顯示的文字,只有點擊以後才能夠顯示,一樣咱們能夠經過hintStyle
來設置hintText
的樣式。
9. 不顯示下劃線
TextField( decoration: InputDecoration( labelText: "選中時沒有下劃線", focusedBorder: InputBorder.none, ), )
focusedBorder
能夠幫助咱們設置下劃線的樣式,若是傳入InputBorder.none
則不會顯示下劃線。
10. 自定義下劃線樣式
TextField( decoration: InputDecoration( labelText: "選中時的下劃線顏色", focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.red), ), ), )
咱們能夠給focusedBorder
傳入自定義的UnderlineInputBorder
來自定義下劃線的樣式,這裏咱們簡單作了個顏色的改變。
平常開發中,咱們每每但願在三個地方TextField能夠給咱們回調。
TextField
提供了三個回調方法
onChanged
此方法是在輸入有變化的時候就會回調。參數是當前已經輸入的內容onSubmitted
此方法是在咱們輸入完成後,點擊鍵盤上回車的時候回調。參數是當前的已經輸入的內容onEditingComplete
此方法也是在點擊鍵盤上回車的時候回調,它會在onSubmitted
以前執行。不會帶有參數須要注意是onEditingComplete
回調方法沒有攜帶參數。若是咱們須要在onEditingComplete
方法中獲取到當前的輸入值。
那就須要經過TextEditingController
來捕捉輸入內容,TextField
接收一個TextEditingController
對象來做爲controller
參數,
經過TextEditingController
的屬性text
咱們也能夠獲取到當前的輸入內容。
11. 事件回調
TextEditingController controller = TextEditingController(); TextField( controller: controller, onChanged: (value) { print("onChanged value = " + value); }, onSubmitted: (value) { print("onSubmitted value = " + value); }, onEditingComplete: () { print("onEditingComplete value = " + controller.text); }, decoration: InputDecoration( labelText: "輸入事件監聽", ), )
能夠看到經過controller.text
能夠獲取到當前的輸入內容。
12. 鍵盤迴收
TextField( decoration: InputDecoration( labelText: "鍵盤迴收", suffixIcon: IconButton( icon: Icon(Icons.close), onPressed: () { FocusScope.of(context).requestFocus(FocusNode()); }), ), )
FocusNode
能夠幫助咱們進行鍵盤的回收,我只須要將FocusScope
的requestFocus
方法中傳入一個新的FocusNode
對象即刻。
若是在開發過程當中,咱們但願經過點擊頁面上某個按鈕來結束TextField
輸入而且獲取到當前的輸入內容。使用FocusNode
是頗有效的。
想體驗以上的示例的運行效果,能夠到個人Github倉庫項目flutter_app
->lib
->routes
->textfield_page.dart
查看,而且能夠下載下來運行並體驗。