Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CurrencyInputFormatter - onValueChange reports incorrect value when cursor is at the end #115

Closed
nizioleque opened this issue Jan 7, 2023 · 3 comments

Comments

@nizioleque
Copy link

nizioleque commented Jan 7, 2023

I am using CurrencyInputFormatter in the following way:

CurrencyInputFormatter(
  // ...
  mantissaLength: 2,
  onValueChange: (value) => print("NEW VALUE $value"),
)

When the cursor is at the end of the input (example here:)
image

and I type something (for example 5), the value in the input does not change (which is correct), but I get this message:

NEW VALUE 123456.255

which is clearly incorrect and inconsistent with the content of the input.

Can you please fix this issue?

@caseyryan
Copy link
Owner

It's a bug of Flutter, I can't do anything with this. Sorry.

@FayozbekJumabekov
Copy link

i have this issue also. Can anyone find solution for this bug?

@nizioleque
Copy link
Author

Here's the workaround that I came up with, it's a custom class that extends CurrencyInputFormatter:

class LocaleCurrencyInputFormatter extends CurrencyInputFormatter {
  factory LocaleCurrencyInputFormatter({
    required Locale locale,
    required Function(Fixed value) onValueChange,
  }) {
    final parameters = getCurrencyFormatParameters(
      locale: locale,
    );

    return LocaleCurrencyInputFormatter._(
      leadingSymbol: parameters.leadingSymbol,
      trailingSymbol: parameters.trailingSymbol,
      mantissaLength: parameters.mantissaLength,
      thousandSeparator: parameters.thousandSeparator,
      onValueChange: (value) {
        if (value.isNaN) return;

        // workaround for issue
        // https://github.com/caseyryan/flutter_multi_formatter/issues/115
        final correctValue =
            (value * pow(10, parameters.mantissaLength)).truncate() /
                pow(10, parameters.mantissaLength);

        final Fixed fixedValue = Fixed.fromNum(correctValue, scale: 2);
        onValueChange(fixedValue);
      },
    );
  }

  LocaleCurrencyInputFormatter._({
    super.leadingSymbol,
    super.trailingSymbol,
    super.mantissaLength,
    super.thousandSeparator,
    super.onValueChange,
  });
}

I use it together with my workaround for #134 like this:

return TextFormField(
  inputFormatters: [
    // workaround for issue
    // https://github.com/caseyryan/flutter_multi_formatter/issues/134
    FixZeroBeforeInputFormatter(shouldReplace),
    LocaleCurrencyInputFormatter(
      locale: Localizations.localeOf(context),
      onValueChange: (value) {
        if (shouldReplace[0]) {
          shouldReplace[0] = false;
          value = Fixed.zero;
        }
        widget.onChanged(value);
      },
    ),
    FixZeroAfterInputFormatter(shouldReplace),
  ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants