【问题标题】:Flutter GestureDetector onTap not workingFlutter GestureDetector onTap 不工作
【发布时间】:2020-06-22 03:22:21
【问题描述】:

当用户在外面点击时,尝试使用 GestureDetector 使 TextFormField 失焦,但我无法让它工作。 onTap 从不触发。

class EditScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new GestureDetector(
        onTap: () {
          print('this does not fire, why???????????');

          // this is my attempt to unfocus textformfield when click away
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              TextFormField(
                maxLines: null,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 把它放在Stack。有几个示例可以说明您尝试围绕 SO 做的事情。
  • 尝试在堆栈的背景上定义一种颜色,以确保您触摸它。它可能没有按预期调整大小。

标签: flutter flutter-layout


【解决方案1】:

尝试用 Gesture Detector 和 onTap 函数包裹你的 Scaffold:

      onTap: () {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
    }

所以每次点击脚手架都会触发

【讨论】:

  • 谢谢这个作品,你如何让它只有在脚手架体内敲击时才能工作?
  • 因为您将 Scaffold 包裹在 Gesture Detector 中。这就像如果您将 Container 包裹在 Gesture Detector 中,On Tap 功能只有在您点击 Container 时才会起作用
猜你喜欢
  • 2018-10-07
  • 1970-01-01
  • 2021-11-23
  • 2021-04-24
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 2023-01-25
  • 2021-11-21
相关资源
最近更新 更多