因此,我正在尝试为人们单击按钮时创建一种样式,它将更改其颜色。我有一个单独的函数可以在其中创建按钮,因此我将其称为函数来创建它们。但是当我传递一个布尔变量时,主函数似乎并不接受该变量(布尔)。它一直以红色突出显示,并为我提供了此设置。未为类“ _HomeState”定义“按下”设置程序这是创建按钮功能:
class CustomButton extends StatelessWidget {
// define this parameters as you will need them
final Function onPressed;
final IconData icon;
final String text;
final String text2;
final bool pressed;
// define a constructor for the class custom button
CustomButton({this.onPressed, this.icon, this.text, this.text2, this.pressed});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: SizedBox(
width: 90.0,
height: 90.0,
child: RaisedButton(
// remove the default padding the raised button has
padding: EdgeInsets.zero,
onPressed: () {},
color: Colors.grey,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 5, right: 25, bottom: 10),
child: Icon(
icon,
size: 35,
),
),
Padding(
padding: EdgeInsets.only(
top: 0, right: 0, bottom: 20, left: 0),
child: new Text(
text,
),
),
],
),
Padding(
padding: EdgeInsets.only(top: 8, right: 17),
child: Text(
text2,
style: TextStyle(fontSize: 13),
),
),
],
),
),
),
);
}
}
这里是我调用CustomButton()时的主要功能:
CustomButton(
icon: Icons.lightbulb_outline,
text: 'On',
text2: 'Lâmpada 1\nSchuma',
pressed: false,
onPressed: () => (){
setState(() {
pressed = !pressed; <- THis stay highlighted in red
});
},
),
这意味着您正在尝试将pressed值分配给pressed,但未在_HomeState类中定义。
在_HomeState类中,没有按下任何变量。