如何在屏幕上点击任何GestureDetector时隐藏键盘?

huangapple go评论54阅读模式
英文:

How to hide keyboard when clicked on any gestureDetector on screen?

问题

我有一个屏幕,上面有多个小部件。其中一些是可点击的。有一个textInput小部件,当点击它时会打开键盘。我希望无论何时点击它之外的任何地方都可以隐藏它。但是,如果我点击键盘之外的任何GestureDetector,它会处理具有打开键盘的操作。我只想首先简单地关闭它。

我尝试将整个屏幕包装在一个gestureDetector中,并在其onTap中使用focusNode.unfocus(),但它没有起作用。

英文:

I have a screen with multiple widgets. Some of them are clickable. There is one textInput widget that opens a keyboard when clicked on it. I want to hide it whenever clicked anywhere outside it. But if i click on any GestureDetector outside keyboard, then it handles that action with keyboard open. I want to simply close it first.

I tried wrapping my whole screen in a gestureDetector and use focusNode.unfocus() in its onTap, but it didn't work

答案1

得分: 1

GestureDetector(
    onTap: () {
        /* 隐藏键盘 */
        FocusManager.instance.primaryFocus?.unfocus();
    },
)
英文:

Wrap with following code:

GestureDetector(
             onTap: () {
              /* Hide Keyboard */
             FocusManager.instance.primaryFocus?.unfocus();
       },
)

答案2

得分: 0

I guess you can close the keyboard if already open before clicking on another GestureDetector widgets

Something like:

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final _textController = TextEditingController();
  bool _isKeyboardOpen = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // Close keyboard if it is open
        if (_isKeyboardOpen) {
          FocusScope.of(context).unfocus();
          _isKeyboardOpen = false;
        }
      },
      child: Scaffold(
        appBar: AppBar(title: Text('My Widget')),
        body: Column(
          children: [
            TextField(
              controller: _textController,
              onTap: () {
                // Set keyboard open flag to true when text input is tapped
                _isKeyboardOpen = true;
              },
            ),
            GestureDetector(
              onTap: () {
                // Handle tap on this widget
                print('Tapped on GestureDetector');
              },
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
                child: Center(child: Text('Clickable widget')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

(Note: The code is provided without translation, as per your request.)

英文:

I guess you can close the keyboard if already open before clicking on another GestureDetector widgets

Something like:

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() =&gt; _MyWidgetState();
}

class _MyWidgetState extends State&lt;MyWidget&gt; {
  final _textController = TextEditingController();
  bool _isKeyboardOpen = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // Close keyboard if it is open
        if (_isKeyboardOpen) {
          FocusScope.of(context).unfocus();
          _isKeyboardOpen = false;
        }
      },
      child: Scaffold(
        appBar: AppBar(title: Text(&#39;My Widget&#39;)),
        body: Column(
          children: [
            TextField(
              controller: _textController,
              onTap: () {
                // Set keyboard open flag to true when text input is tapped
                _isKeyboardOpen = true;
              },
            ),
            GestureDetector(
              onTap: () {
                // Handle tap on this widget
                print(&#39;Tapped on GestureDetector&#39;);
              },
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
                child: Center(child: Text(&#39;Clickable widget&#39;)),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年3月1日 15:30:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定