Flutter IconButton 没有设置 onPress 状态
Flutter IconButton not setting state onPress
我正在尝试将我的 IconButton 设为 return 带有文本值的 setState,然后我们在小部件树中进一步呈现该值,但似乎没有应用 setState
IconButton _buildRating(message, color, icon) {
return IconButton(
enableFeedback: true,
iconSize: MediaQuery.of(context).size.width * .2,
onPressed: () {
switch (message) {
case 'Very Dissatisfied':
rating = 1;
ratingDesc = 'Terrible';
break;
case 'Dissatisfied':
rating = 2;
ratingDesc = 'Bad';
break;
case 'Neutral':
rating = 3;
ratingDesc = 'Ok, but had some issues';
break;
case 'Satisfied':
rating = 4;
ratingDesc = 'Good, not great';
break;
case 'Very Satisfied':
rating = 5;
ratingDesc = 'Everything was great!';
break;
}
},
tooltip: message,
icon: Icon(icon, color: color),
);
}
Text _buildRatingDesc() {
return Text(ratingDesc, style: Theme.of(context).textTheme.bodyText2);
}
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildRating('Very Dissatisfied', Colors.red,
Icons.sentiment_very_dissatisfied),
_buildRating('Dissatisfied', Colors.amber,
Icons.sentiment_dissatisfied),
_buildRating(
'Neutral', Colors.blue, Icons.sentiment_neutral),
_buildRating('Satisfied', Colors.lightGreen,
Icons.sentiment_satisfied),
_buildRating('Very Ssatisfied', Colors.green,
Icons.sentiment_very_satisfied),
],
)),
_buildRatingDesc(),
将您的函数包装在 setState(() {})
中
onPressed: () {
setState(() {
switch (message) {
case 'Very Dissatisfied':
rating = 1;
ratingDesc = 'Terrible';
break;
case 'Dissatisfied':
rating = 2;
ratingDesc = 'Bad';
break;
case 'Neutral':
rating = 3;
ratingDesc = 'Ok, but had some issues';
break;
case 'Satisfied':
rating = 4;
ratingDesc = 'Good, not great';
break;
case 'Very Satisfied':
rating = 5;
ratingDesc = 'Everything was great!';
break;
}})
},
我正在尝试将我的 IconButton 设为 return 带有文本值的 setState,然后我们在小部件树中进一步呈现该值,但似乎没有应用 setState
IconButton _buildRating(message, color, icon) {
return IconButton(
enableFeedback: true,
iconSize: MediaQuery.of(context).size.width * .2,
onPressed: () {
switch (message) {
case 'Very Dissatisfied':
rating = 1;
ratingDesc = 'Terrible';
break;
case 'Dissatisfied':
rating = 2;
ratingDesc = 'Bad';
break;
case 'Neutral':
rating = 3;
ratingDesc = 'Ok, but had some issues';
break;
case 'Satisfied':
rating = 4;
ratingDesc = 'Good, not great';
break;
case 'Very Satisfied':
rating = 5;
ratingDesc = 'Everything was great!';
break;
}
},
tooltip: message,
icon: Icon(icon, color: color),
);
}
Text _buildRatingDesc() {
return Text(ratingDesc, style: Theme.of(context).textTheme.bodyText2);
}
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildRating('Very Dissatisfied', Colors.red,
Icons.sentiment_very_dissatisfied),
_buildRating('Dissatisfied', Colors.amber,
Icons.sentiment_dissatisfied),
_buildRating(
'Neutral', Colors.blue, Icons.sentiment_neutral),
_buildRating('Satisfied', Colors.lightGreen,
Icons.sentiment_satisfied),
_buildRating('Very Ssatisfied', Colors.green,
Icons.sentiment_very_satisfied),
],
)),
_buildRatingDesc(),
将您的函数包装在 setState(() {})
onPressed: () {
setState(() {
switch (message) {
case 'Very Dissatisfied':
rating = 1;
ratingDesc = 'Terrible';
break;
case 'Dissatisfied':
rating = 2;
ratingDesc = 'Bad';
break;
case 'Neutral':
rating = 3;
ratingDesc = 'Ok, but had some issues';
break;
case 'Satisfied':
rating = 4;
ratingDesc = 'Good, not great';
break;
case 'Very Satisfied':
rating = 5;
ratingDesc = 'Everything was great!';
break;
}})
},