我有以下代码:
@override
Widget build(BuildContext context) {
return new Container(
height: 72.0, // in logical pixels
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
decoration: new BoxDecoration(color: Colors.white),
// Row is a horizontal, linear layout.
child: new MaterialButton(
child: new Text(
_sprinkler.name,
style: new TextStyle(color: Colors.white)
),
splashColor: Colors.blueAccent,
color: Colors.blue[800],
onPressed: () {
print("onTap(): tapped" + _sprinkler.name);
},
),
);
}
onPressed(),我想改变按钮样式 - 表示喷水器活动 .
因此,我需要访问MaterialButton Widget本身 .
但是如何从回调中访问它?
非常感谢,对n00b问题感到抱歉,我是Dart和Flutter的新手;)
3 回答
您可以将某些属性变为变量 . 然后,您可以在
onPressed()
中调用setState()
来更改属性变量 .此示例显示如何使用此方法更改按钮的文本颜色:
您可能想要使用StatefulWidget,如下所示:
感谢您的意见 . 正确的解决方案是你推荐的,看起来像这样: