Flutter TextField 值总是大写 &去抖动 [英] Flutter TextField value always uppercase & debounce

查看:56
本文介绍了Flutter TextField 值总是大写 &去抖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Flutter 的新手.我正在寻找始终大写的 TextField 值,但我没有找到任何相关资源.

另一个问题是 TextField onChanged 事件去抖动实现.当我在 TextField 上输入时,它会立即触发不适合我的目标的 onChanged 事件.onChange 事件将在每个文本更改 500 毫秒后触发.

 new TextField(控制器:_controller,装饰:新的 InputDecoration(提示文本: '在这里搜索',),onChanged: (str) {//需要实现去抖动})

解决方案

适用于 Android、iOS、Web、macOS、Windows 和 Linux

您可以实现自定义TextInputFormatter

class UpperCaseTextFormatter 扩展 TextInputFormatter {@覆盖TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {返回文本编辑值(文本:newValue.text.toUpperCase(),选择:newValue.selection,);}}


用法:

TextField(输入格式:[UpperCaseTextFormatter(),])

完整示例

I am new in Flutter. I am looking for TextField value to always capitalize but I did not find any resource on that.

Another issue is the TextField onChanged event debounce implementation. When I type on TextField it immediately fires onChanged event which is not suitable for my goal. The onChange event will fire after 500ms on every text changed.

 new TextField(
         controller: _controller,
         decoration: new InputDecoration(
              hintText: 'Search here',
         ),
         onChanged: (str) {
            //need to implement debounce
         }
)

解决方案

Works on Android, iOS, Web, macOS, Windows and Linux

You can implement a custom TextInputFormatter

class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(
      text: newValue.text.toUpperCase(),
      selection: newValue.selection,
    );
  }
}


Usage:

TextField(
  inputFormatters: [
    UpperCaseTextFormatter(),
  ]
)

Full example

这篇关于Flutter TextField 值总是大写 &去抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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