Reputation: 459
In flutter, I have text form fields that I have disabled and sends prefilled values if no user input (fields disabled). I want to create a button called "Filter" and when the user clicks that button, it enables the fields and allows the user to choose their own values to submit.
Currently, I've figured out a way disable the text fields using enabled:false. I'm not sure how to set all 3 of these formfields to enabled:true when a user clicks a button. Also, currently I've checked for empty input to submit these default values. However, if the filter button is pressed and forms are enabled, I don't want to send prefilled values.
//would like to use these 3 values on submit if fields are disabled
double distance = 10.0;
double length = 1.0;
double results = 10.0;
Widget distanceFromUser() {
return TextFormField(
enabled: false,
decoration: InputDecoration(
labelText: "Distance From User: 10", hintText: '10 miles'),
onSaved: (String value) {
if(value.isEmpty){
distance = 10.0;
}
else {
distance = double.parse(value);
}
},
);
}
Widget lengthOfTrail() {
return TextFormField(
enabled: false,
decoration: InputDecoration(
labelText: "Minimum Length of Trail: 1", hintText: '1 mile'),
onSaved: (String value) {
if(value.isEmpty){
length = 1.0;
}
else {
length = double.parse(value);
}
},
);
}
Widget numOfResults() {
return TextFormField(
enabled: false,
decoration: InputDecoration(
labelText: "Number of results : 10", hintText: 'numOfResults'),
onSaved: (String value) {
if(value.isEmpty){
results = 10.0;
}
else {
results = double.parse(value);
}
},
);
}
Widget submitButton() {
return RaisedButton(
color: Color.fromRGBO(58, 66, 86, 1.0),
child: Text("Find trails near me", style: TextStyle(color: Colors.white)),
onPressed: () async {
formkey.currentState.save();
final trails = await fetchData();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MapScreen(trails, userLat, userLon)),
);
},
);
}
}
Widget Filter(){
//code to re-enable text forms would go here
}
Any assistance would be greatly appreciated, thanks
Upvotes: 1
Views: 8974
Reputation: 27177
create a bool variable to handle disable and enable of all TextFormField and you can use this bool variable to decide which data you have to send prefilled or new.
something like this:
bool _isEnable = false;
Widget distanceFromUser() {
return TextFormField(
enabled: _isEnable, // here use the variable in all the TextFormField
decoration: InputDecoration(
labelText: "Distance From User: 10", hintText: '10 miles'),
onSaved: (String value) {
if(value.isEmpty){
distance = 10.0;
}
else {
distance = double.parse(value);
}
},
);
}
// onpress of filter button
setState(() { _isEnable = true });
// onpress of submit
(){
if(!_isEnable){
//defualt data pass
}else{
//new data you have to pass
}
}
Upvotes: 2