flutter - How to check length of value in onchange method in flutter
问题描述
Check value of Textfield in onChange method is empty or not if user remove all values from Textfield show in TextView "0".
Mean I want real time change and show change in TextView.
examples:
when user add value its work fine
when user remove all values here is
TextFormField(
keyboardType: TextInputType.number,
textInputAction: TextInputAction.done,
maxLines: 1,
controller: _paidController,
onChanged: (text) {
int newValue = int.parse(text);
setState(() {
if (text.trim().isEmpty) {
_paidController.text = "0";
print("set 0");
unpaid = 0;
paid = 0;
} else {
paid = newValue;
unpaid = grandTotal.toInt() - paid;
}
});
},
onFieldSubmitted: (value) {
int newValue = int.parse(value);
setState(() {
if (value.isEmpty) {
paid = 0;
unpaid = grandTotal.toInt();
}
paid = newValue;
unpaid = grandTotal.toInt() - paid;
});
},
cursorColor: Theme.of(context).primaryColor,
style: TextStyle(
fontFamily: "Light",
color: Theme.of(context).primaryColor,
),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
labelStyle: TextStyle(
color: Color(0XFF17145A),
fontFamily: "Light",
),
labelText: 'Paid Amount',
),
),
解决方案
Use text.trim().isEmpty
to check whether there has value on textField
.
onChanged: (text) {
if (text.trim().isEmpty){
_paidController.text = "0"; // set to zero if no value
unpaid = grandTotal.toInt();
}else{
unpaid = grandTotal.toInt() - paid;
}
}
推荐阅读
- sql - GoogleSQL - 选择如果
- arrays - 如何在 Google 电子表格中使用数组作为 FILTER 函数的输入?
- php - 小写所有没有 foreach 的关联数组键
- jpa - 具有两个属性的 JPA 映射。是否可以在 mappedBy 中包含两列(属性)?
- spring - refresh_token grant_type 错误:需要 UserDetailsService。但我不想指定一个
- uml - 通过关联访问其他类的属性应该是显式的还是隐式的?
- php - ntr设置刀片Laravel中从数据库中选择的值
- c++ - 我找不到什么问题。mojibake, unicode
- angular6 - 如何在角度7中设置下拉值
- android-studio - 我更改了编码文件,但文件内容更改为符号我想恢复文件内容