Open gidyon opened 4 years ago
@gidyon i got this to work using key and some digging through the code on tap shows "Hint1","Hint2" as default suggestions, when the user starts typing then data1, 2, 3 are shown based on the query
var _auto = GlobalKey<AutoCompleteTextFieldState<String>>();
@override
void initState() {
textEditingController.addListener(() {
if (textEditingController.text.isNotEmpty) {
suggestionList = ["data1","data2","data3","data4"];
_auto.currentState.updateSuggestions(suggestionList);
} else {
suggestionList = ["Hint1", "Hint2"];
_auto.currentState.updateSuggestions(suggestionList);
_auto.currentState.filteredSuggestions = suggestionList;
}
});
super.initState();
}
//Build method
AutoCompleteTextField(
controller: textEditingController,
focusNode: focusNode,
submitOnSuggestionTap: true,
style: TextStyle(fontSize: 13),
decoration: InputDecoration(
hintText: 'Enter Stock',
prefixIcon: Icon(
Icons.search,
color: Colors.grey,
),
contentPadding: EdgeInsets.only(top: 10),
hintStyle: TextStyle(fontSize: 13),
border: UnderlineInputBorder(borderSide: BorderSide.none),
focusedBorder: UnderlineInputBorder(borderSide: BorderSide.none),
enabledBorder: UnderlineInputBorder(borderSide: BorderSide.none)),
textCapitalization: TextCapitalization.characters,
itemSorter: (a, b) => a.compareTo(b),
suggestions: underlyers,
itemBuilder: (context, String suggestion) => ListTile(
title: Text(
suggestion,
style: TextStyle(fontSize: 12),
),
),
itemFilter: (String suggestion, query) =>
suggestion.toUpperCase()?.contains(query.trim()?.toUpperCase()),
textChanged: (data) {
if (data.isEmpty) {
suggestionList = ["Hint1", "Hint2"];
_auto.currentState.filteredSuggestions = suggestionList;
}
},
itemSubmitted: (data) {
},
key: _auto,
textSubmitted: (String data) {
},
),
++ VOTE !
I'm just curious to know how this got closed? It seems like it should be a default feature, but yet this issue got closed because somebody showed a hoop to jump through... Seems like there should be an extra parameter that could be passed to show suggestions on load or on focus.
Can we start showing suggestions immediately after focus? Currently suggestions will only appear if the user starts typing which is great but then having an option to display suggestions on focus would accomodate cases where the user is unsure of what to enter (act as hint).