【发布时间】:2019-12-12 03:26:35
【问题描述】:
我想遍历一个非常相似的按钮列表,但我不确定最好的方法是什么,因为每个按钮都需要一个传递单个参数的 onPressed 函数。
Column(
children: buttons
.map(
(item) => Row(
children: <Widget>[
myButton(item[0], item[2]),
myButton(item[1], item[2]),
],
),
)
.toList(),
),
class MyButton extends StatelessWidget {
MyButton(this.abool, this.onPressed);
final bool abool;
final Function onPressed;
@override
Widget build(BuildContext context) {
return RawMaterialButton(
child:
Image.asset('images/button.png'),
onPressed: onPressed,
);
当然,我在将不同的onPressed 函数存储在List 数组中时会遇到问题。在列中遍历类似按钮列表的最佳方法是什么?
【问题讨论】:
-
您目前在
buttons变量中存储了什么? -
@Marcel 我想存储 onPressed 函数。
-
啊,我明白了。
myButton的论据是什么?标签和onPressed回调?按钮上的标签应该是什么? -
@Marcel 我已经用按钮功能更新了问题。
onPressed应该是类似(val) => aFunction(val)的函数。第一项就像文本、布尔值或其他。 -
@Marcel 也许有更好的方法来做到这一点?我的意思是为 onPressed 生成一个具有不同功能的类似按钮列表?