首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter TextFormField装饰

Flutter TextFormField装饰
EN

Stack Overflow用户
提问于 2020-06-22 22:42:51
回答 1查看 89关注 0票数 1

如何在TextFormfield中将数字"123456789“显示为”123456789“?

代码语言:javascript
复制
TextFormField(
    controller: _numberId,
    keyboardType: TextInputType.number,
),
EN

回答 1

Stack Overflow用户

发布于 2020-06-23 04:58:05

我在字符串上写了下面的扩展:

代码语言:javascript
复制
extension StringSeprate on String {
  String stringSeparate(
      {int count = 3, String separator = ",", bool fromRightToLeft = true}) {
    if (this.isEmpty) {
      return "";
    }

    if (count < 1) {
      return this;
    }

    if (count >= this.length) {
      return this;
    }

    var str = this.replaceAll(separator, "");

    if (fromRightToLeft) {
      str = String.fromCharCodes(str.runes.toList().reversed);
    }

    var chars = str.runes.toList();
    var namOfSeparation =
        (chars.length.toDouble() / count.toDouble()).ceil() - 1;
    var separatedChars = List(chars.length + namOfSeparation.round());
    var j = 0;
    for (var i = 0; i < chars.length; i++) {
      separatedChars[j] = String.fromCharCode(chars[i]);
      if (i > 0 && (i + 1) < chars.length && (i + 1) % count == 0) {
        j += 1;
        separatedChars[j] = separator;
      }

      j += 1;
    }

    return fromRightToLeft
        ? String.fromCharCodes(separatedChars.join().runes.toList().reversed)
        : separatedChars.join();
  }
}

最终代码(复制文件中的以下代码并从main调用它进行测试):

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class TestWidget extends StatelessWidget {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextField(
              controller: _textEditingController,
              inputFormatters: [
                TextInputFormatter.withFunction((oldValue, newValue) =>
                    TextEditingValue(
                        text: newValue.text.stringSeparate(
                            separator: ' ', fromRightToLeft: false)))
              ],
              onChanged: (String value) {
                if (value == null) return;
                _textEditingController.selection = TextSelection.fromPosition(
                    TextPosition(
                        offset: value.length, affinity: TextAffinity.upstream));
              },
            ),
          ],
        ),
      ),
    );
  }
}

extension StringSeprate on String {
  String stringSeparate(
      {int count = 3, String separator = ",", bool fromRightToLeft = true}) {
    if (this.isEmpty) {
      return "";
    }

    if (count < 1) {
      return this;
    }

    if (count >= this.length) {
      return this;
    }

    var str = this.replaceAll(separator, "");

    var chars = str.runes.toList();
    var namOfSeparation =
        (chars.length.toDouble() / count.toDouble()).ceil() - 1;
    var separatedChars = List(chars.length + namOfSeparation.round());
    var j = 0;
    for (var i = 0; i < chars.length; i++) {
      separatedChars[j] = String.fromCharCode(chars[i]);
      if (i > 0 && (i + 1) < chars.length && (i + 1) % count == 0) {
        j += 1;
        separatedChars[j] = separator;
      }

      j += 1;
    }

    return fromRightToLeft
        ? String.fromCharCodes(separatedChars.join().runes.toList().reversed)
        : separatedChars.join();
  }
}

结果如下:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62517048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档