Flutter Web TypAhead 在点击下拉项时不起作用

Flutter Web TypAhead does not work when dropdown item is clicked

提问人:beh aaron 提问时间:1/17/2023 最后编辑:beh aaron 更新时间:1/17/2023 访问量:197

问:

我正在开发一个 Flutter Web 应用程序,并创建了一个 TypeAheadFormField,并设置了一个 onSuggestionSelected 函数以将项目添加到列表中,这仅在我使用箭头键并按“Enter”时有效,但当我使用鼠标左键单击它时不起作用。我现在还注意到,pub.dev 上的预输入包没有将 Web 列为受支持的平台,这可能是问题的原因,但仍想寻求第二意见。

这是我正在处理的片段以供参考:

TypeAheadFormField(
  textFieldConfiguration:
      TextFieldConfiguration(
          decoration:
              const InputDecoration(
                  border:
                      OutlineInputBorder(
                    borderSide: BorderSide(
                        color:
                            projectsFieldOutline),
                  ),
                  prefixIcon:
                      Icon(Icons.people)),
          controller: collabController),
  suggestionsCallback: (pattern) async {
    List<String> matches =
        collabSuggestions
            .toSet()
            .toList();
    matches.retainWhere((element) =>
        element.toLowerCase().startsWith(
            pattern.toLowerCase()));
    return matches;
  },
  itemBuilder:
      (context, String suggestion) {
    return ListTile(
        title: Text(suggestion));
  },
  onSuggestionSelected:
      (String suggestion) {
    setState(() {
      collabController.text = suggestion;

      print(suggestion);
      collabList
          .add(suggestion.toString());
      print(collabList);
    });
  },
)
flutter-web 类型提前

评论


答:

0赞 beh aaron 1/17/2023 #1

对于可能遇到相同问题的任何人来说,我找到了一种使用 Listener 的解决方法:

itemBuilder:
    (context, String suggestion) {
  return basic.Listener(
    child: ListTile(
      title: Text(suggestion),
    ),
    onPointerDown: (_) {
      // place logic from onSuggestionSelected here

    },
  );
},