0

I do the following to change the value of the input field. Regarding documentation this should work but it don't. Purpose is to open a google location search if location input is tapped and write the result value back to the text field. The following code is a simple version to show my problem:

            TextFormField(
             key: locationKey,
                decoration:
                InputDecoration(labelText: 'Location'),
             // onTap: openLocationSearch,
              onTap: () {
                locationKey.currentState.didChange('test');
             },
          ),
MarcS82
  • 2,065
  • 7
  • 29
  • 46

2 Answers2

1

This will do what you are looking for:

class TapTextFormIssue extends StatefulWidget {
  @override
  _TapTextFormIssueState createState() => _TapTextFormIssueState();
}

class _TapTextFormIssueState extends State<TapTextFormIssue> {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextFormField(
        controller: _textEditingController,
        onTap: () {
          setState(() {
            _textEditingController.text = 'test';
          });
        },
      ),
    );
  }
}
J. S.
  • 8,905
  • 2
  • 34
  • 44
1

Add a TextEditingController to your TextFormField like this

TextEditingController myTEC = TextEditingController('Location');
TextFormField(
  key: locationKey,
  controller: myTEC,
  onTap: () {
    locationKey.currentState.didChange('test');
  },
),
NqbraL
  • 553
  • 4
  • 8