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

Number is reset to initial value on pressing the "Done" button #28

Open roland-jungwirth opened 5 years ago

roland-jungwirth commented 5 years ago

Similarly to the closed issue, I am having a problem when clicking on the "Done" button. The displayed value is reset to the initial value (0.00 in most cases) when using the MoneyMaskedTextController. I can reproduce this on iPhone 5 and Android simulated and on a Samsung s10. I've stripped all my logic out of the code and still the issue happens. Should you need more information, please feel free to check in.

This is my implementation of the issue (I have stripped out unnecessary containers, etc. but I have tested it and it does the same):

Widget _createCurrencyField(Field field) {
    final _currencyController = MoneyMaskedTextController(
        initialValue: 0.0,
        decimalSeparator: ',',
        thousandSeparator: '.',
        rightSymbol: '',
        leftSymbol: '€');

     return TextField(
            controller: _currencyController,
            keyboardType: TextInputType.number,
          );
  }
soonsam123 commented 5 years ago

Storing the Controller in a global variable worked for me.

class _LoginPageState extends State<LoginPage> {
  MaskedTextController controller = MaskedTextController(mask: '000.000.000-00');

@override
  Widget build(BuildContext context) {
  ...
  }

Widget _buildCampoCPF() {
    return Container(
      margin: EdgeInsets.only(top: 20.0),
      child: TextFormField(
        controller: controller,
        ...
      ),
    );
  }

Got the idea from issue #13

rockerbacon commented 5 years ago

Storing the controller in a variable did not work for me

EDIT: What did work was making the Widget stateful instead of stateless