【问题标题】:flutter: how to show currency digits inside textfield颤动:如何在文本字段中显示货币数字
【发布时间】:2021-03-01 13:25:55
【问题描述】:

我想创建一个textfield 用于输入像这些图片中的韩元(韩币)金额。 每 3 位数如何显示逗号?

     TextFormField(
                decoration: InputDecoration(hintText: '예) 10,000 원'),
                onChanged: (newValue) {
                  _price = newValue;
                }),

【问题讨论】:

    标签: flutter textfield digits


    【解决方案1】:

    也许this资源可以帮助你

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Container(
              width: 300,
              child: TextField(
                decoration: InputDecoration(
                  border: const OutlineInputBorder(),
                ),
                keyboardType: TextInputType.number,
                inputFormatters: [ThousandsSeparatorInputFormatter()],
              ),
            ),
          ),
        );
      }
    }
    
    class ThousandsSeparatorInputFormatter extends TextInputFormatter {
      static const separator = ','; // Change this to '.' for other locales
    
      @override
      TextEditingValue formatEditUpdate(
          TextEditingValue oldValue, TextEditingValue newValue) {
        // Short-circuit if the new value is empty
        if (newValue.text.length == 0) {
          return newValue.copyWith(text: '');
        }
    
        // Handle "deletion" of separator character
        String oldValueText = oldValue.text.replaceAll(separator, '');
        String newValueText = newValue.text.replaceAll(separator, '');
    
        if (oldValue.text.endsWith(separator) &&
            oldValue.text.length == newValue.text.length + 1) {
          newValueText = newValueText.substring(0, newValueText.length - 1);
        }
    
        // Only process if the old value and new value are different
        if (oldValueText != newValueText) {
          int selectionIndex =
              newValue.text.length - newValue.selection.extentOffset;
          final chars = newValueText.split('');
    
          String newString = '';
          for (int i = chars.length - 1; i >= 0; i--) {
            if ((chars.length - 1 - i) % 3 == 0 && i != chars.length - 1)
              newString = separator + newString;
            newString = chars[i] + newString;
          }
    
          return TextEditingValue(
            text: newString.toString(),
            selection: TextSelection.collapsed(
              offset: newString.length - selectionIndex,
            ),
          );
        }
    
        // If the new value and old value are the same, just return as-is
        return newValue;
      }
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用flutter_masked_text包:

      安装包并导入依赖后创建控制器:

        var controller = MoneyMaskedTextController(
      thousandSeparator: ',',
      rightSymbol: ' 원',
      decimalSeparator: '',
      precision: 0);
      

      在您的文本字段中使用它:

      TextField(
              keyboardType: TextInputType.number,
              textAlign: TextAlign.end,
              controller: controller,
            ),
      

      得到你想要的结果: final result

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多