我已经在颤振中做了一个ListView
,但是现在我在这个ListView
中有了一些可以选择的ListTiles
。在选择时,我希望背景颜色更改为我选择的颜色。我不知道该怎么做。在医生们中,他们提到ListTile
有一个属性style
。但是,当我尝试添加它时(如下面代码中的第三行),这个style
属性在下面会得到一条平淡无奇的红线,编译器告诉我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,),
);
}
发布于 2018-03-17 01:29:19
具有ListTile
属性的不是style
。但是ListTileTheme
。ListTileTheme
是inheritedWidget。和其他人一样,它也用来传递数据(比如这里的主题)。
要使用它,您必须将任何小部件封装在(您的ListTile )之上的任何小部件上,其中包含一个包含所需值的ListTileTheme
。
然后,ListTile
将根据最近的ListTileTheme
实例来主题本身。
发布于 2019-10-24 15:31:33
截图:
简短答覆:
ListTile(
tileColor: isSelected ? Colors.blue : null,
)
完整法典:
// You can also use `Map` but for the sake of simplicity I'm using two separate `List`.
final List<int> _list = List.generate(20, (i) => i);
final List<bool> _selected = List.generate(20, (i) => false); // Fill it with false initially
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemBuilder: (_, i) {
return ListTile(
tileColor: _selected[i] ? Colors.blue : null, // If current item is selected show blue color
title: Text('Item ${_list[i]}'),
onTap: () => setState(() => _selected[i] = !_selected[i]), // Reverse bool value
);
},
),
);
}
发布于 2018-07-06 20:14:24
我能够在ListTile容器中使用BoxDecoration更改的背景色。
ListView (
children: <Widget>[
new Container (
decoration: new BoxDecoration (
color: Colors.red
),
child: new ListTile (
leading: const Icon(Icons.euro_symbol),
title: Text('250,00')
)
)
]
)
https://stackoverflow.com/questions/49331612
复制相似问题