【发布时间】: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