我的场景中有一个数据表。我想当用户将鼠标悬停在任何行上时更改行的背景颜色。我在 flutter.dev 上找到了几个示例,但没有一个有效。
例如,看下面的代码(完整代码)。虽然我将绿色作为背景颜色,但当我将鼠标悬停在行上时,它不会变成蓝色。
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return DataTable(
dataRowColor: MaterialStateProperty.resolveWith(_getDataRowColor),
columns: const <DataColumn>[
DataColumn(
label: Text(
'Name',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Age',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Role',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
],
rows: <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('Sarah')),
DataCell(Text('19')),
DataCell(Text('Student')),
],
onSelectChanged: (isSelected) => {
print('Item 1 clicked!')
},
),
DataRow(
cells: <DataCell>[
DataCell(Text('Janine')),
DataCell(Text('43')), …Run Code Online (Sandbox Code Playgroud) Widget DataTable 有一个内置图标用于排序指示
如何更改颜色甚至图标?
Scrollbar(
controller: _scrollController,
child: SingleChildScrollView(
controller: _scrollController,
scrollDirection: Axis.horizontal,
child: DataTable(
border: TableBorder.all(width: 0.2),
headingRowColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
return Styles.TableHeaderColor;
}),
columns: getColumns(TableTitles),
rows: getRows(),
sortColumnIndex: sortColumnIndex,
sortAscending: isAscending,
dataRowHeight: 50,
headingRowHeight: 40,
),
),
),
Run Code Online (Sandbox Code Playgroud)