如何在Flutter中隐藏表情键盘?

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

How to hide Emoji keyboard in flutter?

问题

我想在点击键盘外部时隐藏表情符号键盘。但它没有隐藏。

我尝试了这段代码。它隐藏了文本键盘,但不隐藏表情符号键盘。

GestureDetector(
        onTap: FocusScope.of(context).unfocus,

以及用于表情符号键盘的部分:

Offstage(
                offstage: !_showEmoji,
                child: SizedBox(
                  height: 300,
                  child: EmojiPicker(
                    textEditingController: _textController,
                    config: Config(
                      bgColor: Colors.grey.shade700,
                      columns: 8,
                      emojiSizeMax: 32 * (Platform.isIOS ? 1.30 : 1.0),
                    ),
                  ),
                ),
              ),
英文:

I want to hide the emoji keyboard when tapped outside of it. But it doesn't hide. (https://i.stack.imgur.com/XGqjV.png)

I have tried this code. It hides the text keyboard but not the emoji keyboard.

GestureDetector(
        onTap: FocusScope.of(context).unfocus,

and for emoji keyboard

Offstage(
                offstage: !_showEmoji,
                child: SizedBox(
                  height: 300,
                  child: EmojiPicker(
                    textEditingController: _textController,
                    config: Config(
                      bgColor: Colors.grey.shade700,
                      columns: 8,
                      emojiSizeMax: 32 * (Platform.isIOS ? 1.30 : 1.0),
                    ),
                  ),
                ),
              ),

答案1

得分: 0

以下是您要翻译的代码部分:

这个可以有一个绕过的方法。例如,您可以创建一个布尔值 `isShowingEmoji = false`,然后在打开 `EmojiPicker` 时将其设置为 true,然后在您的 `GestureDetector` 中,您可以使用以下代码来取消焦点。

onTap: () {
    if (isShowingEmoji) {
        Navigator.pop(context);
    } else {
        FocusScope.of(context).unfocus();
    }
}

注意:这只是一个绕过方法,还有更好的方法可以实现同样的功能。

英文:

There can be a round about to this. You can make a bool for example isShowingEmoji = false, then you can set it to true while opening EmojiPicker, then in your GestureDetector you can use this code to unfocus it.

onTap:() {
    if(isShowingEmoji){
      Navigator.pop(context);
  } else {
  FocusScope.of(context).unfocus(),
      }
    }

Note: there can be a good approach to this, this is just a round about.

答案2

得分: -1

抱歉,它是错误的。

你是否在模态窗口中使用了EmojiPicker小部件,就像showModalBottomSheet一样?如果没有,我建议使用模态窗口。

我认为,如果你在模态窗口中使用EmojiPicker小部件,那么GestureDetector的onTap将起作用。

例如,...

  ...
  TextEditingController _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
      onTap: FocusScope.of(context).unfocus,
      child: Center(
        child: InkWell(
          onTap: () => showModalBottomSheet(
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.vertical(top: Radius.circular(30))),
                context: context,
                builder: (context) {
                  return EmojiPicker(
                    textEditingController: _textController,
                    config: Config(
                      bgColor: Colors.grey.shade700,
                      columns: 8,
                      // emojiSizeMax: 32 * (Platform.isIOS ? 1.30 : 1.0),
                    ),
                  );
                },
              ),
          child: Text('Test')),
    ),
  ),
);

}
}

英文:

oops, sorry it was wrong.

did you use EmojiPicker widget on the modal window like showModalBottomSheet? If not, I recommend using a modal window.

I think if you use EmojiPicker widget using a modal window, then GestureDetector onTap will work.

For example,

  ...
  TextEditingController _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
      onTap: FocusScope.of(context).unfocus,
      child: Center(
        child: InkWell(
          onTap: () => showModalBottomSheet(
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.vertical(top: Radius.circular(30))),
                context: context,
                builder: (context) {
                  return EmojiPicker(
                    textEditingController: _textController,
                    config: Config(
                      bgColor: Colors.grey.shade700,
                      columns: 8,
                      // emojiSizeMax: 32 * (Platform.isIOS ? 1.30 : 1.0),
                    ),
                  );
                },
              ),
          child: Text('Test')),
    ),
  ),
);

}
}

huangapple
  • 本文由 发表于 2023年2月24日 15:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553548.html
匿名

发表评论

匿名网友

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

确定