Positioned(
top: 12,
right: 12,
child: SizedBox(
height: 25,
width: 25,
child: CircleAvatar(
backgroundColor: isGolden? Colors.amber :Colors.black87,
child: IconButton(
padding: EdgeInsets.zero,
icon: Icon(Icons.star,),
color: Color(0xFFFFFFD5),
onPressed: (){
futureMessage = PostStoryFavoriteCreateDelete(snapshot.data!.data![index].id.toString());
ScaffoldMessenger.of(context).showSnackBar(SnackBar(elevation: 20,content: FutureBuilder(
future: futureMessage,
builder: (context, snapshot){
if (snapshot.hasData) {
return Text("SuccessFully Updated");
} else if (snapshot.hasError) {
return Text("Failed To Update");
}
return const Text("Processing...");
},
),));
setState(() {
isGolden == true ?false:true;
});
},
),
),
),
)
itemBuilder: (BuildContext context, index){
isGolden = snapshot.data!.data![index].favoriteStories!.length>0 ? true :false;
onPressed() 不更新我的状态,即改变我的星星的颜色,我必须再次获取 api 才能更新我的星星的颜色。 或者我必须刷新我的页面。
试试这个:
setState(() {
isGolden == true ?isGolden = false:isGolden = true;
}
你只是在检查它是真还是假,你没有改变它的价值
试试这个
setState(() => isGolden =! isGolden);