改变 Flutter 的 FlatButton onPressed 的颜色
change color of the Flutter's FlatButton onPressed
我想在单击按钮时更改按钮的颜色和文本。但它没有改变。我在 setState 中更改我的变量,并使用三元运算符设置文本和颜色。
希望对大家有所帮助。
Container(
padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
alignment: Alignment.bottomCenter,
child: SizedBox(
width: double.infinity, //Full width
height: 40,
child: FlatButton(
child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
onPressed: () {
setState(() {
stopSelling = !stopSelling;
});
},
textColor: Colors.white,
color: stopSelling?Colors.red:Colors.green,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
)
),
),
你的代码是完美的,但我不知道你在哪里声明你的 stopSelling 变量,但我很确定你已经在 build() 方法内声明了 stopSelling 所以你必须在 build() 之外声明 stopSelling 变量class(有状态或无状态)的方法和内部。
Flutter 的生命周期规则是,当 setState() 被调用时,build() 方法会自动调用,它会像以前一样影响你的变量。
试试这个....
Container(
padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
alignment: Alignment.bottomCenter,
child: SizedBox(
width: double.infinity, //Full width
height: 40,
child: stopSelling? FlatButton(
child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
onPressed: () {
setState(() {
stopSelling = !stopSelling;
});
},
textColor: Colors.white,
color: Colors.red,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
):FlatButton(
child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
onPressed: () {
setState(() {
stopSelling = !stopSelling;
});
},
textColor: Colors.white,
color: Colors.green,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
),
)
我想在单击按钮时更改按钮的颜色和文本。但它没有改变。我在 setState 中更改我的变量,并使用三元运算符设置文本和颜色。 希望对大家有所帮助。
Container(
padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
alignment: Alignment.bottomCenter,
child: SizedBox(
width: double.infinity, //Full width
height: 40,
child: FlatButton(
child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
onPressed: () {
setState(() {
stopSelling = !stopSelling;
});
},
textColor: Colors.white,
color: stopSelling?Colors.red:Colors.green,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
)
),
),
你的代码是完美的,但我不知道你在哪里声明你的 stopSelling 变量,但我很确定你已经在 build() 方法内声明了 stopSelling 所以你必须在 build() 之外声明 stopSelling 变量class(有状态或无状态)的方法和内部。
Flutter 的生命周期规则是,当 setState() 被调用时,build() 方法会自动调用,它会像以前一样影响你的变量。
试试这个....
Container(
padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
alignment: Alignment.bottomCenter,
child: SizedBox(
width: double.infinity, //Full width
height: 40,
child: stopSelling? FlatButton(
child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
onPressed: () {
setState(() {
stopSelling = !stopSelling;
});
},
textColor: Colors.white,
color: Colors.red,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
):FlatButton(
child: Text( stopSelling ? "Dejar de vender" : "Empezar a vender",style: TextStyle(fontSize: 20,fontWeight: FontWeight.w300),),
onPressed: () {
setState(() {
stopSelling = !stopSelling;
});
},
textColor: Colors.white,
color: Colors.green,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
),
)