根据颤动中的文本字段启用/禁用提升按钮

问题描述 投票:0回答:1

我想在textformfield不为空时启用按钮,而在textformfield为空时禁用按钮。

bool isEnabled = false;
  TextEditingController _textEditingController = TextEditingController();
RaisedButton(
                // disabledColor: Colors.grey,
                child: Text("Click"),
                onPressed: () {
                  setState(() {
                    if (_textEditingController.text.isNotEmpty) {
                      isEnabled = true;
                    } else
                      isEnabled = false;
                  });
                })
flutter dart flutter-layout
1个回答
1
投票

现在,您正在做的事情是错误的,因为您无法使用onPressed启用禁用的按钮。

应该这样做,在TextFormField内部,将isEnabled设置为true的值是输入文本长度大于零,否则为false

        TextFormField(
            onChanged: (text) {
                setState(() {
                  if(text.length>0)
                     isEnabled=ture;
                  else
                     isEnabled=false;
                  });
            }

然后像这样禁用/启用按钮,

onPressed: isEnabled?
    (){
     //do something
   } 
: null;// or pass blank (){}

-1
投票
  TextEditingController _textEditingController = TextEditingController();

        RaisedButton(               
                child: Text("Click"),
                onPressed:_textEditingController.text.isEmpty ? null : () { })

© www.soinside.com 2019 - 2024. All rights reserved.