I am trying to implement value of a simple int variable which hold user age. By clicking on buttons user can increase or decrease the value.
I want to know if there is an easy way dart to prevent user from passing negative value to the variable.
Thanks
CodePudding user response:
You can do the variable check in the Onpressed function of the button and if the value is negative you can either do nothing or tell the user that value has become negative.
onPressed(){
if(value<0){
//do something
}
else {
setState((){});
}
}
CodePudding user response:
If You have two buttons for increasing and decreasing it's better to disable the decrement button when the minimum age is reached for instance 18 or even 0:
DecreaseBtn(
onTap : age >= 0 ?
() {setState(){ age--}} : null
)