Skip to main content

try this:

inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
    return text.isEmpty
       if (text.isNotEmpty)? double.parse(text);newValue
          : double.tryParse(text) return== newValue;null
          } catch (e)? {}oldValue
          return oldValue;
     : newValue;
  }),
      ],

demo:

TextFormField(
      keyboardType:
          TextInputType.numberWithOptions(
    decimal: true,
    signed: false),
    ),
  onChanged: _yourOnChange,
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
        final text try= {newValue.text;
      return text.isEmpty
     final text = newValue.text;
  ? newValue
         if (text.isNotEmpty): double.parsetryParse(text);
        == null
   return newValue;
          } catch (e)? {}oldValue
          return oldValue;
   : newValue;
    }),
      ],
    )

try this:

inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],

demo:

TextFormField(
      keyboardType:
          TextInputType.numberWithOptions(decimal: true, signed: false),
      onChanged: _yourOnChange,
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],
    )

try this:

inputFormatters: [
  FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
  TextInputFormatter.withFunction((oldValue, newValue) {
    final text = newValue.text;
    return text.isEmpty
        ? newValue
        : double.tryParse(text) == null
            ? oldValue
            : newValue;
  }),
],

demo:

TextFormField(
  keyboardType: TextInputType.numberWithOptions(
    decimal: true,
    signed: false,
  ),
  onChanged: _yourOnChange,
  inputFormatters: [
    FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
    TextInputFormatter.withFunction((oldValue, newValue) {
      final text = newValue.text;
      return text.isEmpty
          ? newValue
          : double.tryParse(text) == null
              ? oldValue
              : newValue;
    }),
  ],
)
added 52 characters in body
Source Link
Chosan
  • 689
  • 5
  • 8

try this:

 inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],

demo:

TextFormField(
      keyboardType:
          TextInputType.numberWithOptions(decimal: true, signed: false),
      onChanged: _yourOnChange,
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],
    )

try this:

 inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            double.parse(newValue.text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],

try this:

inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],

demo:

TextFormField(
      keyboardType:
          TextInputType.numberWithOptions(decimal: true, signed: false),
      onChanged: _yourOnChange,
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            final text = newValue.text;
            if (text.isNotEmpty) double.parse(text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],
    )
Source Link
Chosan
  • 689
  • 5
  • 8

try this:

 inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
        TextInputFormatter.withFunction((oldValue, newValue) {
          try {
            double.parse(newValue.text);
            return newValue;
          } catch (e) {}
          return oldValue;
        }),
      ],