android - setState 不适用于 Flutter 中的下拉按钮
问题描述
当我从下拉列表中选择数量时,我正在根据数量计算总计,但总量Textformfield
保持不变,当我调试代码时,我在setState
方法中获得了更新的数量,但没有反映在 UI 上,而且我也在更新总计从该方法中的另一种方法,它正在工作。
class _ProductDetailsPageState extends State<ProductDetailsPage> {
List _quantity = ["1", "2", "3", "4", "5"];
List<DropdownMenuItem<String>> _dropDownQuantity;
String _currentQuantity;
String _productprice;
double _taxrateamt;
double _taxper = 0.0;
String _producttotal;
int _productqty;
double _discount = 3.0;
String _productname;
List<DropdownMenuItem<String>> getDropDownQuantity() {
List<DropdownMenuItem<String>> items = new List();
for (String city in _quantity) {
items.add(new DropdownMenuItem(
value: city,
child: new Text(
city,
style:
TextStyle(color: MyColors.colorPrimary, fontFamily: 'semibold'),
)));
}
return items;
}
@override
void initState() {
super.initState();
_dropDownQuantity = getDropDownQuantity();
_currentQuantity = _dropDownQuantity[0].value;
}
Container rateAndTotal( String value) {
return Container(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
flex: 1,
child: Container(
child: TextFormField(
style: TextStyle(
fontFamily: 'medium', color: MyColors.colorPrimaryDark),
keyboardType: TextInputType.number,
enabled: false,
textInputAction: TextInputAction.done,
initialValue: '${value}',
decoration: InputDecoration(
border: InputBorder.none, hintText: '$value'),),),),],),],),);}
Container quantity() {
return Container(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
flex:1,
child: Container(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: _currentQuantity,
items: _dropDownQuantity,
onChanged: changedDropDownQuantity,),)),),],),],),);}
@override
Widget build(BuildContext context) {
MediaQueryData queryData;
queryData = MediaQuery.of(context);
double width = queryData.size.width;
double height = queryData.size.height;
return new Container(
height: double.infinity,
decoration: BoxDecoration(color: Colors.grey[200]),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(height * 0.020),
child: Column(
children: <Widget>[
quantity(),
rateAndTotal( '${_productprice}'),
],
),
),
),
);
}
void changedDropDownQuantity(String selected_quantity) {
setState(() {
_currentQuantity = selected_quantity;
// _producttotal=getProductTotal(_productprice,_currentQuantity);
});
setState(() {
_producttotal=getProductTotal(_productprice,_currentQuantity);
});
}
String getProductTotal(String productprice, String _currentQuantity) {
try {
double sprice = 0.0;
double total = double.parse(productprice);
double taxrate = _taxrateamt;
double qty = double.parse(_currentQuantity);
return _producttotal =
(((total + taxrate + sprice) - _discount) * qty).toString();
} catch (e) {
debugPrint('PRODUCT TOTAl EXC.:--${e}');
return _producttotal = productprice;
}
}
}
解决方案
要解决此问题,请在 TextFormField 中替换以下行:
initialValue: '$value',
和:
controller: TextEditingController(text: value),
或者更好:
// Define a controller in the State
class _ProductDetailsPageState extends State<ProductDetailsPage> {
TextEditingController _controller = TextEditingController(text: value);
...
// Then bind that controller to your TextFormField, replacing initialValue
child: TextFormField(
controller: _controller,
...
// Then change the .text property of that controller when you want it to update
setState(() => _controller.text = value);
initialValue
仅用于设置第一次初始化TextFormField
时的第一个值,并通过重建保留,因此覆盖它永远不会导致文本更改。State
State
TextEditingController
每次完全覆盖或定义 aTextEditingController
并更改.text
属性将正确地将更改传递给State
字段并更新您的值。
推荐阅读
- php - php如何在字符串数组中添加反斜杠
- python - 为 python 3.8.5 安装 pip 包管理器时出错
- php - 在php codeigniter中按下后退按钮时如何停止将登录用户重定向到登录页面
- java - Resteasy返回404
- python - Kickstart Round C 2020 倒计时问题 - 查找连续子数组的数量
- swift - 无法从 Xcode 11.7 构建到 iOS 14 设备
- php - PHP正则表达式以给定数字开始和结束
- linear-programming - 在 OPL 中查找集合的下一个数字
- angular - 角度错误消息:无法绑定到“formGroup”,因为它不是“form”的已知属性
- python - django_braces 的任何替代品?