Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter onPressed of a Button on a ListView gets called automatically (when it becomes visible). Is this a bug?

Tags:

flutter

dart

As the title mentions onPressed of a Button on a ListView gets called automatically

I currently have something like this (this is a rough sketch of the code). Basically whenever there is a button in each row and whenever a button gets displayed on the screen its onClick is called. Not sure if this is a bug in flutter or I am doing something wrong any suggestions ?

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

    }

}

and the list view side is this

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

Now when ever a row gets visible the onclick of the button is called.

like image 881
MistyD Avatar asked May 18 '18 02:05

MistyD


People also ask

How do you avoid render overflow in flutter?

Solution : The solution to resolve this overflow error is to make your entire widget or in our case the Column scrollable. We can do that by wrapping our Column inside a SingleChildScrollView. Also, wrap the SingleChildScrollView with Center so that the entire UI is centered.

How do I fix RenderFlex in flutter?

To fix A RenderFlex overflowed by pixels you just have to Wrap Image in flex widget Expanded , height available is calculated then shared among Expanded (as constraints) and Image is resized to fit inside Expanded constraints.

How do I show ListView in flutter?

In the above code, we have ListViewBuilder class which is a stateless class. It returns a new Scaffold which consists of appBar and body. In the body, we have ListView. builder with itemcount 5 and itemBuilder which will create a new widget again and again up to 5 times because we have itemCount=5.


1 Answers

 onPressed: inviteClicked(employee),

should be

 onPressed: () => inviteClicked(employee),

to pass a callback function instead of the result of a function call.

like image 92
Günter Zöchbauer Avatar answered Oct 07 '22 10:10

Günter Zöchbauer