标签: txtextcontrol

如何根据条件将禁用按钮变为启用按钮

我是 flutter 新手,只要某些文本字段为空,我就会尝试禁用一个按钮,所以我制作了 3 个文本控制器来充当 3 个文本字段的控制器,并且我制作了一个函数来检查该函数是否为:

bool isEmpty(){
    setState(() {
          if(textEditingController.text!=" "&&textEditingController2.text!=" "&& textEditingController3.text!=" "){
            isButtonEnabled=true;


          }
        });
       return isButtonEnabled; 
  }
Run Code Online (Sandbox Code Playgroud)

文本字段的代码是:

TextField(
                  onSubmitted:null,

                  controller: textEditingController3,


                )
Run Code Online (Sandbox Code Playgroud)

然后我为按钮编写代码如下:Center(

  child: RaisedButton(
         child: Text("Button 1",style: TextStyle(color:Colors.white),),
          onPressed:isButtonEnabled?(){ print("enabled");}:null,
          color: Colors.red,

            ),
Run Code Online (Sandbox Code Playgroud)

问题是即使我在文本字段中写入后,该按钮仍然处于禁用状态。任何想法?提前致谢。 编辑: 感谢@diegovoper的回答,这有效,但是如果我想输入初始值并且我希望仅当文本字段具有如下文本字段值时才启用按钮怎么办:

@override
  void initState() {
    super.initState();
   textEditingController  = new TextEditingController(text: name);
      textEditingController2  = new TextEditingController(text: email);
         textEditingController3  = new TextEditingController(text: " place");


  }
Run Code Online (Sandbox Code Playgroud)

然后我将 isEmpty 方法更新为:

bool isEmpty(){
    setState(() {
          if((textEditingController.text!=" ")&&(textEditingController2.text!=" ")&& (textEditingController3.text!=" ")&&(textEditingController!=null)&&(textEditingController2!=null)&&(textEditingController3!=null)){
            isButtonEnabled=true;



          }
          else{ …
Run Code Online (Sandbox Code Playgroud)

button textfield txtextcontrol flutter

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

button ×1

flutter ×1

textfield ×1

txtextcontrol ×1