I am trying to create a Todo App in Flutter, and in my app bar I have a navigation drawer. Inside the navigation drawer I have three different filtering options: 'All', 'Done' and 'Undone'. When the user presses on let's say 'Done', then all the todo items that have been checked as 'Done' should be displayed on the page.
Every single todo item is stored inside a list called 'TodoItemsList'. So far, I am displaying all the todo items using a Listview.builder:
body: ListView.builder(
itemCount: TodoItemsList.length,
itemBuilder: (context, index) {
return getCard(TodoItemsList[index]);
},
),
And so far, I have managed to add all the checked items in a list called 'filterDone', and all the unchecked items in a list called 'filterUndone'. I also have a filter variable that changes value whenever a user presses on 'All', 'Done', or 'Undone':
My problem: I don't know how to actually make the filtering buttons in the navigation drawer work. I tried to add if/else statements that checks the value of the filter variable to my Listview.builder, in order to return the correct list:
body: ListView.builder(
itemCount: TodoItemsList.length,
itemBuilder: (context, index) {
if(filter == 'All') {
return getCard(TodoItemsList[index]);
} else if (filter == 'Done') {
return getCard(filterDone[index]);
} else if (filter == 'Undone') {
return getCard(filterUndone[index]);
} else {
return Container();
}
},
),
However, this isn't working and I understand that it is a dead code because I have no setState or rebuild function anywhere, but I don't know where to add it. I tried to wrap the return getCard() inside a setState function like so:
if(filter == 'All') {
setState(() {
return getCard(TodoItemsList[index]);
});
.. but it just returns the error message: The return type 'Widget' isn't a 'void', as required by the closure's context.
I'd appreciate the help and I can add more code to my question if anything is unclear. Right now my code is not modular enough to paste everything into the question.