0

I have a problem with Flutter InputTextField. I'm developing an app like CashApp were you have a feature which you can send money to other people.

The problem is: I need to achieve a number format and allow only two decimals.

For example, if I enter:

  • "1000,25" -> It needs to transform to 1.000,25

  • "10000,25" -> It needs to transform to 10.000,25

And so on..

I've been using this code to reach the only two decimals part

import 'package:flutter/services.dart';
import 'dart:math' as math;

class DecimalTextInputFormatter extends TextInputFormatter {
  DecimalTextInputFormatter({this.decimalRange, this.activatedNegativeValues})
      : assert(decimalRange == null || decimalRange >= 0,
            'DecimalTextInputFormatter declaretion error');

  final int decimalRange;
  final bool activatedNegativeValues;

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue, // unused.
    TextEditingValue newValue,
  ) {
    TextSelection newSelection = newValue.selection;
    String truncated = newValue.text;


if (newValue.text.contains(' ')) {
  return oldValue;
}

if (newValue.text.isEmpty) {
  return newValue;
} else if (double.tryParse(newValue.text) == null &&
    !(newValue.text.length == 1 &&
        (activatedNegativeValues == true ||
            activatedNegativeValues == null) &&
        newValue.text == '-')) {
  return oldValue;
}

if (activatedNegativeValues == false &&
    double.tryParse(newValue.text) < 0) {
  return oldValue;
}

if (decimalRange != null) {
  String value = newValue.text;

  if (decimalRange == 0 && value.contains(".")) {
    truncated = oldValue.text;
    newSelection = oldValue.selection;
  }

  if (value.contains(".") &&
      value.substring(value.indexOf(".") + 1).length > decimalRange) {
    truncated = oldValue.text;
    newSelection = oldValue.selection;
  } else if (value == ".") {
    truncated = "0.";

    newSelection = newValue.selection.copyWith(
      baseOffset: math.min(truncated.length, truncated.length + 1),
      extentOffset: math.min(truncated.length, truncated.length + 1),
    );
  }

  return TextEditingValue(
    text: truncated,
    selection: newSelection,
    composing: TextRange.empty,
  );
}
return newValue;
}
}

This is a response I found from this post

This code almost do the thing. It restrict the two decimals part, but doesn't have the NumberFormat part, and instead of 'commas' for the decimals, it uses 'dots'.

I want to swap the '.' for thousands and ',' for decimals. And add the NumberFormat too.

Is there any way I can achieve this?

James Z
  • 12,209
  • 10
  • 24
  • 44
Nikssj_
  • 187
  • 2
  • 3
  • 11

1 Answers1

0

Is this for locale Brazil? Then the number currency format of Brazil could be set in you DecimalTextInputFormatter class to achieve the same result.

bluenile
  • 5,673
  • 3
  • 16
  • 29
  • Not Brazil, but Argentina. I can change the locale to ARG. In which part of the class I have to paste your part? Can you give me a full example if its not a problem please? Thanks! – Nikssj_ Nov 02 '20 at 05:37
  • I've added it as a new Class. But I cant input anything, just one number :/. Its like it get stucked – Nikssj_ Nov 02 '20 at 05:40