Sachu
Sachu

Reputation: 187

How to delete listview item without dismissible

I have a list of listTiles each with text and a button beside it to remove the tile. If the button is pressed the user confirms they want to delete it and the tile is removed. Previously I used a dismissible widget which took a key, but in this case how would I associate the item in the list with the tile I need to remove.

Code (Ignore onTap(), at the bottom the flatButtons are used as the prompt):

ListView.builder(
  itemCount: subGoals.length,
  itemBuilder: (context,index){
    return Card(
      color: Colors.grey[500],
      child: ListTile(
        onTap: (){
          //should make new list with the title of the ListTile's text
        },
        title: Text(subGoals[index],
          style: TextStyle(color: Colors.white,fontSize: 35),
        ),
        trailing: IconButton(
          icon: Icon(Icons.close),
          color: Colors.white,
          splashColor: Colors.red[600], 
          onPressed: (){
            showDialog(
              context: context,
              builder: (BuildContext context){
                return AlertDialog(
                  title: Center(child: Text("Delete List?", style: TextStyle(fontSize: 25))),
                  content: Row(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      FlatButton(
                        child: Text("Yes", style: TextStyle(fontSize: 22)),
                        onPressed: (){
                          if(true){
                             //removeTile method goes here
                          }
                        }
                      ),
                      FlatButton(
                        child: Text("No", style: TextStyle(fontSize: 22)),
                        onPressed: (){
                          Navigator.pop(context);
                        }
                      ),

Upvotes: 0

Views: 830

Answers (1)

J. S.
J. S.

Reputation: 9625

If I understood your problem correctly from the question and comment, then you should have a method that you call from your ListTile to remove the item from your item List. You should be passing the index generated by your ListView to that method and using it to remove the item from the list from the matching position with the index. Then you use setState() to make sure your list is re-rendered on your ListView.

If you don't have this method and you need help writing it, please share more of your code and comment, and I will update this answer.

Upvotes: 2

Related Questions