【发布时间】:2020-11-15 02:22:30
【问题描述】:
我正在用颤振构建一个计算器,我试图将带有参数的回调 onclick 函数传递给位于不同文件中的按钮小部件,但是当我单击任何按钮时,它会引发异常,该方法在 null 上被调用.我也不知道如何在 CustomBtn 类中声明一个带参数的函数。
这是我传递函数的主要小部件:
CustomBtn(
btext: '8',
color: Colors.grey[600],
textColor: Colors.grey[50],
onClick: buttonPressed('8'),
),
这是按钮小部件:
class CustomBtn extends StatelessWidget {
final String btext;
final color;
final textColor;
final Function onClick;
CustomBtn({
this.btext,
this.color,
this.textColor,
this.onClick,
});
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Text(
btext,
style: TextStyle(fontSize: 35.0, color: textColor),
),
onPressed: () => onClick(btext),
color: color,
padding: EdgeInsets.fromLTRB(0.0, 24.0, 0.0, 24.0),
);
}
}
【问题讨论】:
标签: function flutter dart callback