需求是:在添加信息之前先判斷textfiled裏面有沒有輸入信息,否則按鈕就變灰且不能點擊。
監聽:textfiled裏面設置onchange屬性,並setstate進行刷新
TextField(
onChanged: (value) {
setState(() {
this.phone = value;
if (value.length >= 11)
this.phoneEnough = true;
else
this.phoneEnough = false;
});
},
控制按鈕是否能點擊:當按鈕的onpressed的方法爲null的時候,就不能點擊,再修改disabledcolor屬性,就能控制顏色。
寫個小事例。
MaterialButton(
height: 50,
minWidth: double.infinity,
color: Color(0xff0096ff),
disabledColor: HitchColor.gray_999999,
onPressed: this._buttonClickListener(),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
child: Text("確認", textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 16)),
)
//點擊事件,不能點擊則返回null
_buttonClickListener() {
if(this.canBeClicked){
return (){//業務代碼}
}else{
return null;
}
}