简体   繁体   English

Flutter cursor 焦点为真时键盘不显示

[英]Flutter The cursor and the keyboard does not show when focus is true

I am using a Linkable to highlight web links, phone numbers, and email addresses from what the user types into a textfield.我正在使用Linkable来突出显示用户在文本字段中键入的 web 链接、电话号码和 email 地址。 The first problem is that the linkable does not take textfield in, it only takes in a Text.第一个问题是 linkable 不接受文本字段,它只接受文本。 So, my solution was to show TextField only when user is typing, and replace Textfield with what the user typed in as Text when user is not typing.因此,我的解决方案是仅在用户键入时显示 TextField,并在用户不键入时将 Textfield 替换为用户键入的文本。 The second problem is that whenever the textfield is visible, at first it does not show the keyboard or the cursor, even when I can verify that its focus is true.第二个问题是,只要文本字段可见,起初它不会显示键盘或 cursor,即使我可以验证它的焦点是真实的。 How can I show cursor every time the textfield pops up?每次弹出文本字段时如何显示 cursor? I even set showcursor to true and it still does not work.我什至将 showcursor 设置为 true 但它仍然不起作用。 In another post I read that using a timer with a delay would solve the problem, so I did that but it still does not work.在另一篇文章中,我读到使用延迟计时器可以解决问题,所以我这样做了,但它仍然不起作用。

FocusNode _focusNode = FocusNode();
@override
  Widget build(BuildContext context) {
...
return GestureDetector(
onTap: () {
            FocusNode currentFocus = FocusScope.of(context);
           if (currentFocus.hasFocus) {
              currentFocus.unfocus();
           }
          },
child: Column(
          children: <Widget>[
              Visibility(
                visible: _focusNode.hasFocus,
                child: TextField(
                focusNode: _focusNode,
                showCursor: true,
                controller: _contentTextController,
              ),
           
            InkWell(
              child: Linkable(
                text: "testing",
                
              onTap: () async {
                FocusScope.of(context).requestFocus(_focusNode);

                //   FocusScope.of(context).requestFocus(_focusNode);
                //   Timer(const Duration(milliseconds: 1000), () {
                //     FocusScope.of(context).requestFocus(_focusNode);
                //     _focusNode.requestFocus();
                //   });
                  await Future.delayed(
                    Duration(milliseconds: 10)
                  );
                  Timer(const Duration(milliseconds: 10), () {
                    setState(() {
                    print(_focusNode.hasPrimaryFocus);
                    //showTextEditor = !showTextEditor;
                  });
                });
              }
            ),
          
          ],

Here is my code.这是我的代码。

I had the same issue.我遇到过同样的问题。 One workaround i tried that fixed the problem is我尝试解决问题的一种解决方法是

Future.delayed(const Duration(milliseconds: 10), () {
      focusNode.requestFocus();
    });

in other words to put a small delay before request focus.换句话说,在请求焦点之前放置一个小的延迟。 Hope will help希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 如何在单击 TextField 时始终隐藏键盘但保持焦点(保持显示光标) - Flutter How to always hide keyboard when click on TextField but keep focus(Keep show cursor) Flutter:当焦点文本字段时键盘不显示 - Flutter:Keyboard not show when focus TextField 当 flutter 文本字段中的自动对焦为真时,键盘未显示 - keyboard is not showing when auto focus is true in flutter textfield 单击颤动中的字段时,文本字段不显示键盘 - Textfield does not show keyboard when click on the field in flutter 颤振 | 如何始终保持文本字段处于焦点状态(保持显示光标) - Flutter | How to always keep Text Field in focus(Keep show cursor) 当焦点在 TextField 上并且在 Flutter 中打开键盘时,如何向上推送内容? - How to push content up when focus is on TextField and keyboard opens in Flutter? Flutter:当我在键盘上向上箭头时,ScrollView 的焦点发生变化 - Flutter: The focus of ScrollView change when I arrow up on keyboard Flutter:- 键盘聚焦时导致布局在底部溢出 - Flutter :- Keyboard causes layout to overflow on the bottom when on focus 显示脉冲 cursor 但初始化文本字段时不要打开键盘 - Show pulsing cursor but do not open the keyboard when textfield is initialized Flutter:将键盘聚焦在后退导航上 - Flutter: Focus keyboard on back navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM