Flutter DataTable 排序图标未显示

Ser*_*rte 2 flutter

Flutter DataTable 中的排序图标未显示,我不确定这是否是一个错误,或者我的代码是否错误。

\n

根据DataTable (Flutter Widget of the Week),我只需sortColumnIndex: 0DataTable()

\n

我的期望:\n带排序箭头的数据表

\n

我得到什么:没有排序箭头的数据表

\n

这是我正在使用的代码:

\n
import \'package:flutter/material.dart\';\n\nvoid main() {\n  runApp(MyApp());\n}\n\nclass MyApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: \'Flutter DataTable\',\n      home: DataScreen(),\n    );\n  }\n}\n\nclass DataScreen extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      body: Center(\n        child: DataTable(\n          sortColumnIndex: 0,\n          sortAscending: true,\n          columns: [\n            DataColumn(label: Text(\'Name\')),\n            DataColumn(label: Text(\'Year\')),\n          ],\n          rows: [\n            DataRow(cells: [\n              DataCell(Text(\'Dash\')),\n              DataCell(Text(\'2018\')),\n            ]),\n            DataRow(cells: [\n              DataCell(Text(\'Gopher\')),\n              DataCell(Text(\'2009\')),\n            ]),\n          ],\n        ),\n      ),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我知道,在 DataTable #26845 上存在无法禁用问题排序的问题,并且有一个拉取请求[DataTable] 在不排序时隐藏箭头填充 #51667。但即使我更改为开发频道,排序箭头也没有显示。我也厌倦了Flutter.dev DataTable 类上的交互式代码示例

\n

我测试过的版本:

\n
    \n
  • Flutter 1.22.1 \xe2\x80\xa2 通道稳定
  • \n
  • Flutter 1.22.1 \xe2\x80\xa2 通道测试版
  • \n
  • Flutter 1.23.0-7.0.pre \xe2\x80\xa2 通道开发
  • \n
  • Flutter 1.23.0-8.0.pre.283 \xe2\x80\xa2 通道主控
  • \n
\n

所以我不确定我是否错过了什么,或者它是否是一个错误。

\n

Jac*_*hen 5

您必须设置功能:onSort

class DataScreen extends StatefulWidget {
  @override
  _DataScreenState createState() => _DataScreenState();
}

class _DataScreenState extends State<DataScreen> {
  bool ascending = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: DataTable(
          sortColumnIndex: 0,
          sortAscending: ascending,
          columns: [
            DataColumn(
              label: Text('Name'),
              onSort: (columnIndex, ascending) {
                setState(() {
                  this.ascending = ascending;
                });
              },
            ),
            DataColumn(
              label: Text('Year'),
            ),
          ],
          rows: [
            DataRow(cells: [
              DataCell(
                Text('Dash'),
              ),
              DataCell(
                Text('2018'),
              ),
            ]),
            DataRow(cells: [
              DataCell(
                Text('Gopher'),
              ),
              DataCell(
                Text('2009'),
              ),
            ]),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 如何更改箭头图标? (6认同)