如何更改 ListTile 的背景颜色?

8

我正在制作设置页面,希望它看起来像iOS设置页面(灰色背景和白色瓷砖)。我尝试添加容器以便为ListTile添加颜色,但总是出现错误。

有人知道如何在这里添加容器吗?

  body: new SingleChildScrollView(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[

               Column(

                children: <Widget>[

                  ListTile(
                    leading: Icon(
                      Icons.person,
                      color: Colors.grey,
                    ),
                    title: Text("Account"),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),

如果我理解正确,您想要类似于这样的东西。所以我不明白您是想更改瓷砖背景还是父容器(列)背景。 - shadowsheep
2个回答

16

只需将您的ListTile包装在一个Container中即可。

2019年的答案

      Container(
            color: Colors.grey[200],
            child: ListTile(
              leading: Icon(
                Icons.person,
                color: Colors.grey,
              ),
              title: Text("Account"),
              trailing: Icon(Icons.keyboard_arrow_right),
            ),
          )

更新的答案(在此PR已于2020年合并https://github.com/flutter/flutter/pull/61347之后)

 ListTile(    tileColor: Colors.grey[200],
              leading: Icon(
                Icons.person,
                color: Colors.grey,
              ),
              title: Text("Account"),
              trailing: Icon(Icons.keyboard_arrow_right),
            )

谢谢回复!我已经尝试过这个方法,但是是否有可能将所有的ListTile都包装在Container中,这样我就不必为每个ListTile重复包装了?当我尝试在Container中使用children: <Widget>[时,会出现“未定义children”的错误。 - FlutterFirebase
两个选项,一是将您的列包装在带有颜色的容器中,二是创建一个StatelessWidget并在其中编写您的项目(容器->ListTile)。 - diegoveloper
为什么ListTile的tileColor属性不能设置颜色? - Chris Nadovich
因为这个答案是来自2019年的 :D - diegoveloper

9

我用一个Material小部件包装了ListTile并设置了颜色。这样可以保留列表瓦片的涟漪效果。

例如:

Material(
        color: Colors.grey[300],
              child: ListTile(
                title: Text('Hello')
              )

    );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接