屬性介紹
TextEditingController controller = TextEditingController();
Widget buildTextField(TextEditingController controller) {
return TextField(
//控制正在編輯的文本。通過其可以拿到輸入的文本值
//獲取方式 String value=controller.text
controller: controller,
//給TextField設(shè)置裝飾(形狀等)
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(color: Colors.transparent)
),
//輸入內(nèi)容距離上下左右的距離 ,可通過這個(gè)屬性來控制 TextField的高度
contentPadding: EdgeInsets.all(10.0),
fillColor: Colors.white, filled: true,
// labelText: 'Hello',
// 以下屬性可用來去除TextField的邊框
disabledBorder: InputBorder.none,
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
),
//鍵盤類型
keyboardType: TextInputType.text,
//控制鍵盤的功能鍵 指enter鍵,比如此處設(shè)置為next時(shí),enter鍵
//顯示的文字內(nèi)容為 下一步
textInputAction: TextInputAction.next,
//鍵盤大小寫的顯示 Only supports text keyboards 但是好像不起作用?
//characters 默認(rèn)為每個(gè)字符使用大寫鍵盤
//sentence 默認(rèn)為每個(gè)句子的第一個(gè)字母使用大寫鍵盤
//word 默認(rèn)為每個(gè)單詞的第一個(gè)字母使用大寫鍵盤。
//none 默認(rèn)使用小寫
textCapitalization: TextCapitalization.words,
//是否是密碼
obscureText: false,
//文本對齊方式(即光標(biāo)初始位置)
textAlign: TextAlign.center,
//輸入文本的樣式
style: TextStyle(fontSize: 30.0, color: Colors.blue),
//最大行數(shù)
maxLines: 1,
//最大長度,設(shè)置此項(xiàng)會讓TextField右下角有一個(gè)輸入數(shù)量的統(tǒng)計(jì)字符串
//這種情況一般是不符合我們設(shè)計(jì)的要求的,但是有需要限制其輸入的位數(shù)
// 這時(shí)候我們可以使用下方的屬性來限制
maxLength: 9,
// 跟最大長度限制對應(yīng),如果此屬性設(shè)置為false,當(dāng)輸入超過最大長度
// 限制時(shí),依然會顯示輸入的內(nèi)容,為true不會(默認(rèn)為 true)
maxLengthEnforced: false,
// 限制輸入的 最大長度 TextField右下角沒有輸入數(shù)量的統(tǒng)計(jì)字符串
// inputFormatters: [LengthLimitingTextInputFormatter(11)],
//允許的輸入格式 下方的模式指只允許輸入數(shù)字
// inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
//控制此小部件是否具有鍵盤焦點(diǎn)。
focusNode: FocusNode(),
//是否自動更正
autocorrect:false,
//是否自動獲得焦點(diǎn)
autofocus: false,
//是否禁用textfield:如果為false, textfield將被“禁用”
enabled: true,
//光標(biāo)顏色
cursorColor: Colors.red,
//光標(biāo)寬度
cursorWidth: 5.0,
//光標(biāo)圓角弧度
cursorRadius: Radius.circular(5.0),
//內(nèi)容改變的回調(diào)
onChanged: (text) {
print('change $text');
},
//內(nèi)容提交(按回車)的回調(diào)
onSubmitted: (text) {
print('submit $text');
},
//按回車時(shí)調(diào)用
onEditingComplete: (){
print('onEditingComplete');
},
);
}