我有一个TextField,它只允许两个十进制数字。
TextField(
controller: extraAmountController,
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'^d+.?,?d{0,2}'),
),
],
keyboardType:
TextInputType.numberWithOptions(
decimal: true, signed: true),
onChanged: (amount) {
extraAmount = double.parse(amount)
.toStringAsFixed(2);
print(amount.length);
print(extraAmount);
},
onSubmitted: (amount) {
amount.length == 0
? extraAmountController.text =
extraAmount = "00.00"
: extraAmount = double.parse(amount)
.toStringAsFixed(2);
print(amount.length);
print(extraAmount);
},
textAlignVertical:
TextAlignVertical.center,
cursorColor: Colors.black45,
decoration: InputDecoration(
hintText: usersList.first.extraAmount,
contentPadding:
const EdgeInsets.only(left: 10),
suffixIcon: Icon(
Icons.business_center,
color: Colors.grey,
),
border: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide:
BorderSide(color: Colors.grey),
),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide:
BorderSide(color: Colors.grey),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide:
BorderSide(color: Colors.grey),
),
disabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide:
BorderSide(color: Colors.grey),
),
errorBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide:
BorderSide(color: Colors.grey),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide:
BorderSide(color: Colors.grey),
),
),
),
现在的问题是我们在两个小数点后给出的文本。在给出十进制值之后,我们仍然可以进行键入。但是,它不会对文本字段做任何改变。但是,如果我们按退格键,它将删除输入在两个小数点后的值。只有这样,我们才能编辑文本字段中的实际值。有办法解决这个问题吗?
如果你不想再编辑它,你可以使用enabled
参数并将其设置为true。
new TextField(
enabled: false,
...
...
)