Home > Enterprise >  make TextField enable after button click not work flutter
make TextField enable after button click not work flutter

Time:12-10

I have simple text Field. User not allow to write in this Text Field before click special button. This is my code , this feature not work but all other stuff in it work fine, what i did wrong ?

   TextEditingController _textFieldController = TextEditingController();
    bool isTextFieldEnable = false;
    FocusNode _focusNode = FocusNode();
  Row(
            children: [
              Container(
                width: 305,
                margin: EdgeInsets.only(left: 34),
                height: 250,
                child: SingleChildScrollView(
                    child: TextField(
                  controller: _textFieldController,
                  decoration: new InputDecoration.collapsed(hintText: "Описание"),
                  maxLines: null,
                  focusNode: _focusNode,
                  enabled: isTextFieldEnable,
                  onChanged: (value) {
                    CheckListModel.checkLists[arg['index']].description = value;
                  },
                  onSubmitted: (value) async {
                    FocusScope.of(context).unfocus();
                    _textFieldController.text = CheckListModel.checkLists[arg['index']].description;
                    CheckListModel checkList = CheckListModel.checkLists[arg['index']];
                    await checkList.writeToFile(checkList.toJson());
                  },
                  textInputAction: TextInputAction.done,
                )),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  IconButton(
                    onPressed: () {
                      setState(() {
                        isTextFieldEnable = true;
                        print(isTextFieldEnable);
                      });
                    },
                    icon: Icon(Icons.edit),
                  ),
                ],
              )
            ],
          ),

CodePudding user response:

Please refer to below code

We can control whether a TextField is disabled or not by setting its readOnly property to true or false,

TextEditingController _textFieldController = TextEditingController();
    bool isTextFieldEnable = true;
    FocusNode _focusNode = FocusNode();
  Row(
            children: [
              Container(
                width: 305,
                margin: EdgeInsets.only(left: 34),
                height: 250,
                child: SingleChildScrollView(
                    child: TextField(
                  controller: _textFieldController,
                  decoration: new InputDecoration.collapsed(hintText: "Описание"),
                  maxLines: null,
                  focusNode: _focusNode,
                  readOnly: isTextFieldEnable, /* change this if read  only is set to false you can enter in text field or if it is set to true you cannot enter any text in TextField. */
                  onChanged: (value) {
                    CheckListModel.checkLists[arg['index']].description = value;
                  },
                  onSubmitted: (value) async {
                    FocusScope.of(context).unfocus();
                    _textFieldController.text = CheckListModel.checkLists[arg['index']].description;
                    CheckListModel checkList = CheckListModel.checkLists[arg['index']];
                    await checkList.writeToFile(checkList.toJson());
                  },
                  textInputAction: TextInputAction.done,
                )),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  IconButton(
                    onPressed: () {
                      setState(() {
                        isTextFieldEnable = false;
                        print(isTextFieldEnable);
                      });
                    },
                    icon: Icon(Icons.edit),
                  ),
                ],
              )
            ],
          ),


  • Related