flutter - 无法更改 AutoCompleteTextField 的建议
问题描述
问题描述 -
我正在编写一个搜索小部件,它是一个 AutoCompletTextField。它获取用户输入的 queryString 并从 Internet 获取相关建议,然后将这些建议附加到 AutoCompleteTextField。在从互联网上获取建议之前,一切都运行良好。当我调用小部件的 setState() 方法时,建议没有弹出。即使在调用 setState() 之后,这些建议也不会弹出。请帮忙。代码 -
`import 'package:flutter/material.dart';
import 'miscellaneous_widgets.dart';
import 'models/AppContextData.dart';
import 'package:autocomplete_textfield/autocomplete_textfield.dart';
import 'workers/MCAutoSuggestionsGetter.dart';`
class CompanySearchBox extends StatefulWidget {
@override
_CompanySearchBoxState createState() => _CompanySearchBoxState();
}
class _CompanySearchBoxState extends State<CompanySearchBox> {
GlobalKey<AutoCompleteTextFieldState<String>> key =
GlobalKey<AutoCompleteTextFieldState<String>>();
List<String> lstSuggestions = List<String>();
TextEditingController companySearchBoxController = TextEditingController();
@override
Widget build(BuildContext context) {
AutoCompleteTextField<String> myCompanySearchBox;
TextEditingController CompanyNameController = TextEditingController();
myCompanySearchBox = AutoCompleteTextField<String>(
key: key,
clearOnSubmit: false,
style: TextFieldStyle.get(),
decoration: InputDecoration(
labelText: "Search companies here.",
//border: OutlineInputBorder(),
suffixIcon: Icon(Icons.search),
),
submitOnSuggestionTap: true,
suggestions: lstSuggestions,
textChanged: (queryString){
if(queryString.length >= 3)
queryCompanyNames(queryString);
},
itemBuilder: (context,item){
return Center(
heightFactor: 1,
child: Column(
children: <Widget>[
ListTile(
contentPadding: EdgeInsets.all(2.0),
title: Text(
item,
style: TextRegularStyle.get()),
),
Divider(color: Colors.blueGrey,)
],)
);
},
itemFilter: (item,queryString ){
if(item.toLowerCase().startsWith(queryString.toLowerCase()))
return true;
},
itemSorter: (item1, item2){
return item1.toLowerCase().compareTo(item2.toLowerCase());
},
itemSubmitted: (SelectedItem){
//Not doing here anything as of now.
},
);
//myCompanySearchBox.textField.controller = CompanyNameController,
return myCompanySearchBox;
}
queryCompanyNames(String queryString) async
{
/*Calling the MCAutoSuggestionsGetter which fetches the suggestions from the internet. This step is working fine. We are getting a List<String> in "Suggestions" variable below. But when setState() is called, the previous "lstSuggestions" should be cleared and new "Suggestions" should be added. And these newly added suggestions should be displayed. But I am unable to achieve this.*/
MCAutoSuggestionsGetter.fetchSuggestions(queryString).then((Suggestions){
setState((){
lstSuggestions.clear();
lstSuggestions.addAll(Suggestions);
});
});
}
}
我的 pubspec.yaml -
name: dev1_stock_meter
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_core: ^0.2.5+1
firebase_auth: ^0.7.0
fluttertoast: ^3.0.4
autocomplete_textfield: ^1.6.4
html: ^0.13.3+3
http: ^0.12.0
date_format: ^1.0.6
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- images/logo.jpg
fonts:
- family: GoogleSans
fonts:
- asset: fonts/GoogleSans-Regular.ttf
weight: 300
- asset: fonts/GoogleSans-Bold.ttf
weight: 400
预期行为:
预期的行为是当用户键入长度超过 3 个字符的字符串时,应从 Internet 获取建议并将这些建议附加到 AutoCompletTextField 并显示建议。
解决方案
推荐阅读
- javascript - 如何在 $ionicplatform.ready 中引用 cordova 插件?
- routes - 如何在 TYPO3 CMS 9.5 LTS 中创建动态字符串路由增强器?
- flutter - 在颤动中获取当前位置时出错
- perl - 在perl中查找嵌套数组长度
- c - 如何输入与给定数量一样多的单词?
- flutter - 如何在颤动中验证交易收据
- node.js - 我正在使用 docusign-esign 节点 SDK,并且我想更改发件人的名称(截至目前,使用与帐户关联的名称进行 docusign)
- ios - Xcode 的 C99 中函数“ERR_load_crypto_strings”的隐式声明无效
- tomtom - API 密钥未附加到 ReverseGeocode 请求
- python - SQL(ite) 快速检索较大字符串的多个子字符串(基因组)