gpt4 book ai didi

dart - 如何在 Dart/Flutter 中从它自己的回调 (onPressed) 中访问 Widget

转载 作者:IT老高 更新时间:2023-10-28 12:43:58 32 4
gpt4 key购买 nike

我有以下代码:

  @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(),我想改变 Buttons 样式 - 代表喷水器事件。

因此,我需要访问 MaterialButton 小部件本身。

但是如何从回调中访问它?

提前非常感谢,对于 n00b 问题,我很抱歉,我是 Dart 和 Flutter 的新手 ;)

最佳答案

您可以将某些属性设为变量。然后,您可以在 onPressed() 中调用 setState() 来更改属性变量。
此示例显示如何使用此方法更改按钮的文本颜色:

  Color textColor = Colors.white;
@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: textColor)
),
splashColor: Colors.blueAccent,
color: Colors.blue[800],
onPressed: () {
this.setState(() {
textColor = Colors.red;
})
},
),
);
}

关于dart - 如何在 Dart/Flutter 中从它自己的回调 (onPressed) 中访问 Widget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45470806/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com