[英]How to change TextField border color at onChanged (change character) in flutter?
我需要在更改基於 email 或電話驗證的字符時更改文本字段邊框顏色。
例如:
當鍵入不完整的 email 時邊框顏色設置為紅色,完成鍵入 email 后邊框顏色設置為白色。
當正在輸入的文本文件字符長度小於 10 時設置邊框的紅色,當字符長度為 10 時設置白色。
快速單詞:使用 Getx、Bloc、Provider 等更改字符時更改邊框顏色...
我已經以正常方式嘗試了,但是已經嘗試使用BLOC State 在我的項目中的 You Tube 上管理這個視頻,請參考這個視頻,它對你很有幫助
電子郵件驗證 function
emailValidation(emailValue) {
return RegExp(
r'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$',
).hasMatch(emailValue);
}
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) => value!.isEmpty
? 'Field Not Empty'
: !emailValidation(value)
? 'Enter Valid Email Address'
: null,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Email Address',
labelText: 'Email',
),
),
變量聲明
final text = TextEditingController();
bool validate = false;
處理方法:
void dispose() {
text.dispose();
super.dispose();
}
TextField(
onChanged: (value) {
setState(() {
text.text.isEmpty || !emailValidation(text.text)
? validate = true
: validate = false;
});
},
controller: text,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'Enter Email Address',
errorText: validate ? 'Enter Valid Email' : null,
),
),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.