颤振如何从ListTile更改所选图块的背景颜色

时间:2020-11-03 01:02:14

标签: flutter dart

我正在尝试从ListTile更改所选图块的背景。

我搜索并找到了以下两个帖子,但是没有一个可以解决我的问题。

Post1 Post2

我得到的更好的是@CopsOnRoad的应答者的帮助。

使用以下代码,如果我选择了多个图块,则所有图块均保持选中状态。如何一次只选择一个并取消选择上一个?

图块索引受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');
              }
            }),
        );

1 个答案:

答案 0 :(得分:1)

让一个变量保存选定的图块索引。



    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');
              }
            }),
        );