仅将焦点放在TextField中的IconButton上

时间:2019-08-12 08:58:48

标签: flutter dart

我有一个TextFormField用来输入密码,而IconButton可以切换obscureText。我要使按钮单击时,文本字段不会成为焦点。但是,事实并非如此。我想这与FocusNode有关。

ValueListenableBuilder<bool>(
  valueListenable: _obscureText,
  builder: (context, obscureText, child) => TextFormField(
    obscureText: obscureText,
    decoration: InputDecoration(
      suffixIcon: IconButton(
        icon: Icon(obscureText ? Icons.visibility_off : Icons.visibility),
        onPressed: () => _obscureText.value = !obscureText,
      ),
    ),
  ),
),

有人知道解决方案吗?

1 个答案:

答案 0 :(得分:0)

您需要的只是

"internetClient",
"internetClientServer",
"privateNetworkClientServer",
"enterpriseAuthentication",
"userAccountInformation",
"picturesLibrary",
"userDataTasks",
"backgroundMediaPlayback",
"contacts",
"phoneCall",
"phoneCallHistoryPublic",
"removableStorage",
"videosLibrary",
"musicLibrary",
"userNotificationListener",
"location",
"pointOfService",
"webcam",
"microphone",
"serialcommunication"

FocusNode.unfocus() 方法中的setState之后调用它。

输出:

enter image description here

完整解决方案:

onPressed()