【问题标题】:Flutter TypeaheadFlutter Typeahead
【发布时间】:2021-02-11 11:08:26
【问题描述】:

使用 typeahead 包时,我没有得到想要的结果。

当前不良行为:

  • 当我点击文本字段时,在我开始输入之前,我会立即看到所有建议的列表(超过 26,000 条)

  • 当我开始输入时,建议列表不会调整(例如,如果我输入“a”,则会显示完整的建议列表,并且该列表不会过滤为仅显示以“a”开头的建议

期望的结果:

  • 我只是希望该功能根据我输入的内容向我显示建议 - 我的代码实现错误我确定,非常感谢任何帮助!

我的相关代码:

import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';

import '../providers/analysis_route_provider.dart';

class AutoCompleteTextfieldTwo extends StatefulWidget {
  @override
  _AutoCompleteTextfieldTwoState createState() =>
      _AutoCompleteTextfieldTwoState();
}

class _AutoCompleteTextfieldTwoState extends State<AutoCompleteTextfieldTwo> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TypeAheadField(
      hideOnEmpty: true,
      textFieldConfiguration: TextFieldConfiguration(
        style: TextStyle(
          color: Colors.white,
        ),
        autofocus: false,
        controller: this._controller,
        keyboardType: TextInputType.text,
        enabled: true,
        focusNode: FocusNode(),
        decoration: InputDecoration(
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(20),
            borderSide: BorderSide(
              width: 2,
              color: Colors.blue,
            ),
          ),
          hintText: 'Type in company name or ticker symbol',
          hintStyle: TextStyle(
            color: Colors.grey,
          ),
        ),
      ),
      getImmediateSuggestions: true,
      hideOnError: true,
      suggestionsCallback: (pattern) async {
        return await AnalysisRouteProvider.getCompaniesForTextfield2(pattern);
      },
      itemBuilder: (context, itemData) {
        return ListTile(
          title: Text(itemData['name'].toString()),
          subtitle: Text(itemData['symbol'].toString()),
        );
      },
      onSuggestionSelected: (suggestion) {
        print('selected');

        FocusNode().unfocus();
        this._controller.text = suggestion['name'].toString();
        _controller.clear();
      },
    );
  }
}

Http请求:

 static Future getCompaniesForTextfield2(String query) async {
    var url = *url with api key here*;
    http.Response response = await http.get(url, headers: {
      'Content-Type': 'application/json',
    });
    var jsonData = json.decode(response.body);

    return jsonData;
  }

JSON sn-p(从 API 实际返回的对象超过 26,000 个):

[ {
  "symbol" : "SPY",
  "name" : "SPDR S&P 500",
  "price" : 326.7,
  "exchange" : "NYSE Arca"
}, {
  "symbol" : "CMCSA",
  "name" : "Comcast Corp",
  "price" : 41.98,
  "exchange" : "Nasdaq Global Select"
}, {
  "symbol" : "KMI",
  "name" : "Kinder Morgan Inc",
  "price" : 11.83,
  "exchange" : "New York Stock Exchange"
}]

我也确信这些信息是相关的。目前,当我在调试并点击 typeahead 文本字段时,我会在调试控制台中看到以下内容:

W/IInputConnectionWrapper(13704): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(13704): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(13704): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(13704): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(13704): endBatchEdit on inactive InputConnection

【问题讨论】:

  • 您在 suggestionsCallback 方法中根本没有使用 query 参数
  • @pskink 是的,我需要代码方面的帮助
  • 所以你必须以某种方式使用query - 这是你在文本框中输入的String
  • @pskink 有一个代码示例?我得到的概念,是困扰我的代码

标签: flutter dart texteditingcontroller flutter-typeahead


【解决方案1】:

我认为你应该在false 上设置getImadiateSugetions 以禁用在点击时显示建议列表。

关于第二个问题,你没有得到建议,我认为问题出在 json 中, 您正在使用 json.decode,它将 [] 中的任何 json 解码到列表中,如果它在 {} 中,则在 map 中,所以我认为您应该做的是:

suggestionsCallback: (pattern) async {
        var list = await AnalysisRouteProvider.getCompaniesForTextfield2(pattern);
        return list[0];
      },

【讨论】:

  • 感谢您愿意帮助我!我已经实现了你建议的代码,但它仍然没有给我我想要的行为。虽然我最初在文本字段中输入时没有立即得到建议;当我在文本字段中输入时,我也没有得到任何建议——即当我输入“a”时获取以“a”开头的公司,依此类推
猜你喜欢
  • 2020-02-20
  • 2020-08-16
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
相关资源
最近更新 更多