flutter-autocomplete-textfield icon indicating copy to clipboard operation
flutter-autocomplete-textfield copied to clipboard

Suggestions on focus

Open gidyon opened this issue 4 years ago • 3 comments

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).

gidyon avatar Apr 12 '20 14:04 gidyon

@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) {
                                  },
                                ),

rajeshzmoke avatar Sep 24 '20 07:09 rajeshzmoke

++ VOTE !

maciey avatar Nov 18 '20 18:11 maciey

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.

Buddyboy-git avatar Feb 14 '24 15:02 Buddyboy-git