在ListView上按下按钮时会自动调用(当它变为可见时)。这是一个错误吗?

时间:2018-05-18 02:21:45

标签: dart flutter

标题提到ListView上的Button onPressed被自动调用

我目前有类似的东西(这是代码的粗略草图)。 基本上每当每行都有一个按钮时,每当屏幕上显示一个按钮时,就会调用onClick。不确定这是一个颤动的错误还是我做错了什么建议?

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,
            );

    }

}

,列表视图一边是

 var emplyeeListView = new ListView.builder(
            itemCount: employeeListShared.length,
            padding: new EdgeInsets.symmetric(vertical: 16.0),
            itemBuilder: (context, index) {
                return new ModelEmployeeRow(employeeListShared[index]);
            },
            );

现在,只要有一行可见,就会调用该按钮的onclick。

1 个答案:

答案 0 :(得分:14)

 onPressed: inviteClicked(employee),

应该是

 onPressed: () => inviteClicked(employee),

传递回调函数而不是函数调用的结果。