簡體   English   中英

Flutter 如何從 ListTile 更改所選圖塊的背景顏色

[英]Flutter how to change the background color of a selected tile from a ListTile

我正在嘗試從 ListTile 更改所選圖塊的背景。

我搜索並找到了以下兩個帖子,但是沒有一個帖子解決了我的問題。

POST1 POST2

在@CopsOnRoad 的回答的幫助下,我得到了更好的幫助。

使用以下代碼,如果我選擇多個圖塊,則全部保持選中狀態。 如何一次只選擇一個並取消選擇上一個選擇?

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

讓一個變量來保存選定的瓷磚索引。



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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM