为什么不使用typeSelected()方法更改变量blueColor的值
bool blueColor = true;
typeSelected(){
setState(() {
blueColor = !blueColor;
print("run:");
print("blueColor:$blueColor"); // changing
});
}
print("blueColor:$blueColor"); //(true) without change
我要更改的地方
GestureDetector(
onTap: () => typeSelected(),
child: Container(
width: 60,
height: 60,
padding: EdgeInsets.all(5),
margin: EdgeInsets.only(left: 5, right: 5),
decoration: BoxDecoration(
color: Colors.brown.shade500,
shape: BoxShape.circle,
border: blueColor ?
border: blueColor ? Border.all(
color:Colors.white70,width:0,style:BorderStyle.solid)
:Border.all(color:Colors.blue,width:2,style: BorderStyle.solid)
),
答案 0 :(得分:-1)
您需要在变量中使用static关键字,以便在更改任何变量的值时,它将反映在整个项目或程序中
static bool blueColor = true;
其余代码相同
typeSelected(){
setState(() {
blueColor = !blueColor;
print("run:");
});
}
print("blueColor:$blueColor");
我希望这会对您有所帮助。