Flutter中如何修改选中状态下ListTile的图标和文字颜色?
问题解答
1. 默认蓝色的来源
Drawer中ListTile选中状态的默认前景色(文字、图标颜色)来自全局主题ThemeData的colorScheme.primary属性,Material默认主题的主色为蓝色,因此默认显示蓝色选中效果。
2. 是否可以修改?
完全可以直接修改,常用两种实现方案:
方案1:单独修改当前ListTile的选中前景色
直接使用ListTile自带的selectedColor属性即可,该属性专门用于配置选中状态下的前景色(同时作用于图标和文字),修改后的代码如下:
int _selectedDestination = 0; ListTile( leading: const Icon(Icons.home), title: const Text('Home'), selected: _selectedDestination == 0, selectedTileColor: Colors.grey[200], // 你之前配置的选中背景色 selectedColor: Colors.red, // 新增:配置选中前景色,可替换为你需要的颜色 onTap: () => selectDestination(0), ), void selectDestination(int index) { setState(() { _selectedDestination = index; }); }
方案2:全局统一修改所有ListTile的选中前景色
如果需要整个应用内的ListTile选中前景色统一生效,可以在全局MaterialApp的主题配置中指定:
MaterialApp( theme: ThemeData( listTileTheme: const ListTileThemeData( selectedColor: Colors.green, // 全局配置所有ListTile的选中前景色 // 也可以同时配置全局选中背景色:selectedTileColor: xxx ) ), // 其他应用配置 )

内容的提问来源于stack exchange,提问作者Tim
相关产品推荐
相关产品推荐

