英文:
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() => _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')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论