flutter-masked-text icon indicating copy to clipboard operation
flutter-masked-text copied to clipboard

incorrect value from MoneyMaskedTextController

Open codegrue opened this issue 5 years ago • 5 comments

The .numberValue property (and .text for that matter) are giving me a value with the decimal in the wrong place (different from how it looks in my TextField).

Code used:

    var moneyController = MoneyMaskedTextController(
        decimalSeparator: '.',
        thousandSeparator: ',');
    moneyController.updateValue(65.99);

change value via TextField to 659.91

  moneyController.numberValue
  // returns  65.991

also:

  • It always seems to return 3 places after the decimal, not 2
  • Once the thousands placeholders appear, the TextField onchange event stops firing

codegrue avatar Aug 06 '18 21:08 codegrue

Hi!

I'm using this sample and I can't reproduce these errors:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var moneyController = MoneyMaskedTextController(
        decimalSeparator: '.',
        thousandSeparator: ',');
    moneyController.updateValue(65.99);

    moneyController.addListener((){
      print(moneyController.numberValue);
    });

    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new SafeArea(
        child: new Scaffold(
          body: new Column(
            children: <Widget>[
              new TextField(controller: moneyController, keyboardType: TextInputType.number,) // <--- here
            ],
          ),
        ),
      ),
    );
  }
}

This is my screen after add a lot of digits: image

And this is the console output:

Performing hot restart...
Restarted app in 1,809ms.
I/flutter ( 8387): 65.99
I/flutter ( 8387): 65.99
I/flutter ( 8387): 659.91
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 659.91
I/flutter ( 8387): 6599.12
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 5 lines
I/flutter ( 8387): 6599.12
I/flutter ( 8387): 6599.12
I/flutter ( 8387): 65991.23
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 65991.23
I/flutter ( 8387): 659912.34
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 659912.34
I/flutter ( 8387): 6599123.45
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 6599123.45
I/flutter ( 8387): 65991234.56
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 65991234.56
I/flutter ( 8387): 659912345.67
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 659912345.67
I/flutter ( 8387): 6599123456.78
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 6599123456.78
I/flutter ( 8387): 65991234567.89
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 65991234567.89
I/flutter ( 8387): 659912345678.9
I/chatty  ( 8387): uid=10083(com.benhurott.maskedtextexample) Thread-2 identical 6 lines
I/flutter ( 8387): 659912345678.9

Can you add more information? (SO, Flutter version, code sample, etc)

bhrott avatar Aug 07 '18 02:08 bhrott

It has something to do with the TextField onChange and TextFormField onSave methods. The "value" returned from these is formatted incorrectly. In my preference, the value returned from these should be the controller's numberValue not the literal string (i.e. no thousanths indicators).

I was playing with just the pure MaskedTextController and it has a similar issue, the returned value is can lag the controller text. I added this to the example below. Type '1234' and the value will be missing the right paren. Similarly, would prefer to get an "unmasked" value as the value, to not have to parse the result to remove the mask characters.

Edit: I did originally state that .numberValue was giving me the same incorrect value. In my app that is indeed the case. Not yet able to reproduce in this sample. Maybe it's a timing issue of some kind.


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

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var moneyController = MoneyMaskedTextController(
        decimalSeparator: '.', thousandSeparator: ',');
    moneyController.updateValue(65.99);

    var phoneController = MaskedTextController(mask: '(000) 000-0000');
    moneyController.updateValue(65.99);

    void moneyChanged(value) {
      print('value = ' + value);
      print('controller.text = ' + moneyController.text);
      print('numberValue = ' + moneyController.numberValue.toString());
      print(''); //linebreak
    }

    void phoneChanged(value) {
      print('value = ' + value);
      print('controller.text = ' + phoneController.text);
      print(''); //linebreak
    }

    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new SafeArea(
        child: new Scaffold(
          body: new Column(
            children: <Widget>[
              new TextField(
                controller: moneyController,
                keyboardType: TextInputType.number,
                onChanged: moneyChanged,
              ),
              new TextField(
                controller: phoneController,
                keyboardType: TextInputType.number,
                onChanged: phoneChanged,
              )
            ],
          ),
        ),
      ),
    );
  }
}

codegrue avatar Aug 07 '18 12:08 codegrue

I'm having this same issue. To mean it appears that the value from the onChanged function is giving the previous value, not the new value.

coleweinman avatar Nov 03 '19 18:11 coleweinman

I have the same issue. I believe it does have something to do with the onSave method. When my money field is in a form and I call save(), the value that I get passed to the onSaved() method is the value with the last digit appended to it. For example, if my masked value was $0.75 and I pressed 8, I would see $.0758 passed to my onSaved() method. I spent some time trying to figure it out but no luck so far. Any fix would be greatly appreciated.

kevin-haynie avatar Nov 30 '19 01:11 kevin-haynie

I'm having this same issue. To mean it appears that the value from the onChanged function is giving the previous value, not the new value.

I was having the same issue, I resolved it (with a workaround) passing controller Text property to the onChanged function, like this:

new TextField(
    controller: moneyController,
    keyboardType: TextInputType.number,
    onChanged: moneyChanged(moneyController.text),
)

ebsene avatar Feb 06 '20 03:02 ebsene