I'm rendering a list fetched from api on the screen. I have a save button for each Item in the list which changes on click. it works fine but when I scroll the list it reverts. I think its because the list builder is building the list again and again but I can't think of a solution to fix icon state on user click. here is the part of code:
....
class NewsListBuilderItemsState extends State<NewsListBuilderItems> {
final _repository = Repository();
bool selected = false;
...
and here is the button:
GestureDetector(
child: Icon(selected
? Icons.bookmark
: Icons.bookmark_border_outlined),
onTap: () {
setState(() {
selected = !selected;
_repository.addToDb(
widget.snapshot.data[widget.index]);
bloc.addToSavedNews(
widget.snapshot, widget.index);
});
},
)