我有這個 BMI 計算器,我希望身高不能低于 100(厘米)。為此,我創建了一個TextFormField如下所示的:
TextFormField(
onFieldSubmitted: (value) {
setState((){
_heighController.text= value;
if (int.parse(value) >= 100) {
value = "100";
}
value=_heighController.text;
});
},
inputFormatters: [
LengthLimitingTextInputFormatter(3),
FilteringTextInputFormatter.allow(RegExp(r'^[1-9][0-9]*')),
],
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
style: const TextStyle(fontSize: 16, color: Colors.white),
controller: _heighController,
cursorColor: Colors.white,
decoration: InputDecoration(hintText: "in cm", hintStyle: TextStyle(color: Colors.white)),
),
如您所見,我已經嘗試添加onFieldSubmitted,但這并沒有像我計劃的那樣作業。例如,當我寫 90 時,當我按下鍵盤上的“輸入”按鈕時,它會接受 90。但是,當我使用加號或減號按鈕(見下圖)更新此小部件的狀態時,它會自動變為 100。我希望每次從該欄位中離開“編輯選項”并且輸入低于 100 時它都會變為 100。我如何實作這一點?

uj5u.com熱心網友回復:
您應該在“onFieldSubmitted”函式上更改 _heighController 的值,如下所示:
onFieldSubmitted: (value) {
setState((){
if (int.parse(value) <= 100) {
value = "100";
}
_heighController.text= value;
});
},
您的錯誤是在檢查有效性之前將該值歸因于您的控制器。之后,您將控制器的值設定為函式的引數,在這種情況下這是沒有意義的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520958.html
標籤:安卓扑镖
