0
votes

I'm creating a app, where one of the features is update text fields based on changes of another text fields.

The fields are about product prices in: Dollar, Euro, Real.

If the price in Dollar are changed by user, the Euro and Real price are too, and so on...

The problem is, if I use the normal TextEditingController all works well, but if I use MoneyMaskedTextController from the flutter_masked_text package, the updating stops.

Could any one test my code and to answer me why the updating stops with the MoneyMaskedTextController?

To test, don't forget to install the flutter_masked_text: ^0.8.0 in your pubspec.yaml.

If I can not use the flutter_masked_text for it, how could I use masks and update text fields?

Thank you.

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

void main() {
  runApp(MaterialApp(
    home: ProductType(),
  ));
}

class ProductType extends StatefulWidget {
  _ProductTypeScreen createState() => _ProductTypeScreen();
}

class _ProductTypeScreen extends State<ProductType> {

  @override
  Widget build(BuildContext context) {

    double dollarRate = 3.70;
    double euroRate = 4.20;

    //Normal controllers
/*    final ctrl_real = TextEditingController();
    final ctrl_dollar = TextEditingController();
    final ctrl_euro = TextEditingController();*/
    //Money Mask controllers
    final ctrl_real = MoneyMaskedTextController();
    final ctrl_dollar = MoneyMaskedTextController();
    final ctrl_euro = MoneyMaskedTextController();

    void change_real(String text) {
      double real = double.parse(text);
      ctrl_dollar.text = (real / dollarRate).toString();
      ctrl_euro.text = (real / euroRate).toString();
    }

    void change_dollar(String text) {
      double dolar = double.parse(text);
      ctrl_real.text = (dolar * dollarRate).toString();
      ctrl_euro.text = (dolar * dollarRate / euroRate).toString();
    }

    void change_euro(String text) {
      double euro = double.parse(text);
      ctrl_real.text = (euro * euroRate).toString();
      ctrl_dollar.text = (euro * euroRate / dollarRate).toString();
    }

    return Scaffold(
        body: SingleChildScrollView(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[

                  Container(
                      width: 160.0,
                      height: 200.0,
                      padding: EdgeInsets.all(20.0),
                      child: TextField(
                        controller: ctrl_euro,
                        decoration: InputDecoration(
                            labelText: "Euro price",
                            prefixText: "€"),
                        onChanged: change_euro,
                        keyboardType: TextInputType.numberWithOptions(
                            decimal: true),
                      )),

                  Container(
                      width: 160.0,
                      height: 200.0,
                      padding: EdgeInsets.all(20.0),
                      child: TextField(
                        controller: ctrl_dollar,
                        decoration: InputDecoration(
                            labelText: "Dolar price",
                            prefixText: "US\$"),
                        onChanged: change_dollar,
                        keyboardType: TextInputType.numberWithOptions(
                            decimal: true),
                      )),

                  Container(
                      width: 160.0,
                      height: 200.0,
                      padding: EdgeInsets.all(20.0),
                      child: TextField(
                        controller: ctrl_real,
                        decoration: InputDecoration(
                            labelText: "Real price",
                            prefixText: "R\$"),
                        onChanged: change_real,
                        keyboardType: TextInputType.numberWithOptions(
                            decimal: true),
                      )),

                ]
            )
        )
    );
  }
}
1

1 Answers

2
votes

If you check the value received on each method , you will see a comma "," in your decimal values.

void change_real(String text) {
   print("Text  : $text");
}

so every time you try to parse those values , it crashes here:

 double real = double.parse(text);

One way to solve your issue just change the decimalSeparator to '.' , like this:

final ctrl_real = MoneyMaskedTextController(decimalSeparator: ".");
final ctrl_dollar = MoneyMaskedTextController(decimalSeparator: ".");
final ctrl_euro = MoneyMaskedTextController(decimalSeparator: ".");