gpt4 book ai didi

使用内置功能的DataTable排序进行 flutter

转载 作者:行者123 更新时间:2023-12-03 20:29:56 25 4
gpt4 key购买 nike

如何使用内置函数对 DataTable 上的列进行排序.
是否可以对多列进行操作,以便当我按下排序图标时,它实际上对列表进行了排序?

感谢您的任何支持:)

class HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
var myColumns = [
new DataColumn(label: new Text('name')),
new DataColumn(label: new Text('age')),
new DataColumn(label: new Text('Hight')),
];

var myRows = [
new DataRow(cells: [
new DataCell(new Text('George')),
new DataCell(new Text('18')),
new DataCell(new Text('173cm')),
]),
new DataRow(cells: [
new DataCell(new Text('Dave')),
new DataCell(new Text('21')),
new DataCell(new Text('183cm')),
]),
new DataRow(cells: [
new DataCell(new Text('Sam')),
new DataCell(new Text('55')),
new DataCell(new Text('170cm')),
])
];

return new Scaffold(
body: new Center(
child: new Container(
child: new DataTable(
columns: myColumns,
rows: myRows,
sortColumnIndex: 0,
sortAscending: true,
)),
),
);
}
}

enter image description here

最佳答案

我做了一些小版本,以帮助那些可能面临一些问题的人。

class Person {
String name;
int age;
num hight;
Person({this.name, this.age, this hight});
}

class HomePageState extends State<HomePage> {
bool _sortNameAsc = true;
bool _sortAgeAsc = true;
bool _sortHightAsc = true;
bool _sortAsc = true;
int _sortColumnIndex;
List<Person> _persons;

@override
initState() {
super.initState();
_persons = [
Person(
name: 'George',
age: 18,
height: 173,
),
Person(
name: 'Dave',
age: 21,
height: 183,
),
Person(
name: 'Sam',
age: 55,
height: 170,
),
];
}

@override
Widget build(BuildContext context) {
var myColumns = [
DataColumn(
label: Text('name'),
onSort: (columnIndex, sortAscending) {
setState(() {
if (columnIndex == _sortColumnIndex) {
_sortAsc = _sortNameAsc = sortAscending;
} else {
_sortColumnIndex = columnIndex;
_sortAsc = _sortNameAsc;
}
_persons.sort((a, b) => a.name.compareTo(b.name));
if (!_sortAscending) {
_persons = _persons.reversed.toList();
}
});
},
),
DataColumn(
label: Text('age'),
onSort: (columnIndex, sortAscending) {
setState(() {
if (columnIndex == _sortColumnIndex) {
_sortAsc = _sortAgeAsc = sortAscending;
} else {
_sortColumnIndex = columnIndex;
_sortAsc = _sortAgeAsc;
}
_persons.sort((a, b) => a.age.compareTo(b.age));
if (!_sortAscending) {
_persons = _persons.reversed.toList();
}
});
},
),
DataColumn(
label: Text('Hight'),
onSort: (columnIndex, sortAscending) {
setState(() {
if (columnIndex == _sortColumnIndex) {
_sortAsc = _sortHeightAsc = sortAscending;
} else {
_sortColumnIndex = columnIndex;
_sortAsc = _sortHeightAsc;
}
_persons.sort((a, b) => a.height.compareTo(b.height));
if (!_sortAscending) {
_persons = _persons.reversed.toList();
}
});
},
),
];

var myRows = _persons.map((person) {
return DataRow(cells: [
DataCell( Text(person.name)),
DataCell( Text('${person.age}')),
DataCell( Text('${person.height}cm')),
]);
});

return Scaffold(
body: Center(
child: Container(
child: DataTable(
columns: myColumns,
rows: myRows.toList(),
sortColumnIndex: _sortColumnIndex,
sortAscending: _sortAsc,
)),
),
);
}
}

关于使用内置功能的DataTable排序进行 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51434778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com