我想将多种类型的类添加到列表中,以便稍后在数据表中使用它。我的愿望是对特定列进行排序,并稍后使用其中的按钮执行一些操作。如何将多种类型的类添加到列表中?
因此我想向其中添加图标、字符串、整数、小部件,这可能吗? 注释行是不起作用的行。
我要写什么来添加图标或按钮? 有没有人有解决办法?
class _MyBooksState extends State<MyBooks> {
List<Map> _books = [
{
//Icons: Icon(Icons.warning),
"book": "Harry Potter",
"author": "Goethe",
"id": "1",
//"btn": Switch(
// inactiveThumbColor: Colors.white,
// activeColor: Colors.white,
// activeTrackColor: Colors.green,
// inactiveTrackColor: Colors.grey,
// value: true,
// onChanged: (value) {},
// ),
},
{
//Icons: Icon(Icons.warning),
"book": "Harry Potter",
"author": "Goethe",
"id": "2",
// "btn": Switch(
// inactiveThumbColor: Colors.white,
// activeColor: Colors.white,
// activeTrackColor: Colors.green,
// inactiveTrackColor: Colors.grey,
// value: true,
// onChanged: (value) {},
// ),
},
{
Icons: Icon(Icons.warning),
"book": "Harry Potter",
"author": "Goethe",
"id": "3",
// "btn": Switch(
// inactiveThumbColor: Colors.white,
// activeColor: Colors.white,
// activeTrackColor: Colors.green,
// inactiveTrackColor: Colors.grey,
// value: true,
// onChanged: (value) {},
// ),
}
];
int _currentSortColumn = 0;
bool _isSortAsc = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50),
child: MyAppBarNotificationsView(
title: "Books",
callback: () {},
),
),
body: ListView(
children: [_createDataTable()],
),
);
}
DataTable _createDataTable() {
return DataTable(
columns: _createColumns(),
rows: _createRows(),
sortColumnIndex: _currentSortColumn,
sortAscending: _isSortAsc,
);
}
List<DataColumn> _createColumns() {
return [
//DataColumn(label: Text('type')),
DataColumn(
label: Text('book'),
onSort: (columnIndex, _) {
setState(() {
_currentSortColumn = columnIndex;
if (_isSortAsc) {
_books.sort((a, b) => b['book'].compareTo(a['book']));
} else {
_books.sort((a, b) => a['book'].compareTo(b['book']));
}
_isSortAsc = !_isSortAsc;
});
},
),
DataColumn(label: Text('author')),
//DataColumn(label: Text('icons')),
DataColumn(label: Text('id')),
// DataColumn(label: Text('btn'))
];
}
List<DataRow> _createRows() {
return _books
.map((connection) => DataRow(cells: [
//DataCell(Text(connection['icons'])),
DataCell(Text(connection['book'])),
DataCell(Text(connection['author'])),
DataCell(Text(connection['id'])),
// DataCell(Text(connection['btn']))
]))
.toList();
}
}
问题出在地图列表的声明中。在 Flutter 中,您可以像 List