颤振如何从ListTile更改所选图块的背景颜色 [英] Flutter how to change the background color of a selected tile from a ListTile

查看:82
本文介绍了颤振如何从ListTile更改所选图块的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

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

推荐答案

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

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

这篇关于颤振如何从ListTile更改所选图块的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆