Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch tap event on TextFormField

I am trying to catch the tap event on TextFormField into a flutter Form.

I use a GestureDetector to do that with the TextFormField as child but nothing is firing when a click on it :

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      appBar: new AppBar(title: const Text('Recherche de sorties')),
      body: new DropdownButtonHideUnderline(
        child: new Form(
          key: _formKey,
          autovalidate: _autovalidate,
          child: new ListView(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              children: <Widget>[
                new DatePicker(
                  labelText: 'Date',
                  selectedDate: widget.request.dateDebut,
                  initialDate: widget.request.dateDebut,
                  firstDate: new DateTime.now().add(new Duration(days: -1)),
                  lastDate: new DateTime.now().add(new Duration(days: 365 * 4)),
                  selectDate: (DateTime value) {
                    setState(() {
                      widget.request.dateDebut = value;
                    });
                  },
                  datePickerMode: DatePickerMode.day,
                  icon: const Icon(Icons.date_range),
                ),
                new InputDecorator(
                  decoration: const InputDecoration(
                    labelText: 'Rayon',
                    hintText: '-- Choisissez un rayon --',
                    icon: const Icon(Icons.settings_backup_restore),
                  ),
                  isEmpty: widget.request.rayon == null,
                  child: new DropdownButton<String>(
                    value: widget.request.rayon.toString(),
                    isDense: true,
                    onChanged: (String newValue) {
                      setState(() {
                        widget.request.rayon = int.parse(newValue);
                      });
                    },
                    items: _rayons.keys.map((int key) {
                      return new DropdownMenuItem<String>(
                        value: key.toString(),
                        child: new Text(_rayons[key]),
                      );
                    }).toList(),
                  ),
                ),

                new GestureDetector(
                  onTap: () async {
                    print("Container clicked");

                    Prediction p = await showGooglePlacesAutocomplete(
                        context: context,
                        apiKey: Consts.googlePlacesApiKey,
                        mode: Mode.fullscreen,
                        language: "fr",
                        components: [new Component(Component.country, "fr")]);

                    if (p != null) {
                      (_scaffoldKey.currentState).showSnackBar(
                          new SnackBar(content: new Text(p.description)));
                    }
                  },
                  child: new TextFormField(
                    // controller: controller,
                    decoration: const InputDecoration(
                      icon: const Icon(Icons.room),
                      hintText: 'Où êtes vous ?',
                      labelText: 'Localisation',
                    ),
                  ),
                ),

                new Container(
                    padding: const EdgeInsets.all(20.0),
                    alignment: Alignment.center,
                    child: new Align(
                      alignment: const Alignment(0.0, -0.2),
                      child: new ButtonBar(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          new RaisedButton(
                            child: const Text('ANNULER'),
                            onPressed: _fermerCritereRecherche,
                          ),
                          new RaisedButton(
                            child: const Text('VALIDER'),
                            onPressed: _valider,
                          ),
                        ],
                      ),
                    )),
              ]),
        ),
      ),
    );
  }

If i replace :

      new GestureDetector(
          onTap: () async {
            print("Container clicked");

            Prediction p = await showGooglePlacesAutocomplete(
                context: context,
                apiKey: Consts.googlePlacesApiKey,
                mode: Mode.fullscreen,
                language: "fr",
                components: [new Component(Component.country, "fr")]);

            if (p != null) {
              (_scaffoldKey.currentState).showSnackBar(
                  new SnackBar(content: new Text(p.description)));
            }
          },
          child: new TextFormField(
            // controller: controller,
            decoration: const InputDecoration(
              icon: const Icon(Icons.room),
              hintText: 'Où êtes vous ?',
              labelText: 'Localisation',
            ),
          ),
        ),

By a simple Container it is working :

   new GestureDetector(
          onTap: () async {
            print("Container clicked");

            Prediction p = await showGooglePlacesAutocomplete(
                context: context,
                apiKey: Consts.googlePlacesApiKey,
                mode: Mode.fullscreen,
                language: "fr",
                components: [new Component(Component.country, "fr")]);

            if (p != null) {
              (_scaffoldKey.currentState).showSnackBar(
                  new SnackBar(content: new Text(p.description)));
            }
          },
          child: new Container(
             width: 80.0,
             height: 80.0,
             margin: new EdgeInsets.all(10.0),
             color: Colors.black),
        ),

Do you have any ideas how to make GestureDetector work with TextFormField ? Maybe with a controller but i have tried without any success Thanks in advance

like image 436
toregua Avatar asked Dec 21 '17 17:12

toregua


Video Answer


1 Answers

Simply use onTap Method of TextFormField:

TextFormField(
  onTap: () {
    print("I'm here!!!");
  }
)
like image 77
badelectron77 Avatar answered Nov 06 '22 09:11

badelectron77