【问题标题】:Text field update doesn't work with flutter_masked_text文本字段更新不适用于flutter_masked_text
【发布时间】:2019-02-28 00:46:56
【问题描述】:

我正在创建一个应用,其中一项功能是更新文本字段 基于其他文本字段的更改。

这些字段是关于产品价格的:美元、欧元、雷亚尔。

如果用户更改了美元的价格,那么欧元和实际价格也会改变,等等......

问题是,如果我使用普通的 TextEditingController 一切正常,但如果我使用 flutter_masked_text 包中的 MoneyMaskedTextController,更新就会停止。

谁能测试我的代码并回答我为什么用 MoneyMaskedTextController 停止更新?

要进行测试,请不要忘记在您的 pubspec.yaml 中安装 flutter_masked_text: ^0.8.0。

如果我不能使用flutter_masked_text,我怎么能使用掩码和更新文本字段?

谢谢。

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

                ]
            )
        )
    );
  }
}

【问题讨论】:

    标签: dart flutter maskedtextbox


    【解决方案1】:

    如果您检查每种方法收到的值,您会在十进制值中看到一个逗号“,”。

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

    所以每次你尝试解析这些值时,它都会在这里崩溃:

     double real = double.parse(text);
    

    解决问题的一种方法是将小数分隔符更改为“。” ,像这样:

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

    【讨论】:

    • 谢谢你!它对我来说很好。现在我要找到一种让逗号起作用的方法。
    • double.parse前的逗号可以替换成点
    猜你喜欢
    • 2023-04-02
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多