Search code examples
flutterlistview

show List Tile after clicking on button flutter


I have an Elevatebutton where I want to do that when I click on it, I can see a listTile displayed and after I click on another button the listTile will disappear. I have the code written like this:

ElevatedButton(
                        onPressed: ( ) {
                          setState(() {
                            ListTile(
                              leading: Icon(Icons.add),
                              title: Text('GFG title',textScaleFactor: 1.5,),
                              trailing: Icon(Icons.done),
                            );
                          });
                        },
                        child: Text('button1'),
                        style: ElevatedButton.styleFrom(shape: StadiumBorder()),
                      ),

but even when I click on the button I do not see the listTile. How do I fix this? Can someone help me?


Solution

  • When pressing the button you can't just write a widget and it will appear. One thing you can do is to already have the ListTile in the view but it's invisible. Then when you press the button it becomes visible. Here's an example:

    bool _isVisible = false;
    
    ...
    ...
    
    Column(
            children: [ElevatedButton(
              onPressed: () {
                setState(() {
                  _isVisible = true;
                });
              },
              child: Text('button1'),
              style: ElevatedButton.styleFrom(shape: StadiumBorder()),
            ),
    
              Visibility(
                  visible: _isVisible,
                  child: ListTile(
                    leading: Icon(Icons.add),
                    title: Text('GFG title', textScaleFactor: 1.5,),
                    trailing: Icon(Icons.done),
                  ))
            ],
          )