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

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

Open roland-jungwirth opened this issue 5 years ago • 2 comments

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

roland-jungwirth avatar May 07 '19 21:05 roland-jungwirth

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

soonsam123 avatar Jul 15 '19 17:07 soonsam123

Storing the controller in a variable did not work for me

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

rockerbacon avatar Jul 18 '19 17:07 rockerbacon