用于日期的 flutter 输入格式化程序

标签 flutter

我正在寻找一个用于文本字段的 inputformatter 示例,它将是日期 mm/dd/yyyy,当用户键入更新格式时,我正在尝试执行此操作。例如用户开始输入mm并输入/,然后当输入dd时输入/。

有没有人做过这个或者有一个例子?我已经用其他语言完成了,但在 flutter/dart 中找不到类似的方法。

这是我迄今为止尝试过的,但无法使逻辑正确。有任何想法吗?

    class _DateFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue,
      TextEditingValue newValue
      ) {
    final int newTextLength = newValue.text.length;
    int selectionIndex = newValue.selection.end;
    int usedSubstringIndex = 0;
    final StringBuffer newText = new StringBuffer();
    if (newTextLength == 2) {
      newText.write(newValue.text.substring(0, 2) + '/ ');
      if (newValue.selection.end == 3)
        selectionIndex+=3;
    }
    if (newTextLength == 5) {
      newText.write(newValue.text.substring(0, 5) + '/ ');
      if (newValue.selection.end == 6)
        selectionIndex += 6;
    }
    // Dump the rest.
    if (newTextLength >= usedSubstringIndex)
      newText.write(newValue.text.substring(usedSubstringIndex));
    return new TextEditingValue(
      text: newText.toString(),
      selection: new TextSelection.collapsed(offset: selectionIndex),
    );
  }
}

谢谢

最佳答案

我也为此苦苦挣扎。我最终得到了以下不太优雅的解决方案:

class DateInputTextField extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _DateInputTextFieldState();
  }
}

class _DateInputTextFieldState extends State<DateInputTextField> {
  @override
  Widget build(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.number,
      inputFormatters: [DateTextFormatter()],
      onChanged: (String value) {},
    );
  }
}

class DateTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {

    //this fixes backspace bug
    if (oldValue.text.length >= newValue.text.length) {
      return newValue;
    }

    var dateText = _addSeperators(newValue.text, '/');
    return newValue.copyWith(text: dateText, selection: updateCursorPosition(dateText));
  }

  String _addSeperators(String value, String seperator) {
    value = value.replaceAll('/', '');
    var newString = '';
    for (int i = 0; i < value.length; i++) {
      newString += value[i];
      if (i == 1) {
        newString += seperator;
      }
      if (i == 3) {
        newString += seperator;
      }
    }
    return newString;
  }

  TextSelection updateCursorPosition(String text) {
    return TextSelection.fromPosition(TextPosition(offset: text.length));
  }
}

关于用于日期的 flutter 输入格式化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47403758/

相关文章:

flutter - UI 在渲染前不等待值

当文本字段从 Controller 带来值时,Flutter 文本字段 onChanged 不起作用

flutter - 多个访问同一流

android - 当使用发布 apk 时,某些代码已被 Dart AOT 编译器 (TFA) 删除

ios - Flutter 中的共享首选项错误 - Mac 上的 iOS

asynchronous - 异步对象未向 get_it 注册

flutter - 使用 Bloc 从 ListView 中删除特定项目

image - 如何获得一个数组/列表,其中包含我在Flutter中作为 Assets 加载的所有图像路径?

android - 如何将耀斑动画从几秒减到几小时

flutter - Flutter中解析Json日期