使用内置的DataTable排序功能颤动 [英] flutter using DataTable sorting built in function

查看:83
本文介绍了使用内置的DataTable排序功能颤动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I use the built in function for sorting columns on DataTable. Is it possible to do for multiple columns, so that when I press the sort icon it actually sorts the list?

感谢您的支持:)

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,
        )),
      ),
    );
  }
}

推荐答案

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

I faced with the same problem. Below is solution that works for me:

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,
        )),
      ),
    );
  }
}

这篇关于使用内置的DataTable排序功能颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆