Change background color of ListTile upon selection in Flutter

前端 未结 12 1736
闹比i
闹比i 2020-12-15 03:10

I\'ve made a ListView in Flutter, but now I have some ListTiles in this ListView that can be selected. Upon selection, I want the back

相关标签:
12条回答
  • 2020-12-15 03:38

    This is no more pain!

    Now you can use tileColor and selectedTileColor property of ListTile widget to achieve it.

    Have a look at this Issue #61347 which got merged into master.

    0 讨论(0)
  • 2020-12-15 03:39

    It's not ListTile that has the style property. But ListTileTheme. ListTileTheme is an inheritedWidget. And like others, it's used to pass down data (such as theme here).

    To use it, you have to wrap any widget above your ListTile with a ListTileTheme containing the desired values.

    ListTile will then theme itself depending on the closest ListTileTheme instance.

    0 讨论(0)
  • 2020-12-15 03:39

    If you also need an onTap listener with a ripple effect, you can use Ink:

    ListView(
      children: [
        Ink(
          color: Colors.lightGreen,
          child: ListTile(
            title: Text('With lightGreen background'),
            onTap() { },
          ),
        ),
      ],
    );
    

    0 讨论(0)
  • 2020-12-15 03:42

    Unfortunately, ListTile doesn't have background-color property. Hence, we have to simply wrap the ListTile widget into a Container/Card widget and then we can use its color property. Further, We have to provide SizedBox widget with some height to separate the same colored ListTiles.

    I am sharing that worked for me :)

    I hope it will definitely help you.

    Screenshot: see how it works

                return 
                  ListView(
                    children: snapshot.data.documents.map((doc) {
                      return Column(children: [
                        Card(
                          color: Colors.grey[200],
                           child: ListTile(
                          leading: Icon(Icons.person),
                          title: Text(doc.data['coursename'], style: TextStyle(fontSize: 22),),
                          subtitle: Text('Price: ${doc.data['price']}'),
                          trailing: IconButton(
                            icon: Icon(Icons.delete),
                            onPressed: () async {
                              await Firestore.instance
                                  .collection('courselist')
                                  .document(doc.documentID)
                                  .delete();
                            },
                          ),
                      ),
                        ),
                     SizedBox(height: 2,)
                      ],);
                    }).toList(),[enter image description here][1]
                  );
    
    0 讨论(0)
  • 2020-12-15 03:42

    I was able to change the Background Color of ListTile by making it a child of Container Widget and adding color to the Container Widget.

    Here drawerItem is the model class which holds the isSelected value. Color of background depends on isSelected value.

    Note: For unselected items keep the color Transparent so you will still get the ripple effect.

     for (var i = 0; i < drawerItems.length; i++) {
          var drawerItem = drawerItems[i];
          drawerOptions.add(new Container(
            color: drawerItem.isSelected
                ? Colors.orangeAccent
                : Colors.transparent,
            child: new ListTile(
              title: new Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[Text(drawerItem.title), drawerItem.count],
              ),
              leading: SvgPicture.asset(
                drawerItem.icon,
                width: 34,
                height: 34,
              ),
              onTap: () {
                _handleNavigation(i);
              },
              selected: drawerItem.isSelected,
            ),
          ));
        }
    

    0 讨论(0)
  • 2020-12-15 03:50

    Wrap ListTile in an Ink.

    Ink(
      color: isSelected ? Colors.blue : Colors.transparent,
      child: ListTile(title: Text('hello')),
    )
    
    0 讨论(0)
提交回复
热议问题