TypeAheadField:如何处理 TypeAheadField 中的轻微拼写错误

问题描述 投票:0回答:1

这就是我实现

TypeAheadField
的方式,这与在其中输入的确切拼写配合得很好。

TypeAheadField(
      noItemsFoundBuilder: (context) => const SizedBox(
      height: 50,
      child: Center(child: Text("No user found!")),
      ),
      suggestionsBoxDecoration: SuggestionsBoxDecoration(
      color: Colors.deepPurple.shade50,
      elevation: 3.0,
      borderRadius: const BorderRadius.only(
      topLeft: Radius.circular(15),
      topRight: Radius.circular(15),
      bottomLeft: Radius.circular(15),
      bottomRight: Radius.circular(15))),
      textFieldConfiguration: TextFieldConfiguration(
      // Use inputFormatters to restrict input to alphabetic characters.
      inputFormatters: [
             FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z]')),
                            ],
             controller: _allLeaveViewModel.searchController,
             onChanged: (value) {
                   _allLeaveViewModel.isSearchTextNotEmpty.value =value.isNotEmpty;
                              //_allLeaveViewModel.updateList(value);
                              // if (value.isEmpty) {
                              //   Utils.removeFocusFromSearch(context);
                              // }
                            },
                            decoration: InputDecoration(
                              contentPadding: const EdgeInsets.fromLTRB(
                                  16.0, 12.0, 16.0, 12.0),
                              hintText: 'Filter by applicant...',
                              hintStyle: TextStyle(
                                  color: Colors.black.withOpacity(0.5),
                                  fontSize: 16),
                              filled: true,
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(70),
                                borderSide: BorderSide.none,
                              ),
                              suffixIcon: Obx(() {
                                return Visibility(
                                  visible: _allLeaveViewModel
                                      .isSearchTextNotEmpty.value,
                                  child: IconButton(
                                    icon: Icon(
                                      Icons.cancel_outlined,
                                      color: Colors.grey.shade700,
                                      size: 25,
                                    ),
                                    onPressed: () {
                                      _allLeaveViewModel.clearSearchAndList();
                                    },
                                  ),
                                );
                              }),
                            ),
                          ),
                          suggestionsCallback: (pattern) async {
                            return _allLeaveViewModel.getSuggestions(pattern);
                          },
                          minCharsForSuggestions: 2,
                          itemBuilder:
                              (BuildContext context, String suggestion) {
                            return Padding(
                              padding: const EdgeInsets.symmetric(vertical: 10),
                              child: Row(
                                children: [
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  Flexible(
                                      child: Text(
                                    suggestion,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: const TextStyle(fontSize: 16),
                                  ))
                                ],
                              ),
                            );
                          },
                          onSuggestionSelected: (suggestion) {
                     _allLeaveViewModel.isSearchTextNotEmpty.valuesuggestion.isNotEmpty;
                            _allLeaveViewModel.searchController.text = suggestion;
                            _allLeaveViewModel.updateList(suggestion);
                          },
                        )

但我无法找出如何处理其中输入的小拼写错误, 有人可以帮我吗?

flutter autocomplete typeahead
1个回答
0
投票

尝试编写一个自定义回调,它将跟踪您的实际输入和某些存储值之间的差异。我所说的“差异”是指Hamming DistanceLevenstein Distance。然后你将它传递给

suggestionCallback

© www.soinside.com 2019 - 2024. All rights reserved.