如何在Flutter的TextFromField上使用按键事件? [英] How to use Key Press Event on TextFromField in Flutter?

查看:158
本文介绍了如何在Flutter的TextFromField上使用按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在文本字段中捕获按键?在我的情况下,当用户在文本字段内按Enter键时,值将被存储.为此,我需要像在Kotlin + Android中一样使用Keypress-event.我本周刚开始尝试Flutter,因为它有趣且跨平台.

Is there any way to catch a keypress in textfield? In my case, when the user press enter key inside the text field, the values will be stored. For this to happen, I need to use Keypress-event like in Kotlin+Android. I just started trying flutter this week since it is interesting and cross-platform.

RawKeyboardListener(
  child: TextFormField(
    keyboardType: TextInputType.text,
    decoration: new InputDecoration(labelText: "Phone"),
    validator: (val) => val.length == 0 ? 'Enter your phone' : null,
    onSaved: (val) => this.phone = val,
  ),
   focusNode: FocusNode(),
   onKey: (RawKeyEvent event) {
     print(event.data.logicalKey.keyId);
     if (event.runtimeType == RawKeyDownEvent ) {
       print("asdadda");

     }
   },
),

但是我不知道为什么它不起作用,但是我按下了键.

But I don't know why it doesn't work however I press key.

推荐答案

我确定您要查找的是TextField的 onSubmitted .这样做是在按键盘上的 Enter 时,它会为您提供值,该值将作为参数/参数.有关此的更多信息,您可以查看以下内容: onSubmitted属性TextField

I am sure what you're looking for is TextField's onSubmitted. What does this do is, on press of Enter on your keyboard, it gives you the value, which it takes as a param/args. For more info about this, you can checkout this: onSubmitted Property TextField

如何执行此操作,它是TextField的属性,您只需简单地执行此操作即可完成任务.

How you can do this, it is a property of TextField, you just have to simply do this to get your task done.

您还可以在此属性内执行任何操作.

You can also do anything inside this property.

TextField(
  onSubmitted: (value){ 
     print(value);
     // or do whatever you want when you are done editing
     // call your method/print values etc
  }
)

还详细了解 TextField类.这将在许多方面为您提供帮助.希望对您有帮助.学习愉快:)

Read more about TextField Class also. This will help you in many ways. Hope that will help you in your case. Happy learning :)

这篇关于如何在Flutter的TextFromField上使用按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆