【flutter】动态控制按钮是否可点击

需求是:在添加信息之前先判断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;
  	}
  }
发布了57 篇原创文章 · 获赞 3 · 访问量 6178

猜你喜欢

转载自blog.csdn.net/qq_39830579/article/details/104920059
今日推荐