【问题标题】:Flutter hide emoji keyboard when system keyboard shows系统键盘显示时颤动隐藏表情符号键盘
【发布时间】:2022-06-24 20:51:40
【问题描述】:

我试图在系统键盘显示时隐藏表情符号键盘,反之亦然。我目前拥有的是当系统键盘打开然后我点击表情符号图标,表情符号键盘显示在系统键盘顶部,如下所示:

我正在使用emoji_picker_flutter: ^1.1.2 包,这里是代码:

              Offstage(
                offstage: !emojiShowing,
                child: Padding(
                  padding: const EdgeInsets.all(10),
                  child: SizedBox(
                    height: 250,
                    child: EmojiPicker(
                        onEmojiSelected: (Category category, Emoji emoji) {
                          _onEmojiSelected(emoji);
                        },
                        onBackspacePressed: _onBackspacePressed,
                        config: Config(
                            columns: 8,
                            // Issue: https://github.com/flutter/flutter/issues/28894
                            emojiSizeMax: 32 * (Platform.isIOS ? 1.30 : 1.0),
                            verticalSpacing: 0,
                            horizontalSpacing: 0,
                            initCategory: Category.SMILEYS,
                            bgColor: Theme.of(context).scaffoldBackgroundColor,
                            indicatorColor: Colors.blue,
                            iconColor: Colors.grey,
                            iconColorSelected: Colors.blue,
                            progressIndicatorColor: Colors.blue,
                            backspaceColor: Colors.blue,
                            skinToneDialogBgColor: Colors.white,
                            skinToneIndicatorColor: Colors.grey,
                            enableSkinTones: true,
                            showRecentsTab: true,
                            recentsLimit: 28,
                            noRecentsText: 'No Recents',
                            noRecentsStyle: const TextStyle(
                                fontSize: 20, color: Colors.black26),
                            tabIndicatorAnimDuration: kTabScrollDuration,
                            categoryIcons: const CategoryIcons(),
                            buttonMode: ButtonMode.MATERIAL)),
                  ),
                ),
              ),

有什么解决办法吗?

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    添加这个:

    bool emojiShowing = false;
    var chatMessageController = TextEditingController();
    
    _onEmojiSelected(Emoji emoji) {
    chatMessageController
      ..text += emoji.emoji
      ..selection = TextSelection.fromPosition(
          TextPosition(offset: chatMessageController.text.length));
    setState(() {
      _send = true;
    });}
    
    
      _onBackspacePressed() {
    chatMessageController
      ..text = chatMessageController.text.characters.skipLast(1).toString()
      ..selection = TextSelection.fromPosition(
          TextPosition(offset: chatMessageController.text.length));}
    

    而在你的身体里,你需要这样做:

    body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Stack(
          children: [
            ChatStream(widget.chatRoomId),
            Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Container(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                    ),
                    child: Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(right: 15, left: 15, top: 0,bottom: 2),
                          height: 60,
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(35.0),
                                    boxShadow: [
                                      BoxShadow(
                                          offset: Offset(0, 3),
                                          blurRadius: 5,
                                          color: Colors.grey)
                                    ],
                                  ),
                                  child: Row(
                                    children: <Widget>[
                                      IconButton(
                                        onPressed: () {
                                          setState(() {
                                            emojiShowing = !emojiShowing;
                                          });
                                          if (emojiShowing != false) {
                                            FocusScope.of(context).unfocus();
                                          }
                                        },
                                        icon: const Icon(
                                          Icons.face,
                                        ),
                                      ),
                                      //IconButton(
                                      //    icon: Icon(Icons.face), onPressed: () {}),
                                      Expanded(
                                        child: TextField(
                                          keyboardType: TextInputType.multiline,
                                          maxLines: null,
                                          controller: chatMessageController,
                                          textCapitalization: TextCapitalization.sentences,
                                          decoration: InputDecoration.collapsed(
                                            hintText: 'Envoyer un message...',
                                          ),
                                          onTap: () {
                                            if (emojiShowing != false) {
                                              setState(() {
                                                emojiShowing = !emojiShowing;
                                              });
                                            }
                                          },
                                          onChanged: (value){
                                            if(value.isNotEmpty){
                                              setState(() {
                                                _send = true;
                                              });
                                            }else{
                                              setState(() {
                                                _send = false;
                                              });
                                            }
                                          },
                                          onSubmitted: (value){
                                            //you can send message by pressing enter
                                            if(value.length>0){
                                              sendMessage();
                                            }
                                          },
                                        ),
                                      ),
                                      IconButton(
                                        icon: Icon(Icons.photo_camera),
                                        onPressed: () {},
                                      ),
                                      IconButton(
                                        icon: Icon(Icons.attach_file),
                                        onPressed: () {},
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              _send == true
                                  ? Row(
                                    children: [
                                      SizedBox(width: 15),
                                      Container(
                                padding: const EdgeInsets.all(15.0),
                                decoration: BoxDecoration(
                                        color: Theme.of(context).primaryColor, shape: BoxShape.circle),
                                child: InkWell(
                                      child: Icon(
                                        Icons.send,
                                        color: Colors.white,
                                      ),
                                      onTap: sendMessage,
                                ),
                              ),
                                    ],
                                  )
                                  : Text('')
                            ],
                          ),
                        ),
                        Offstage(
                          offstage: !emojiShowing,
                          child: SizedBox(
                            height: 250,
                            child: EmojiPicker(
                              onEmojiSelected: (Category category, Emoji emoji) {
                                _onEmojiSelected(emoji);
                              },
                              onBackspacePressed: _onBackspacePressed,
                              config: Config(
                                columns: 8,
                                emojiSizeMax: 28 * (Platform.isIOS ? 1.30 : 1.0), // Issue: https://github.com/flutter/flutter/issues/28894
                                verticalSpacing: 0,
                                horizontalSpacing: 0,
                                gridPadding: EdgeInsets.zero,
                                initCategory: Category.RECENT,
                                bgColor: Colors.white,
                                indicatorColor: Theme.of(context).primaryColor,
                                iconColor: Colors.grey,
                                iconColorSelected: Theme.of(context).primaryColor,
                                progressIndicatorColor: Theme.of(context).primaryColor,
                                backspaceColor: Theme.of(context).primaryColor,
                                skinToneDialogBgColor: Colors.white,
                                skinToneIndicatorColor: Colors.grey,
                                enableSkinTones: true,
                                showRecentsTab: true,
                                recentsLimit: 32,
                                noRecents: const Text(
                                  'Pas d\'émojis récents',
                                  style: TextStyle(fontSize: 20, color: Colors.black26),
                                  textAlign: TextAlign.center,
                                ),
                                tabIndicatorAnimDuration: kTabScrollDuration,
                                categoryIcons: const CategoryIcons(),
                                buttonMode: ButtonMode.MATERIAL,
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  )
                ),
              ],
            ),
          ],
        ),
      ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-26
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多