正如标题中提到的那样,ListView 上的按钮的 onPressed
会被自动调用
我目前有这样的东西(这是代码的粗略草图)。 基本上,只要每行中有一个按钮,并且每当一个按钮显示在屏幕上,它的 onClick 就会被调用。不确定这是 flutter 中的错误还是我做错了什么建议?
class ModelEmployeeRow extends StatelessWidget
{
dynamic getInviteButton(String text, {var lambda,var borderRadius,var height})
{
final skillTextStyle = baseTextStyle.copyWith(
color: Colors.white,//const Color(0xffb6b2df),
fontSize: 11.0,
fontWeight: FontWeight.w200
);
var container = new Container(
alignment: Alignment.center,
margin:EdgeInsets.fromLTRB(0.0,0.0,100.0,0.0),
padding:EdgeInsets.fromLTRB(0.0,5.0,0.0,5.0),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(new Radius.circular(4.0)),
color: Colors.green
),
child: new Text(text, style:skillTextStyle),
);
var button = new FlatButton(
onPressed: inviteClicked(employee),
child: container
);
return button;
}
@override
Widget build(BuildContext context)
{
var stacked = new Stack(
children: <Widget>
[
//mainContainer,
getInviteButton("Test"),
employeeThumbnail,
],
);
return new Container(
child: stacked,
);
}
}
ListView 是这样的
var emplyeeListView = new ListView.builder(
itemCount: employeeListShared.length,
padding: new EdgeInsets.symmetric(vertical: 16.0),
itemBuilder: (context, index) {
return new ModelEmployeeRow(employeeListShared[index]);
},
);
现在,只要一行可见,就会调用按钮的 onclick。
最佳答案
onPressed: inviteClicked(employee),
应该是
onPressed: () => inviteClicked(employee),
传递回调函数而不是函数调用的结果。
关于dart - 自动调用 ListView 上的按钮的 Flutter onPressed(当它变得可见时)。这是一个错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50402913/