简体   繁体   English

在键盘打开之前在 TextField 中点击打开对话框 - Flutter

[英]Open dialog on tap in TextField before keyboard opens - Flutter

Tapping in a TextField gives it focus and causes the keyboard to pop up.在 TextField 中点击会使其获得焦点并弹出键盘。 I need to show a dialog with a choice to the user before she/he types into the TextField.在她/他输入文本字段之前,我需要向用户显示一个带有选择的对话框。

I can do it but it's ugly: first the TextField gets focus and the keyboard comes up, then the dialog pops up and the keyboard disappears (since the TextField looses focus).我可以做到,但它很丑:首先 TextField 获得焦点并且键盘出现,然后弹出对话框并且键盘消失(因为 TextField 失去焦点)。 Then the focus goes back to the tapped TextField and the keyboard comes back up.然后焦点回到点击的 TextField 上,键盘又回来了。

I'd like to avoid all the ups and downs and I cannot add any extra buttons to call the dialog from somwhere else other then tapping on the TextField.我想避免所有的起伏,我不能添加任何额外的按钮来从其他地方调用对话框,然后点击 TextField。

Is there a way to do it so that the dialog appears first (without the keyboard showing up) and after the choice the focus goes to the tapped TextField?有没有办法让对话框首先出现(没有键盘出现),然后选择焦点转到点击的文本字段?

You can follow a simple trick.你可以遵循一个简单的技巧。 There is a read-only properties of TextField, you can use a flag on that read-only properties. TextField 有一个只读属性,您可以在该只读属性上使用标志。 When user chooses a selection from pop up, than change focus to that TextField.当用户从弹出窗口中选择一个选项时,将焦点更改为该 TextField。

bool readOnly = true;
FocusNode f1 = FocusNode();

TextFormField(                                
     read-only: readOnly ,
     focusNode: f1,
     decoration: InputDecoration(
     border: OutlineInputBorder(
     borderRadius: BorderRadius.circular(4.0)
      ),
      ),
     style: TextStyle(
      color: Colors.orange,
      fontSize: 15.0,
      ),
      ),

on your dialog code when selecting:选择时在对话框代码上:

readOnly = false;
FocusScope.of(context).requestFocus(f1);

store a map of focusNodes in a scoped model/context provider将焦点节点的 map 存储在范围模型/上下文提供程序中

check map if it should open dialog on specific focus node.检查 map 是否应该在特定焦点节点上打开对话框。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM