[英]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.