사용자로부터 입력을 받는 위젯이 있습니다. 사용자가 정상적으로 변수를 입력하고 저장 버튼을 누르면 문제가 없습니다. 사용자가 일부 입력을 제공한 후 입력을 삭제하면 내 앱이 중단됩니다. 내 질문은: 사용자가 입력을 삭제하고 내 앱이 이를 올바르게 처리하도록 할 때를 어떻게 이해할 수 있습니까?
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.grey.withOpacity(0.3),
),
child: TextField(
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
onChanged: (value){
setState(() {
_ingredients[index].ingredientAmount=value;
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients[index].ingredientAmount=value;
var flag=0;
for(int i=0;i<=_ingredients.length-1;i++){
if(_ingredients[i].ingredientAmount==null){
flag=-1;
}
}
if(flag==-1){
falseIngredientAmountSelected(context);
}
else{
trueIngredientAmountSelected(context);
}
});
},
decoration: InputDecoration(
border:OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
borderSide: BorderSide(
),
),
hintText: "Miktar Girin",
hintStyle: TextStyle(
color: Colors.black,
fontSize: 14,
),
),
),
height: 50,
),
),
],
),
),
),
);
},
),
),
)
내부에서 onChanged
먼저 확인하십시오 if(value.isNotEmpty)
. false return;
를 value.isNotEmpty
반환하는 경우를 넣어 빈 값으로 수행할 모든 작업을 중단할 수 있습니다 . 또한 사용자에게 입력이 비어 있으면 안된다는 메시지를 표시할 수 있습니다.
나는 또한 사용자가 모든 문자가 아니라 텍스트 입력을 완료했을 때만 확인하거나 작업을 수행하도록 onSubmitted
또는 onEditingComplete
대신 사용하는 것이 좋습니다 onChanged
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다