android - 如何处理Flutter中的文字栏位?

标签 android android-studio flutter dart

我有以下文本,我想像这样处理:如果用户输入前两个数字,则用户将在此文本字段中输入数据,然后在用户输入最后两个数字之前自动插入 /
代码

      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),
                ),
              ),
            ),
Image

最佳答案

  • 您可以创建自定义的InputFormatter并在TextField中编写用于格式化文本的逻辑。
  • 有一个名为flutter_mask_text的软件包,可减轻创建自定义InputFormatter的压力。我将链接添加到下面的包中:

  • Flutter masked text


    软件包的示例演示:
    创建一个 Controller :
    var controller = new MaskedTextController(mask: '00/00');
    
    将 Controller 分配给您的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),
                    ),
                  ),
                ),
    

    关于android - 如何处理Flutter中的文字栏位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63569173/

    相关文章:

    java - Android Studio 提供的 android 应用程序触发错误

    android - 通过 Google Fit Android API 检索的每周步数与 Google Fit 官方应用中显示的步数不匹配

    ios - Xcode 无法为 ios 13.3.1 设备构建 flutter 代码,但在 13.3 ios 模拟器上运行代码

    dart - 无法根据状态更改页面

    android - 何时保存文档

    Android:从相机获取图片时发生 NPE

    android - 水平滚动在 phonegap 中不起作用

    android - 使用 Android SDK+NDK 构建复杂的原生 + Java 库的最佳方式

    android - 在我的应用程序中出现构建错误

    java - SQLite查询产生最后一个但一行值而不是最后插入的行值