flutter textinputformatter 不起作用

发布于 2025-01-15 06:55:44 字数 2410 浏览 2 评论 0原文

我想在 textformfield 中实现 textinputformatter

我尝试了下面主题中的代码片段来格式化传入的数字文本以适合 (###) ###-#### ## 的格式,但它不起作用(类 _UsNumberTextInputFormatter 扩展了 TextInputFormatter)

https://medium.com/@rubensdemelo/flutter-formatting-textfield-with-textinputformatter-6caba78668e5

你能告诉我我做错了什么吗?格式化程序如何工作?


final _textInputFormatter = _UsNumberTextInputFormatter();
TextFormField(
              inputFormatters: [_textInputFormatter],
              readOnly: true,
              controller: _textController,
              decoration: InputDecoration(
                hintText: '(123) 456 78 90',
                suffixIcon: _textController.text.isNotEmpty
                    ? IconButton(
                        icon: const Icon(Icons.clear),
                        onPressed: () => _textController.clear(),
                      )
                    : null,
                helperText: 'Enter phone number',
              ),
            ),

class _UsNumberTextInputFormatter 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 = StringBuffer();
    if (newTextLength >= 1) {
      newText.write('(');
      if (newValue.selection.end >= 1) selectionIndex++;
    }
    if (newTextLength >= 4) {
      newText.write(newValue.text.substring(0, usedSubstringIndex = 3) + ') ');
      if (newValue.selection.end >= 3) selectionIndex += 2;
    }
    if (newTextLength >= 7) {
      newText.write(newValue.text.substring(3, usedSubstringIndex = 6) + '-');
      if (newValue.selection.end >= 6) selectionIndex++;
    }
    if (newTextLength >= 11) {
      newText.write(newValue.text.substring(6, usedSubstringIndex = 10) + ' ');
      if (newValue.selection.end >= 10) selectionIndex++;
    }
    // Dump the rest.
    if (newTextLength >= usedSubstringIndex)
      newText.write(newValue.text.substring(usedSubstringIndex));
    return TextEditingValue(
      text: newText.toString(),
      selection: TextSelection.collapsed(offset: selectionIndex),
    );
  }
}

I want to implement textinputformatter in textformfield

I tried code snippet from topic below to format incoming numeric text to fit the format of (###) ###-#### ##, but it's not working (class _UsNumberTextInputFormatter extends TextInputFormatter)

https://medium.com/@rubensdemelo/flutter-formatting-textfield-with-textinputformatter-6caba78668e5

Can you tell me what I'm doing wrong? How the formatter works?


final _textInputFormatter = _UsNumberTextInputFormatter();
TextFormField(
              inputFormatters: [_textInputFormatter],
              readOnly: true,
              controller: _textController,
              decoration: InputDecoration(
                hintText: '(123) 456 78 90',
                suffixIcon: _textController.text.isNotEmpty
                    ? IconButton(
                        icon: const Icon(Icons.clear),
                        onPressed: () => _textController.clear(),
                      )
                    : null,
                helperText: 'Enter phone number',
              ),
            ),

class _UsNumberTextInputFormatter 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 = StringBuffer();
    if (newTextLength >= 1) {
      newText.write('(');
      if (newValue.selection.end >= 1) selectionIndex++;
    }
    if (newTextLength >= 4) {
      newText.write(newValue.text.substring(0, usedSubstringIndex = 3) + ') ');
      if (newValue.selection.end >= 3) selectionIndex += 2;
    }
    if (newTextLength >= 7) {
      newText.write(newValue.text.substring(3, usedSubstringIndex = 6) + '-');
      if (newValue.selection.end >= 6) selectionIndex++;
    }
    if (newTextLength >= 11) {
      newText.write(newValue.text.substring(6, usedSubstringIndex = 10) + ' ');
      if (newValue.selection.end >= 10) selectionIndex++;
    }
    // Dump the rest.
    if (newTextLength >= usedSubstringIndex)
      newText.write(newValue.text.substring(usedSubstringIndex));
    return TextEditingValue(
      text: newText.toString(),
      selection: TextSelection.collapsed(offset: selectionIndex),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2025-01-22 06:55:44

我也有同样的问题。使用 TextFormField(...readOnly: false 是原因。

I had the same problem. Using TextFormField(...readOnly: false was the reason.

憧憬巴黎街头的黎明 2025-01-22 06:55:44

尝试一下这个包: https://pub.dev/packages/flutter_multi_formatter

你的 TextFormField 看起来像这:

TextFormField(
          inputFormatters: [MaskedInputFormatter('(###) ### ## ##')],
          readOnly: true,
          controller: _textController,
          decoration: InputDecoration(
            hintText: '(123) 456 78 90',
            suffixIcon: _textController.text.isNotEmpty
                ? IconButton(
                    icon: const Icon(Icons.clear),
                    onPressed: () => _textController.clear(),
                  )
                : null,
            helperText: 'Enter phone number',
          ),
        );

Give this package a try: https://pub.dev/packages/flutter_multi_formatter

Your TextFormField would look like this:

TextFormField(
          inputFormatters: [MaskedInputFormatter('(###) ### ## ##')],
          readOnly: true,
          controller: _textController,
          decoration: InputDecoration(
            hintText: '(123) 456 78 90',
            suffixIcon: _textController.text.isNotEmpty
                ? IconButton(
                    icon: const Icon(Icons.clear),
                    onPressed: () => _textController.clear(),
                  )
                : null,
            helperText: 'Enter phone number',
          ),
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文