【问题标题】:Flutter how to change the background color of a selected tile from a ListTile颤动如何从 ListTile 更改所选图块的背景颜色
【发布时间】:2021-02-15 16:32:29
【问题描述】:

我正在尝试从 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');
              }
            }),
        );

【问题讨论】:

    标签: flutter dart


    【解决方案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');
                  }
                }),
            );
    

    【讨论】:

    • 效果很好!一个如此简单的解决方案,我有很多东西要学 XD。感谢您的快速回答。
    • 享受你的扑腾生活!
    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2021-05-26
    • 1970-01-01
    • 2015-11-01
    • 2021-04-27
    • 1970-01-01
    • 2021-03-31
    • 2019-12-28
    相关资源
    最近更新 更多