flutter - 在 TextFormField 上捕获点击事件

我正在尝试将 TextFormField 上的点击事件捕获到 flutter 的表单中。

我使用 GestureDetector 将 TextFormField 作为子级来执行此操作,但是单击它时没有任何触发:

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

如果我替换:

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

您对如何使 GestureDetector 与 TextFormField 一起工作有任何想法吗?也许使用 Controller ,但我尝试过但没有成功 提前致谢

最佳答案

只需使用 onTapTextFormField 方法:

TextFormField(
  onTap: () {
    print("I'm here!!!");
  }
)

https://stackoverflow.com/questions/47929865/

相关文章:

flutter - 如何在 Flutter 中添加虚线

flutter - BlocProvider 在继承的小部件中不可用

dart - Flutter中的弯曲形状背景标题

dart - 如何使用 Flutter 检测用户在我的屏幕上点击的位置?

flutter - 没有为类 'signInWithGoogle' 定义方法 'FirebaseAu

visual-studio-code - 如何在 Visual Studio Code 中使用 Fl

flutter - 如何完全处理我的 Stateful Widget?

android - 在无状态小部件中使用 TextFormField 在 flutter 中非常困难

file - 在 Flutter 中保存照片(尤其是到相机胶卷)

json - 将 JSON 映射到类对象