使用DataTable内置函数进行抖动

时间:2018-07-20 03:47:06

标签: flutter

如何使用内置函数对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

2 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。以下是适用于我的解决方案:

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(perosn.name)),
         DataCell( Text('${person.age}')),
         DataCell( Text('${person.height}cm')),
      ]);
    });

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

答案 1 :(得分:0)

我建议您以一种简单的方式来做。 假设存在一个名称为 StudentDetails (学生详细信息)和属性 姓名,年龄,身高。 列出具有不同值的该类对象。

StudentDetails s1, s2, s3;
s1.name = George;
s1.age = 18;
s1.height = 173;
//ans same for the rest of two students .
List<dynamic> sd = [s1,s2,s3];

现在将其传递到DataCell列表 这样。

var myRows= [ ];
for(int i = 0 ; i<sd.length;i++)
{
myRows.add(DataCell(child:Text(sd[i].name);
myRows.add(DataCell(child:Text(sd[i].age);
myRows.add(DataCell(child:Text(sd[i].height);
}

现在当您必须对表格进行排序时,请按“排序”图标按

setState((){
sd.sort((a,b)=>a.name.compareTo(b.name));
});