inputFormatter 应该只允许十进制数和负数

inputFormatter should allow just decimal numbers and negative numbers

我想允许用户仅添加数字“12345”和十进制数字(如“21321.12312”)和负数(如 -23423.32432)。用户不应该能够添加多个“。”像“12..32”,并在输入的第一个位置添加“-”,例如 -324.34 而不是 324-4323。 我使用了这个正则表达式 r'^(-?\d+\.\d+)(\s*,\s*-?\d+\.\d+)+$' 但无法输入任何内容。

文本字段代码:

TextFormField(
          inputFormatters: [
            FilteringTextInputFormatter.allow(
                RegExp(r'^(-?\d+\.\d+)(\s*,\s*-?\d+\.\d+)+$')),
          ],
          controller: budget,
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            contentPadding:
                EdgeInsets.only(right: 20, left: 20, top: 10, bottom: 10),
            hintText: getTranslated(context, "budget_example"),
            hintStyle: TextStyle(fontSize: 13, fontFamily: "tahoma"),
            border: OutlineInputBorder(
              borderSide: BorderSide(width: 1, color: MyColors.secondary),
              borderRadius: BorderRadius.circular(100),
            ),
          ),
        ),

只允许 1 个。

允许负数

开头加负号

我们应该创建自己的输入格式化程序

import 'package:flutter/services.dart';

class NumberTextInputFormatter extends TextInputFormatter {
  NumberTextInputFormatter({this.decimalRange}) : assert(decimalRange == null || decimalRange > 0);

  final int decimalRange;

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    TextEditingValue _newValue = this.sanitize(newValue);
    String text = _newValue.text;

    if (decimalRange == null) {
      return _newValue;
    }

    if (text == '.') {
      return TextEditingValue(
        text: '0.',
        selection: _newValue.selection.copyWith(baseOffset: 2, extentOffset: 2),
        composing: TextRange.empty,
      );
    }

    return this.isValid(text) ? _newValue : oldValue;
  }

  bool isValid(String text) {
    int dots = '.'.allMatches(text).length;

    if (dots == 0) {
      return true;
    }

    if (dots > 1) {
      return false;
    }

    return text.substring(text.indexOf('.') + 1).length <= decimalRange;
  }

  TextEditingValue sanitize(TextEditingValue value) {
    if (false == value.text.contains('-')) {
      return value;
    }

    String text = '-' + value.text.replaceAll('-', '');

    return TextEditingValue(text: text, selection: value.selection, composing: TextRange.empty);
  }
}

和(别忘了导入之前的class)

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class NumberFormField extends StatelessWidget {
  final InputDecoration decoration;
  final TextEditingController controller;
  final int decimalRange;

  const NumberFormField({Key key, this.decoration, this.controller, this.decimalRange}) :super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      decoration: this.decoration,
      controller: this.controller,
      keyboardType: TextInputType.numberWithOptions(decimal: true),
      inputFormatters: [
        WhitelistingTextInputFormatter(RegExp(r'[\d+\-\.]')),
        NumberTextInputFormatter(decimalRange: this.decimalRange),
      ],
    );
  }
}