2

I am trying to change the background of a selected tile from a ListTile.

I searched and found the following two posts, however non of them worked with my problem.

Post1 Post2

The better I got was with the help from @CopsOnRoad's answere.

With the following code, if I select multiple tiles, all remain select. How to select only one at the time and deselect the previous selected?

The tile index is limited by itemCount: is books.length.

    List<Favorited> books;
    
    // todo: this needs to be changed, has a hard coded value of 200
    List<bool> _selected = List.generate(200, (i) => false); // Pre filled list
    
      @override
      Widget build(BuildContext context) {
        final booksProvider = Provider.of<Model>(context);
    
        return Container(
          child: StreamBuilder(
            stream: booksProvider.getUserFavList('103610812025'),
            builder: (context, AsyncSnapshot<List<Favorited>> snapshot) {
              if (snapshot.hasData) {
                books= snapshot.data.toList();
                return ListView.builder(
                    itemCount: books.length,
                    itemBuilder: (buildContext, index) {
                      return Container(
                        color: _selected[index] ? Colors.amber : Colors.transparent,
                        child: ListTile(
                          title: InkWell(
                              child: Text(snapshot.data[index].title),
                              onTap:() {
                                setState(() {
                                  _selected[index] = !_selected[index];
                                });
                              }),
                          subtitle: Text(snapshot.data[index].name),
                        ),
                      );
                    });
              } else {
                return Text('Fetching');
              }
            }),
        );
Richardd
  • 956
  • 14
  • 27

1 Answers1

7

Let a one variable to save selected tile index.



    List<Favorited> books;
    
    // todo: this needs to be changed, has a hard coded value of 200
    List<bool> _selected = List.generate(200, (i) => false); // Pre filled list
    int selectedIndex;
    
      @override
      Widget build(BuildContext context) {
        final booksProvider = Provider.of<Model>(context);
    
        return Container(
          child: StreamBuilder(
            stream: booksProvider.getUserFavList('103610812025'),
            builder: (context, AsyncSnapshot<List<Favorited>> snapshot) {
              if (snapshot.hasData) {
                books= snapshot.data.toList();
                return ListView.builder(
                    itemCount: books.length,
                    itemBuilder: (buildContext, index) {
                      return Container(
                        color: selectedIndex == index ? Colors.amber : Colors.transparent,
                        child: ListTile(
                          title: InkWell(
                              child: Text(snapshot.data[index].title),
                              onTap:() {
                                setState(() {
                                  selectedIndex = index;
                                });
                              }),
                          subtitle: Text(snapshot.data[index].name),
                        ),
                      );
                    });
              } else {
                return Text('Fetching');
              }
            }),
        );
KuKu
  • 6,654
  • 1
  • 13
  • 25