简体   繁体   English

如何更改 Flutter 中选定的 ListTile 图标和文本颜色?

[英]How to change the selected ListTile Icon and Text color in Flutter?

By default when an item is selected in the drawer it is currently blue (see the picture below), where is this color property coming from, and is it possible to change it directly?默认情况下,当在抽屉中选择一个项目时,它当前是蓝色的(见下图),这个颜色属性是从哪里来的,是否可以直接更改它? I am using ListTile() ;我正在使用ListTile() for the background there is a property called selectedTileColor but, I cannot find anything for the foreground.对于背景,有一个名为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;
   });
 }

抽屉

you can do by wrapping ListTile in an Ink as:您可以通过将 ListTile 包装在 Ink 中来实现:

Ink(
  color: isSelected ? Colors.green : Colors.transparent,
  child: ListTile(title: Text('hello')),
)

OR using SelectedTileColor或使用 SelectedTileColor

ListTile(
  selected: true,
  selectedTileColor: Colors.brown,
  tileColor: Colors.black,
  title: const Text(
  'Title',
 style: TextStyle(color: Colors.orange),
 ),
)

Try this to change selected icon and text color to red or every color you want:试试这个,将选定的图标和文本颜色更改为红色或您想要的每种颜色:

 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),
  ),

I think I found out the most efficient way to do it according to the docs.我想我根据文档找到了最有效的方法。 So you just need to wrap your ListTile() in a ListTileTheme() and then use the selectedColor: property, and then put your ListTile in the child: property like so child: ListTile()因此,您只需将ListTile()包装在 ListTileTheme( ListTileTheme()中,然后使用selectedColor:属性,然后将 ListTile 放入child:属性中,就像这样child: ListTile()

Finally we obtain something like this:最后我们得到这样的东西:

    ListTileTheme(
      selectedColor: Colors.red,
      child: ListTile(
        leading: const Icon(Icons.add),
        title: const Text('New Item'),
        selected: _selectedDestination == 1,
        onTap: () => selectDestination(1),
      )),

Check the docs for more info: ListTileTheme查看文档以获取更多信息: ListTileTheme

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM