bhrott / flutter-masked-text

A masked text for Flutter.
https://pub.dartlang.org/packages/flutter_masked_text
MIT License
276 stars 124 forks source link

incorrect value from MoneyMaskedTextController #7

Open codegrue opened 6 years ago

codegrue commented 6 years ago

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:

bhrott commented 6 years ago

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)

codegrue commented 6 years ago

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,
              )
            ],
          ),
        ),
      ),
    );
  }
}
coleweinman commented 4 years ago

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.

kevin-haynie commented 4 years ago

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.

ebsene commented 4 years ago

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),
)