1

I think this is probably not too complicated, but I am new to coding and don't understand this, nor could I find an explanation online that I could understand.

In my app my AppBar includes both a title and a bottom, the title has been just text, and the bottom was a TextFormField users used to search through my database.

Recently I updated the title to a DropdownButton, and I wanted to use what the user selected there to change the the search parameters in my TextFormField (so that it would search different columns in my database).

I built the DropdownButton in a separate document, and then inserted it into title. Finding how to use it has been more tricky.

I built some sort of callback, which worked to print in my console, but I couldn't figure out how to pass the information into my TextFormField.

class HomePage extends StatelessWidget {
  HomePage({Key key, this.title}) : super(key: key);
  _handleValueReturned(value) {
    if (value == 1) {print('This works');} if (value == 2) {print('This works not');} else {return null;}
  }

...

appBar: AppBar(
      title: DropDownButton(
          valueReturned: _handleValueReturned,
      ),

      bottom:...

...

Do I have to use some sort of provider or "of" or packages like Eventifier or ChangeNotifier? I couldn't figure out how all these things worked.

Bill Watts
  • 21
  • 4
  • It sounds like you need to either use a state management strategy like setState or Bloc to change things according to what option the user selects in the drop-down. – Kris Aug 25 '20 at 06:38

1 Answers1

0

According to my understanding, you are trying to set the selected value from the DropDownButton in the TextFormField, if that is what you are trying to do then create a TextEditingController and add it to the TextFormField then set its text property inside your callback which is called _handleValueReturned, but you have to rebuild the widget so the TextFormField is rebuilt with the new value, of course, you have to convert your widget into StatefulWidget to do that.

Creating and initializing the TextEditingController:

final _controller = TextEditingController();

Attatching the controller to the TextFromField:

TextFormField(
  controller: _controller,
  ...
);

Changing the value of the controller inside _handleValueReturned callback:

 _handleValueReturned(value) {
    ...
    setState((){
      _controller.text = value; // It can be any String according to your scenario.
    });
  }

Side Note: When you are dealing with a simple text input such as one input field then you do not have to use TextFormField and you can simply replace it with TextField.

I hope I got your problem correctly and I hope it got fixed.

Moaz El-sawaf
  • 2,306
  • 1
  • 16
  • 33