Reputation: 817
I am new to Flutter and am trying to figure out how to enable my action button only when a rating value has been selected in an AlertDialog. When the "submit" button has been selected with nothing selected, it still changes the value to e.g. 6.0 for some odd reason so an "if" statement may not work comparing it to null as I've tried it previously in the onPressed() method.
This is my method code to show the AlertDialog:
void _showDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text("Pain Rating"),
content: RatingBar(
onRatingChanged: (rating) {
setState(() {
_rating = rating;
print("rating changed: rating = $_rating");
});
},
maxRating: 10,
filledIcon: Icons.radio_button_checked,
emptyIcon: Icons.radio_button_unchecked,
isHalfAllowed: false,
filledColor: Colors.green,
emptyColor: Colors.green,
size: 28,
),
actions: <Widget>[
FlatButton(
child: const Text('SUBMIT'),
onPressed: () {
print("submit pressed: rating = $_rating");
Navigator.of(context).pop();
},
)
],
);
});
Currently when any or no rating has been selected and the "submit" button has been pressed it will close the dialog. I expect the "submit" button to be disabled until a rating has been selected
Upvotes: 0
Views: 1379
Reputation: 7686
This is one suggested way to do it:
var _rating = 0.0
FlatButton(
child: const Text('SUBMIT'),
onPressed: _rating > 0.0
? () {
print("submit pressed: rating = $_rating");
Navigator.of(context).pop();
} : null,
)
If your _rating
gets grater than 0, that means the used changed the value (assuming your rating can't be 0). If so, you enable the button by passing that VoidCallback
... if not, the null
value is called and that disables the button.
If by any reason you need the 0.0 value, then use a bool
variable and change the value to true once you reach the onRatingChanged
callback and then change my logic from _rating > 0.0
to simply your bool variable.
Upvotes: 0