在 Flutter 中选择时更改 ListTile 的背景颜色 [英] Change background color of ListTile upon selection in Flutter
问题描述
我已经在 Flutter 中创建了一个 ListView
,但是现在我在这个 ListView
中有一些可以选择的 ListTiles
.选择后,我希望背景颜色更改为我选择的颜色.我不知道该怎么做.在文档中,他们提到ListTile
有一个属性style
.但是,当我尝试添加它时(如下面代码的倒数第三行),这个 style
属性在下面有一条波浪状的红线,编译器告诉我 命名参数 'style' 未定义
.
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 background color to change to a color of my choice. I don't know how to do that.
In the docs they mention that a ListTile
has a property style
. However, when I try to add that (as in third last line in the code below), this style
property gets a squiggly red line underneath and the compiler tells me that The named parameter 'style' isn't defined
.
Widget _buildRow(String string){
return new ListTile(
title: new Text(string),
onTap: () => setState(() => toggleSelection(string)),
selected: selectedFriends.contains(string),
style: new ListTileTheme(selectedColor: Colors.white,),
);
}
推荐答案
不是 ListTile
具有 style
属性.但是ListTileTheme
.ListTileTheme
是一个继承的Widget.和其他人一样,它用于传递向下数据(例如这里的主题).
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).
要使用它,您必须使用包含所需值的 ListTileTheme
包裹 上方 ListTile 的任何小部件.
To use it, you have to wrap any widget above your ListTile with a ListTileTheme
containing the desired values.
ListTile
然后将根据最近的 ListTileTheme
实例来主题化自己.
ListTile
will then theme itself depending on the closest ListTileTheme
instance.
这篇关于在 Flutter 中选择时更改 ListTile 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!