Flutter how to change the background color of a selected tile from a ListTile
Asked Answered
M

1

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');
              }
            }),
        );
Malo answered 3/11, 2020 at 1:2 Comment(0)
D
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');
              }
            }),
        );
Duumvir answered 3/11, 2020 at 1:10 Comment(2)
it worked well! And a so simple solution, I have so much to learn XD. Thanks for the quick answer.Malo
Enjoy your flutter life!Duumvir

© 2022 - 2024 — McMap. All rights reserved.