为什么变量的值不改变方法

时间:2019-02-23 21:09:26

标签: dart flutter

为什么不使用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)
                              ),

1 个答案:

答案 0 :(得分:-1)

您需要在变量中使用static关键字,以便在更改任何变量的值时,它将反映在整个项目或程序中

  static bool blueColor = true;

其余代码相同

typeSelected(){
   setState(() {
    blueColor = !blueColor;
    print("run:");
  });
  }
 print("blueColor:$blueColor");

我希望这会对您有所帮助。