我正在尝试控制一个RatingBar,但我在这方面有点困难。问题是评级栏没有更新。下面是我的代码:
Widget _buildBody(videoid) {
double rating = 3;
return Container(
color: Colors.red,
child: Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(3, 7, 0, 0),
child: Align(
alignment: Alignment.topLeft,
child: RatingBarIndicator(
rating: rating,
itemBuilder: (context, index) => InkWell(
onTap: (){
setState(() {
rating=index.toDouble()+1;
});
},
child: Icon(
Icons.star,
color: Colors.amber,
),
),
itemCount: 5,
itemSize: 31.0,
direction: Axis.horizontal,
),
),
),
Align(
alignment: Alignment.topRight,
child: TextButton(
onPressed: () {
letuservoting = true;
setState(() {
rating = 0;
israting = false;
});
dislike(idovvideo, _rating);
},
child: Text(
"Clear",
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
],
),
);
}我将等级设置为3,然后在明文按钮上我想将其设置为0,然后当用户点击2时,它应该显示2星。目前,无论我做什么,它都只显示这3颗星。我怎么才能修复它呢?
发布于 2021-04-29 01:03:33
最好不要在小部件内部赋值变量,因为它会不断重建并重新赋值变量,在你的例子中,rating变量会一直重新赋值为3。试着把它放在你的类中,在build方法之外。
https://stackoverflow.com/questions/67304203
复制相似问题