Flutter The cursor and the keyboard does not show when focus is true
Asked Answered
D

2

6

I am using a Linkable to highlight web links, phone numbers, and email addresses from what the user types into a textfield. The first problem is that the linkable does not take textfield in, it only takes in a Text. 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. 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. How can I show cursor every time the textfield pops up? I even set showcursor to true and it still does not work. 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.

Duhamel answered 26/6, 2021 at 8:56 Comment(0)
C
1

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

Counterfeit answered 30/9, 2022 at 10:41 Comment(0)
S
0
  WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        _focusNode.requestFocus();
      });
Selfconfidence answered 21/2, 2024 at 15:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.