简体   繁体   English

flutter - 如何在复选框值更改时禁用/只读特定的文本输入字段?

[英]flutter - how to disable/make read only a specific text input field on a checkbox value change?

I have a checkbox that I would like to disable a field when it is unchecked.我有一个复选框,我想在未选中时禁用一个字段。 How would I do that?我该怎么做? This it the current checkbox code I have so far...这是我目前拥有的当前复选框代码......

              child: CheckboxListTile(
                title: Text("Auto Calculate Big Blind"),
                value: _autoCalcBigBlind,
                onChanged: (bool value) {
                  setState(() {
                    if (value == false) { //focus on the big blind text field
                      _bigBlindFocusNode.requestFocus();
                    } else {

                      //first check small blind is not null
                      if (_textEditControllerSmallBlind.text == null || _textEditControllerSmallBlind.text == '0'){
                        //if the field is null/0, can't double it, so focus on it
                        _smallBlindFocusNode.requestFocus();
                      }else{  //the big blind value = double the current small blind value. we know small blind field is not null
                        _textEditControllerBigBlind.text = (int.tryParse(_textEditControllerSmallBlind.text) * 2).toString();
                       **//at this point, i want a specific text input field disabled. how to do that?** 
                     }
                    }
                
        

You need to toggle the value of the enabled property of TextFormField or TextField .您需要切换TextFormFieldTextFieldenabled属性的值。 If the enabled is false it would disable the TextFormField or TextField .如果enabled为 false ,它将禁用TextFormFieldTextField

Create bool member such as bool _isTextFieldEnabled = false;创建 bool 成员,例如bool _isTextFieldEnabled = false; in the State class of a StatefulWidget & set _isTextFieldEnabled to true when you want to disable the TextField by calling setState.在 StatefulWidget 的 State class 中,当您想通过调用 setState 禁用 TextField 时,将_isTextFieldEnabled设置为true

Checkout this DartPad sample I created to demonstrate this: DartPad查看我创建的这个 DartPad 示例来演示这一点: DartPad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM