[英]Flutter how to change the background color of a selected tile from a ListTile
[英]How to change the selected ListTile Icon and Text color in Flutter?
默认情况下,当在抽屉中选择一个项目时,它当前是蓝色的(见下图),这个颜色属性是从哪里来的,是否可以直接更改它? 我正在使用ListTile()
; 对于背景,有一个名为selectedTileColor
的属性,但是我找不到前景的任何东西。
int _selectedDestination = 0;
ListTile(
leading: const Icon(Icons.home),
title: const Text('Home'),
selected: _selectedDestination == 0,
onTap: () => selectDestination(0),
),
void selectDestination(int index) {
setState(() {
_selectedDestination = index;
});
}
您可以通过将 ListTile 包装在 Ink 中来实现:
Ink(
color: isSelected ? Colors.green : Colors.transparent,
child: ListTile(title: Text('hello')),
)
或使用 SelectedTileColor
ListTile(
selected: true,
selectedTileColor: Colors.brown,
tileColor: Colors.black,
title: const Text(
'Title',
style: TextStyle(color: Colors.orange),
),
)
试试这个,将选定的图标和文本颜色更改为红色或您想要的每种颜色:
ListTile(
leading: Icon(Icons.home , color: isSelected ? Colors.red : Colors.transparent),
title: const Text('Home' , style: TextStyle(color: isSelected ? Colors.red : Colors.transparent)),
selected: _selectedDestination == 0,
onTap: () => selectDestination(0),
),
我想我根据文档找到了最有效的方法。 因此,您只需将ListTile()
包装在 ListTileTheme( ListTileTheme()
中,然后使用selectedColor:
属性,然后将 ListTile 放入child:
属性中,就像这样child: ListTile()
最后我们得到这样的东西:
ListTileTheme(
selectedColor: Colors.red,
child: ListTile(
leading: const Icon(Icons.add),
title: const Text('New Item'),
selected: _selectedDestination == 1,
onTap: () => selectDestination(1),
)),
查看文档以获取更多信息: ListTileTheme
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.