0

As soon as a TextField gets focused, an object which stores if it's enabled changes so that the TextField gets disabled immediately. This also happens when another TextField above gets focused.

The TextField is placed inside a StatefulWidget and a Category object contains another object called Goal which contains a bool variable if it's enabled or disabled. This variable is also used to enable or disable the TextField.

TextField(
  controller: _goalAmountController,
  enabled: widget.category.goal.enabled,
  decoration: InputDecoration(
    labelText: "Goal Amount",
    border: OutlineInputBorder(),
  ),
  onChanged: (value) {
    try {
      widget.category.goal.amount = double.parse(value);
    } on Exception {
      //TODO display error message
      print("Invalid Goal-Amount");
    }
  },
),

There's also a switch below the TextField to enable or disable the Goal by setting it's enabled variable.

        SwitchListTile(
          value: widget.category.goal.enabled,
          title: Text("Enable Goal"),
          onChanged: (value) {
            setState(
              () {
                widget.category.goal.enabled = value;
              },
            );
          },
        ),

I found out that it seems as if the click on a TextField would replace the Goal object with a new one which has false as the default value for enabled.

eli2003
  • 400
  • 5
  • 13

1 Answers1

0

Try to use FocusNodes instead :

FocusNode textNode = FocusNode();

TextField(
  focusNode: textNode,
  controller: _goalAmountController,
  enabled: widget.category.goal.enabled,
  decoration: InputDecoration(
    labelText: "Goal Amount",
  border: OutlineInputBorder(),
),

later when you want to disable this textField when the user interact with another widget you can call:

textNode.unfocus() ;
Mazin Ibrahim
  • 7,433
  • 2
  • 33
  • 40
  • This doesn't work either. And I can't call `textNode.unfocus() ;` because I don*t know when the user interacts with another Widget – eli2003 Feb 12 '19 at 17:19
  • you can wrap the whole form inside an `InkWell`, this worked for me when creating registration forms. On its `onTap` method call `textNode.unfocus()` – Mazin Ibrahim Feb 12 '19 at 17:21