简体   繁体   English

如何处理颤振中的文本字段?

[英]how to handle textfields in flutter?

I have the following text filed in flutter, i want to handle this like while user is going to enter data into this text field if user enters first two numbers than their will be inserted / automatically before user enters last two numbers.我在flutter中提交了以下文本,我想处理这个问题,如果用户输入前两个数字而不是在用户输入最后两个数字之前自动插入/自动,则用户将数据输入到此文本字段中。

Code代码

      Container(
              width: MediaQuery.of(context).size.width * 0.45,
              child: TextField(
                style: style,
                maxLength: 4,
                cursorColor: Colors.red,
                textAlign: TextAlign.left,
                keyboardType: TextInputType.datetime,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Color(0xFF1E1E1E),
                  hintText: 'MM/YY',
                  hintStyle: TextStyle(fontSize: 16, color: Colors.white),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: BorderSide(
                      width: 0,
                      style: BorderStyle.none,
                    ),
                  ),
                  contentPadding: EdgeInsets.all(16),
                ),
              ),
            ),

图片

  1. You can create a custom InputFormatter and write the logic for formatting text in the TextField .您可以创建自定义InputFormatter并在TextField编写用于格式化文本的逻辑。

  2. There is a package called flutter_mask_text which saves you the stress of creating a custom InputFormatter.有一个名为flutter_mask_text的包,它可以为您节省创建自定义 InputFormatter 的压力。 I added the link to the package below:我在下面的包中添加了链接:

Flutter masked text颤动蒙版文本

Sample demo for the package:包的示例演示:

Create a controller:创建控制器:

var controller = new MaskedTextController(mask: '00/00');

Assign the controller to your TextField :将控制器分配给您的TextField

        Container(
              width: MediaQuery.of(context).size.width * 0.45,
              child: TextField(
                controller: controller, // new line
                style: style,
                maxLength: 4,
                cursorColor: Colors.red,
                textAlign: TextAlign.left,
                keyboardType: TextInputType.datetime,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Color(0xFF1E1E1E),
                  hintText: 'MM/YY',
                  hintStyle: TextStyle(fontSize: 16, color: Colors.white),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: BorderSide(
                      width: 0,
                      style: BorderStyle.none,
                    ),
                  ),
                  contentPadding: EdgeInsets.all(16),
                ),
              ),
            ),

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

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