如何在Flutter / Dart中更改ListView项目中的背景颜色

时间:2019-08-07 18:30:41

标签: listview flutter flutter-layout

是否可以使用ListView而非ListView.builder查找特定项目的索引号?然后,我想将背景设置为列表中的特定项目。假设每三个项目在背景中为红色。 我的清单长10到15项。

这是我的列表

// List - Words
class WordList extends StatelessWidget {
  final List<WordModal> _wordModal;

  WordList(this._wordModal);

  @override
  Widget build(BuildContext context) {
    return new ListView(
      padding: new EdgeInsets.symmetric(vertical: 8.0),
      children: _buildList(),
    );
  }

  List<WordCard> _buildList() {
    return _wordModal.map((word) => new WordCard(word)).toList();
  }
}

这是我的Card Builder

// Word Card Item
class WordCard extends StatelessWidget {
  final WordModal _genericModal;

  WordCard(this._genericModal);

  @override
  Widget build(BuildContext context) {
    return new Card(
        margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: Container(
          //color: ** want to put background color here **
          child: Column(
            children: <Widget>[
              new ExpansionTile(
                title: Container(
                  child: Column(
                    children: <Widget>[
                      new ListTile(
                        leading: new CircleAvatar(
                          child: Icon(Icons.image, color: Colors.grey), backgroundColor: Colors.white,),
                        title: Text(_genericModal.animalGenus),
                        subtitle: Text(
                          _genericModal.animalSpecies,
                        ),
                      ),
                    ],
                  ),
                ),
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      SizedBox(
                        width: 50,
                      ),
                      Text(
                        _genericModal.animalHabitatLocation,
                        style: (TextStyle(fontStyle: FontStyle.italic)),
                      ),
                    ],
                  ),
                  new Row(
                    children: <Widget>[
                      Spacer(),
                      IconButton(
                          icon: new Icon(
                            Icons.volume_up,
                            size: 28,
                            color: Colors.grey,
                          ),
                          onPressed: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => DetailPage()));
                          }),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ));
  }
}

2 个答案:

答案 0 :(得分:1)

这是一些最小的实现

    int _selectedIndex;
  _onSelected(int index) {

    setState(() {
      _selectedIndex = index;
    });
  }

,然后在列表视图中要更改颜色的小部件

ListView.builder(

          itemCount: querySnapshot.documents.length,
          padding: EdgeInsets.all(8.0),
          itemBuilder: (context, i) {
...
     IconButton(
                          iconSize: 28,
                          icon: Icon(
                            Icons.favorite_border,
                            color: _selectedIndex != null && _selectedIndex == i
                                ? Colors.redAccent
                                : Colors.grey,
                          ),
                          onPressed: () {
                            _onSelected(i);

答案 1 :(得分:0)

是的,您可以尝试在导览itemBuilder函数中对其进行更改,在这里为您提供示例:

class PageTwoState extends State<PageTwo> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemExtent: 250.0,
      itemBuilder: (context, index) => Container(
            padding: EdgeInsets.all(10.0),
            child: Material(
              elevation: 4.0,
              borderRadius: BorderRadius.circular(5.0),
              color: index % 2 == 0 ? Colors.cyan : Colors.deepOrange,
              child: Center(
                child: Text(index.toString()),
              ),
            ),
          ),
    );
  }
}