TextField点击更改颤动

时间:2019-02-12 15:46:24

标签: dart flutter statefulwidget

TextField获得焦点后,将存储一个对象(如果已启用),该对象将发生更改,以使TextField立即被禁用。当上方的另一个TextField被聚焦时,也会发生这种情况。

TextField放在StatefulWidget内,Category对象包含另一个名为Goal的对象,该对象包含bool变量(如果启用或禁用)。此变量还用于启用或禁用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");
    }
  },
),

switch下方还有一个TextField,可通过设置Goal变量来启用或禁用enabled

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

我发现,点击TextField似乎可以用新的Goal作为false的默认值替换enabled对象

1 个答案:

答案 0 :(得分:0)

尝试改用FocusNodes

FocusNode textNode = FocusNode();

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

稍后,当用户与另一个小部件交互时,您想禁用此textField时,您可以调用:

textNode.unfocus() ;