无法更改AutoCompleteTextField的建议

时间:2019-04-28 13:40:00

标签: flutter

问题描述-

我正在编写一个搜索窗口小部件,它是一个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上,并显示建议。

0 个答案:

没有答案