Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the text of the field in flutter_typehead TypeAheadField widget?

I'm new to Dart and Flutter and I'm trying to use this module https://github.com/AbdulRahmanAlHamali/flutter_typeahead to make a text field with autocomplete/"type ahead" functionality.

In the example they give, when the user selects one of the suggestions, they route the user to another view, but I don't want to do that. I'd just like to set the value of the input text to whatever the user selected.

Here's my code:

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            TypeAheadField(
              textFieldConfiguration: TextFieldConfiguration(
                autofocus: true,
                style: DefaultTextStyle.of(context).style.copyWith(
                  fontStyle: FontStyle.italic
                ),
                decoration: InputDecoration(
                  border: OutlineInputBorder()
                )
              ),
              suggestionsCallback: (pattern) async {
                Completer<List<String>> completer = new Completer();
                completer.complete(<String>["cobalt", "copper"]);
                return completer.future;
              },
              itemBuilder: (context, suggestion){
                return ListTile(
                  title: Text(suggestion)
                );
              },
              onSuggestionSelected: (suggestion) {
              }
            )
          ],
        ),
      )
    );
  }
}

I have no clue what to put inside of the onSuggestionSelectedparameter function to achieve what I described.

like image 225
John Smith Optional Avatar asked Jul 09 '19 22:07

John Smith Optional


People also ask

How do you AutoFill TextField in flutter?

Also, as you already mentioned, you need to configure an AutoFill service in the system settings. Working sample code: @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text("Login"), ), body: AutofillGroup( child: Column( children: [ TextField( autofillHints: [AutofillHints.

How do you use TextField in flutter?

In Flutter, this can be done using TextEditingController . First, create a TextEditingController and set it as a controller property of your TextField widget. In this example, I have added an extra Button and Text widget which will show the added text when you click the “Show Text” button.


1 Answers

Ok, I found the solution which was actually on the same github link I provided, but since the example was not about the exact same component (TypeAheadFormField instead of TypeAheadField) and the example was only a piece of code that was lacking context, I had to look at the source to understand.

Here's how to proceed. This actually works both for TypeAheadFormField and TypeAheadField. You have to create a TextEditingController that you pass to the constructor of the TypeAheadField widget. Then you set the text property of that TextEditingController in your onSuggestionSelected callback method. The TypeAheadField widget will use that value to redraw itself, I guess that's how it works.

Here's the code that works:

class _MyHomePageState extends State<MyHomePage> {

  final TextEditingController _typeAheadController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            TypeAheadField(
              textFieldConfiguration: TextFieldConfiguration(
                autofocus: true,
                style: DefaultTextStyle.of(context).style.copyWith(
                  fontStyle: FontStyle.italic
                ),
                decoration: InputDecoration(
                  border: OutlineInputBorder()
                ),
                controller: this._typeAheadController
              ),
              suggestionsCallback: (pattern) async {
                Completer<List<String>> completer = new Completer();
                completer.complete(<String>["cobalt", "copper"]);
                return completer.future;
              },
              itemBuilder: (context, suggestion){
                return ListTile(
                  title: Text(suggestion)
                );
              },
              onSuggestionSelected: (suggestion) {
                this._typeAheadController.text = suggestion;
              }
            )
          ],
        ),
      )
    );
  }
}
like image 71
John Smith Optional Avatar answered Nov 08 '22 07:11

John Smith Optional